From 4f2a4d9ae15f7038562d2b0e6cb73be02dcd5aba Mon Sep 17 00:00:00 2001 From: Hans Fast Date: Wed, 18 Mar 2026 21:11:55 +0100 Subject: [PATCH] pipeline: WIP flattening links (flat() not working?) --- getLinkedFilesFromHtml.js | 19 +++++++++++++++++++ pathsToObjs.js | 36 ++++++++++++++++++++++++++++++++++++ pipeline.ori | 4 ++++ 3 files changed, 59 insertions(+) create mode 100644 getLinkedFilesFromHtml.js create mode 100644 pathsToObjs.js diff --git a/getLinkedFilesFromHtml.js b/getLinkedFilesFromHtml.js new file mode 100644 index 0000000..97ae245 --- /dev/null +++ b/getLinkedFilesFromHtml.js @@ -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) +} diff --git a/pathsToObjs.js b/pathsToObjs.js new file mode 100644 index 0000000..c48d471 --- /dev/null +++ b/pathsToObjs.js @@ -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)); diff --git a/pipeline.ori b/pipeline.ori index 159e98f..0b34c73 100644 --- a/pipeline.ori +++ b/pipeline.ori @@ -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