Files
mx be89d848dc Max/try fix doc search (#4692)
* minimised dropdown bar

* update ci scripts

* theme changes to dev portal

* theme changes to operators

* theme changes to docs

* theme -> themes

* fixed theme -> themes import in book

* removed bak files

* remove logging from post_process + remove search feature from mdbook

---------

Co-authored-by: mfahampshire <mfahampshire@pm.me>
2024-07-05 12:19:14 +02:00

88 lines
2.7 KiB
JavaScript

import { unified } from "unified";
import parse from "rehype-parse";
import inspectUrls from "@jsdevtools/rehype-url-inspector";
import stringify from "rehype-stringify";
import { read, write } from "to-vfile";
import path from "path";
import fs from "fs";
import { glob } from "glob";
async function main() {
const distDir = "../../../dist/docs";
const items = [];
const books = [
'developers',
'docs',
'operators',
];
for (const book of books) {
// only process the root `index.html` files, because they have absolute paths instead of relative paths
const filenames = [path.resolve(distDir, book, 'index.html')];
// leaving this here for a future where other files need to be processed
// const filenames = await glob(path.resolve(distDir, book) + '/**/*.html');
for (const f of filenames) {
// Create a Rehype processor with the inspectUrls plugin
const processor = unified()
.use(parse)
.use(inspectUrls, {
inspectEach(args) {
const { url: rawUrl, propertyName } = args;
const { tagName } = args.node;
const filename = args.file.history[0];
const relativeFilename = path.relative(distDir, filename);
const relativeDirectory = path.dirname(relativeFilename);
// remove relative paths from URL
const bareUrl = rawUrl.split('/').filter(c => c !== '.' && c !== '..').join('/');
let url;
if (rawUrl.includes('.html#')) {
url = path.join(`/${relativeDirectory}`, bareUrl);
} else if (rawUrl.startsWith('#')) {
url = path.join(`/`, relativeFilename + bareUrl);
} else {
url = path.join(`/${book}`, bareUrl);
}
// const item = { filename, relativeDirectory, tagName, propertyName, rawUrl, url };
const item = { tagName, rawUrl, url };
// if (tagName === 'a') {
// console.log(`tagName = a, args are: $args`);
// }
if (!rawUrl.startsWith('http')) {
if (tagName === 'link' || tagName === 'script' || tagName === 'a') {
args.node.properties[propertyName] = url;
items.push(item);
}
}
}
})
.use(stringify);
// Read the example HTML file
const filename = path.resolve(distDir, f);
console.log(`${filename}...`);
let file = await read(filename);
// Crawl the HTML file and find all the URLs
const res = await processor.process(file);
fs.writeFileSync(filename, res.value);
}
}
// console.table(items);
// console.log();
}
main();