Automatically setting default unique ID on each node

Based on the Linter example code I have been trying to give each node a unique identifier, unfortunately after the node is set once all the following nodes get the same id.

function lint(doc) {
  function record(msg, from, to, fix) {
    result.push({msg, from, to, fix})
  }

  doc.descendants((node, pos) => {
    if (node.isText && node.text.trim() != '') {
      if (node.attrs.id == undefined) {
        node.attrs.id = uuidv4();
      }
      record("ID=" + node.attrs.id, pos, pos + node.text.length)
    }
  })
  return result
}

function uuidv4() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

Nodes, and everything in them, are immutable (and possibly shared). In this case, you’re changing the default attrs object that all the other nodes of this type are also using. Update attributes with a transaction, not by mutating the node.