Skip to content

Commit

Permalink
fix(parser): Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
remillc committed Dec 21, 2023
1 parent e2648c6 commit b6fb7ac
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions config/linkParser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
export function linkParser() {

function normalizeText(str) {
if (typeof str !== 'string') {
return str
}

return str.replace(/\n/g, '').trim()
}

// @ts-ignore
function getNodeText(node) {
let text = null

if (!(node instanceof HTMLElement)) {
return text
}

if (node.nodeName === 'A') {
text = node.innerText
if (!normalizeText(text) && node.querySelector('img[alt]')) {
text = node.querySelector('img[alt]').getAttribute('alt')
}

} else if (node.nodeName === 'IMG') {
if (node.hasAttribute('alt')) {
text = node.getAttribute('alt')
} else if (node.hasAttribute('title')) {
text = node.getAttribute('title')
}
}

return normalizeText(text)
}

return Array
.from(/** @type {NodeListOf<HTMLAnchorElement>} */(document.querySelectorAll('a[href]')))
// Exclude those inside a rss module
Expand All @@ -21,31 +55,3 @@ export function linkParser() {
}))
)
}

function normalizeText(str) {
return str.replace(/\n/g, '').trim()
}

// @ts-ignore
export function getNodeText(node) {
let text = null

if (!node instanceof HTMLElement) {
return text
}

if (node.nodeName === 'A') {
text = node.innerText
if (normalizeText(text) === '' && node.querySelector('img[alt]')) {
text = node.querySelector('img[alt]').getAttribute('alt')
}

} else if (node.nodeName === 'IMG') {
if (node.hasAttribute('alt')) {
text = node.getAttribute('alt')
} else if (node.hasAttribute('title')) {
text = node.getAttribute('title')
}
return normalizeText(text)
}
}

0 comments on commit b6fb7ac

Please sign in to comment.