How to travel through nodes and edit it


(Tuan Anh Tran) #1

I tried to use parseNode but when i made a change to a node content, it doesn’t seem to get saved. the content remains the same as the old one.


(Robin Ward) #2

Does it work in preview but not when saved to the server? Did you restart your rails server?


(Tuan Anh Tran) #3

@eviltrout could u help me take a look at this snippet. When i tried to console.log to see node[1] value, it’s as expected (single \n is replaced with \n\n but it doesn’t save to the post and doesnt get cooked.

Discourse.Dialect.on("parseNode", function(event) {
  var node = event.node,
      path = event.path;

  if (node[0] !== 'p') { return; };
  var parent = path[path.length - 1];
  if (parent[0] === 'blockquote') {
    console.log('Found a quote! Im outa here');
    return;
  }

  var split = node[1].split('\n');
  if (split.length) {
    node[1] = split.join('\n\n');
  }
});

(Robin Ward) #4

This looks correct to me. You’re saying it’s being saved in the node but then later removed?


(Tuan Anh Tran) #5

i haven’t setup the local dev yet so i paste that snippet to console.

when i click save, for a split second, the post content was change. look like a new line char is added but then they went back like before.

btw, how do i wrap a node within p tag?


(Robin Ward) #6

You really need a proper development environment set up to test this kind of work. I really can’t help you until you get all that working yourself.

If you are just pasting it in the console, it will only work for what’s in the current browser memory (in fact even then some of our Dialect calls expect to only be called when the app boots). The reason it reverts is because the server doesn’t have access to your changes. You need to set up a dev environment and create a plugin for your code.