Skip to content

Instantly share code, notes, and snippets.

@jgusta
Last active August 26, 2023 11:28
Show Gist options
  • Save jgusta/03176d52a1b44b5675bb884f41510c93 to your computer and use it in GitHub Desktop.
Save jgusta/03176d52a1b44b5675bb884f41510c93 to your computer and use it in GitHub Desktop.
const walk = (acc = [], outer) => {
if (typeof outer === null) {
return acc
}
if (typeof outer === 'string') {
if (outer === ' ') {
return acc
}
if (outer === '\n') {
return [...acc,"<br></br>"];
}
return [...acc, outer];
}
if (Array.isArray(outer)) {
let mine = [];
for (const inner of outer) {
if(Array.isArray(inner)) {
mine = [...mine, ...walk([],inner)]
}
else {
mine = [...mine, walk([],inner)]
}
}
return [...acc, ...mine];
}
if (outer.nodeType === 3) {
return [...acc, outer.textContent];
}
if (outer.nodeType === 1 && outer.hasChildNodes()) {
let mine = [];
for (const inner of outer.childNodes) {
mine = [...mine, ...walk([], inner)]
}
return [...acc, ...mine];
}
return acc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment