From b6fb7ac46900872f21d4648f4dcb594217449ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20R=C3=A9millard?= Date: Thu, 21 Dec 2023 12:52:36 -0500 Subject: [PATCH] fix(parser): Misc --- config/linkParser.js | 62 ++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/config/linkParser.js b/config/linkParser.js index 3991f2d..1defe7c 100644 --- a/config/linkParser.js +++ b/config/linkParser.js @@ -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} */(document.querySelectorAll('a[href]'))) // Exclude those inside a rss module @@ -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) - } -}