All files / packages/design-system-2-site/src/lib/zeroheight links.js

100% Statements 11/11
100% Branches 18/18
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                            15x                 12x 16x 16x 15x 15x   14x 16x      
/**
 * Zeroheight page links -> site routes, through the manifest: `/p/<page>` is
 * the page's landing file and `/b/<tab>` is the tab's file (the `/b/` id is
 * the tab uid). `/t/<heading>` anchors resolve to the page until headings get
 * stable ids.
 */
 
import { LINK_ALIASES, LINK_ROUTES } from './fixes.js';
 
/**
 * `content/components/badge--usage.md` -> `/components/badge--usage`.
 * @param {string} file
 */
export function routeForFile(file) {
  return `/${file.replace(/^content\//, '').replace(/\.mdx?$/, '')}`;
}
 
/**
 * @param {{ pages: Record<string, import('./manifest.js').ManifestEntry> }} manifest
 * @param {{ aliases?: typeof LINK_ALIASES, routes?: typeof LINK_ROUTES }} [fixes]
 * @returns {(link: import('./convert.js').ZhLink) => string | null}
 */
export function createLinkResolver(manifest, { aliases = LINK_ALIASES, routes = LINK_ROUTES } = {}) {
  return ({ page, tab }) => {
    const uid = aliases[page]?.to ?? page;
    if (routes[uid]) return routes[uid].to;
    const entry = manifest.pages[uid];
    if (!entry || entry.decision !== 'port' || !entry.localFile) return null;
    // A tab Zeroheight removed falls back to its page.
    const tabFile = tab ? entry.tabs.find((t) => t.uid === tab && t.localFile)?.localFile : null;
    return routeForFile(tabFile ?? entry.localFile);
  };
}