Files
documentation/vendor/archify/renderers/shared/sidecar-path.mjs
T
jarvis-at-skic 9ad62a2ed6
Build and publish Eldorado documentation / build (pull_request) Successful in 29s
feat: add section navigation and embedded Archify diagrams
2026-10-02 10:33:55 +00:00

39 lines
1.2 KiB
JavaScript

import { createHash } from 'node:crypto';
const SIDECAR_STEM_NAMESPACE = /\.~archify-[0-9a-f]{64}$/iu;
export function sidecarStemNeedsBounding(stem, suffixes) {
const fits = (value) => value.length <= 255 && Buffer.byteLength(value, 'utf8') <= 255;
return !suffixes.every((suffix) => fits(`${stem}${suffix}`));
}
export function boundedSidecarStem(stem, suffixes, { force = false, hashDomain } = {}) {
if (!force && !sidecarStemNeedsBounding(stem, suffixes)
&& !SIDECAR_STEM_NAMESPACE.test(stem)) return stem;
const hashInput = hashDomain ? `${hashDomain}\0${stem}` : stem;
const marker = `.~archify-${createHash('sha256').update(hashInput).digest('hex')}`;
const codePoints = [...stem];
while (codePoints.length
&& sidecarStemNeedsBounding(`${codePoints.join('')}${marker}`, suffixes)) {
codePoints.pop();
}
return `${codePoints.join('')}${marker}`;
}
export function sidecarStemFromComponent(component) {
if (component.endsWith('.html')) {
return {
stem: component.slice(0, -'.html'.length),
options: undefined,
};
}
return {
stem: component,
options: { force: true, hashDomain: 'full-component' },
};
}
export function isBoundedSidecarStem(stem) {
return SIDECAR_STEM_NAMESPACE.test(stem);
}