MCPcopy
hub / github.com/midudev/libros-programacion-gratis / normalizeHtml

Function normalizeHtml

web/scripts/import-97-things.mjs:32–50  ·  view source on GitHub ↗
(html, pageUrl, localAdvicePaths)

Source from the content-addressed store, hash-verified

30};
31
32const normalizeHtml = (html, pageUrl, localAdvicePaths) =>
33 html
34 .replace(/\sstyle="[^"]*"/g, '')
35 .replace(/\shref="([^"]+)"/g, (_, href) => {
36 const absoluteHref = new URL(decodeEntities(href), pageUrl).toString();
37 const localHref = localAdvicePaths.get(absoluteHref);
38
39 if (localHref) {
40 return ` href="${localHref}"`;
41 }
42
43 const isHttp = /^https?:\/\//.test(absoluteHref);
44 const safeAttrs = isHttp ? ' target="_blank" rel="nofollow noreferrer"' : '';
45
46 return ` href="${absoluteHref}"${safeAttrs}`;
47 })
48 .replace(/\ssrc="([^"]+)"/g, (_, src) => ` src="${new URL(decodeEntities(src), pageUrl).toString()}"`)
49 .replace(/\n{3,}/g, '\n\n')
50 .trim();
51
52const fetchText = async (url) => {
53 const response = await fetch(url);

Callers 1

parseAdvicePageFunction · 0.85

Calls 1

decodeEntitiesFunction · 0.85

Tested by

no test coverage detected