Created
August 16, 2022 04:57
-
-
Save jacksonh/3591fd4167ef38d9f3d5fd3ffb453ece 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
if (article?.dom) { | |
const ANCHOR_ELEMENTS_BLOCKED_ATTRIBUTES = [ | |
'omnivore-highlight-id', | |
'data-twitter-tweet-id', | |
'data-instagram-id', | |
] | |
// Get the top level element? | |
const pageNode = article.dom.firstElementChild as HTMLElement | |
console.log("pageNode: ", pageNode) | |
if (!pageNode || pageNode.childNodes.length == 0) { | |
// return [] | |
} | |
const nodesToVisitStack: [HTMLElement] = [pageNode] | |
const visitedNodeList = [] | |
while (nodesToVisitStack.length > 0) { | |
const currentNode = nodesToVisitStack.pop() | |
console.log("currentNode: ", currentNode?.nodeType) | |
if ( | |
currentNode?.nodeType !== 1 || | |
// Avoiding dynamic elements from being counted as anchor-allowed elements | |
ANCHOR_ELEMENTS_BLOCKED_ATTRIBUTES.some((attrib) => | |
currentNode.hasAttribute(attrib) | |
) | |
) { | |
continue | |
} | |
visitedNodeList.push(currentNode) | |
;[].slice | |
.call(currentNode.childNodes) | |
.reverse() | |
.forEach(function (node) { | |
nodesToVisitStack.push(node) | |
}) | |
} | |
visitedNodeList.shift() | |
visitedNodeList.forEach((node, index) => { | |
// start from index 1, index 0 reserved for anchor unknown. | |
node.setAttribute('data-omnivore-anchor-idx', (index + 1).toString()) | |
}) | |
console.log("article content:", article.dom.outerHTML) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment