pipeline: WIP flattening links (flat() not working?)

This commit is contained in:
Hans Fast 2026-03-18 21:11:55 +01:00
parent f1f6c74e98
commit 4f2a4d9ae1
3 changed files with 59 additions and 0 deletions

19
getLinkedFilesFromHtml.js Normal file
View File

@ -0,0 +1,19 @@
import {fromHtml} from 'hast-util-from-html';
import {visit} from 'unist-util-visit';
export default async function(value) {
const tree = fromHtml(value);
const links = [];
//take tree;
//visit nodes matching one of the test patterns;
//apply function to those nodes.
visit(tree, [
{tagName: 'link'},
{tagName: 'img'}
], function(node) {
links.push(node);
})
return links.map(link => link.properties.href || link.properties.src)
}

36
pathsToObjs.js Normal file
View File

@ -0,0 +1,36 @@
// Original source - https://stackoverflow.com/a/44681235
// Posted by le_m
// Retrieved 2026-03-11, License - CC BY-SA 3.0
// Insert path into directory tree structure:
//
//modified to construct an object instead of an array.
function insert(tree = {}, [head, ...tail]) {
if (tail.length > 0) {
tree[head] = insert(tree[head], tail)
} else {
tree[head] = true
}
return tree;
}
// Example:
let examplepaths = [
'css/style.css',
'about/bird.svg',
];
//naive! assumes relative path like in example.
//A more robust implementation would need to resolve paths first, but to what.
export default function process(paths) {
return paths
.map(path => path.split('/')
//.slice(1) //if path starts with / or ./
)
.reduce((tree, path) => insert(tree, path), {});
}
//test:
// console.log(process(examplepaths));

View File

@ -61,6 +61,10 @@
} }
}) })
links: Tree.flat(Tree.map(renderedPages, getLinkedFilesFromHtml.js), 3)
//→ (links) => Tree.flat(links, 2) // → (a) => [...new Set(a)]
//linksAstree: pathsToObjs.js(links)
/* /*
assets are relative to the pubfiles directory assets are relative to the pubfiles directory