Skip to content

Instantly share code, notes, and snippets.

@jacksonh
Created August 16, 2022 04:57
Show Gist options
  • Save jacksonh/3591fd4167ef38d9f3d5fd3ffb453ece to your computer and use it in GitHub Desktop.
Save jacksonh/3591fd4167ef38d9f3d5fd3ffb453ece to your computer and use it in GitHub Desktop.
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