Last active
August 26, 2023 11:28
-
-
Save jgusta/03176d52a1b44b5675bb884f41510c93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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