Skip to content

Instantly share code, notes, and snippets.

@davidpmccormick
Created February 2, 2017 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpmccormick/f1e24eceba13bdaf144af154c53a1c7a to your computer and use it in GitHub Desktop.
Save davidpmccormick/f1e24eceba13bdaf144af154c53a1c7a to your computer and use it in GitHub Desktop.
function unwrapChildren(node, tag) {
const treeAdapter = parse.treeAdapters.default;
const nodeTagName = treeAdapter.getTagName(node);
const newEl = treeAdapter.createElement(nodeTagName);
const childNodes = treeAdapter.getChildNodes(node);
childNodes.forEach((childNode) => {
if (childNode.nodeName !== tag) {
treeAdapter.appendChild(newEl, childNode);
} else {
const childContent = treeAdapter.getChildNodes(childNode);
childContent.forEach((item) => {
treeAdapter.appendChild(newEl, item);
});
}
});
return newEl;
}
@jamesgorrie
Copy link

Would it be simpler to make this more specific e.g. unwrap em?
And basically say if it's em, empty the parent and fill it with the em's children?

@jamesgorrie
Copy link

Or is there an edge case I am missing e.g:

<p class="standfirst">
  <em>
    some stuff that is italic <a href="asite.com">Somethinghere</a> and more italic
  </em>
  Back to plane old straight up text, maybe some <a href="link.com">links too</a>
</p>

@davidpmccormick
Copy link
Author

Would it be simpler to make this more specific

Yeah – sounds good to me. I haven't managed to get this working in any way, yet though. Not sure what's wrong with it. Shout if you have any ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment