All files / apps/developer-portal/tools/lib remarkDocLinks.ts

95.34% Statements 41/43
81.08% Branches 30/37
100% Functions 8/8
100% Lines 33/33

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                  1x       32x   32x 25x   25x   25x   25x   12x   12x       1x 27x 21x 20x         1x   8x   8x   8x     1x 10x   4x   4x               8x 8x   7x   7x 12x   12x   10x       12x   8x   8x     7x 1x        
import { existsSync } from "node:fs";
import { relative, resolve } from "node:path";
import {
    isPlaceholder,
    REPO_SCHEME,
    resolveDocImage,
    resolveDocLink,
} from "#layers/docs/shared/docLinks.ts";
import { WORKSPACE_ROOT } from "./paths.ts";
 
interface MarkdownNode {
    type: string;
    url?: string;
    children?: MarkdownNode[];
}
 
/** `@nuxtjs/mdc` hands the parser the collection's file record, whose `path` is absolute. */
interface SourceFile {
    path?: string;
}
 
/**
 * Reference definitions and bare autolinks are not used anywhere in these docs, so an inline `link`
 * is every link there is. Takes the parent, because a placeholder is replaced by its own children.
 */
const eachLink = (
    parent: MarkdownNode,
    apply: (link: MarkdownNode) => MarkdownNode[] | null,
): void => {
    const children = parent.children ?? [];
 
    for (let index = children.length - 1; index >= 0; index -= 1) {
        const child = children[index];
 
        Iif (!child) continue;
 
        eachLink(child, apply);
 
        if (child.type !== "link") continue;
 
        const replacement = apply(child);
 
        if (replacement) children.splice(index, 1, ...replacement);
    }
};
 
const eachImage = (parent: MarkdownNode, apply: (image: MarkdownNode) => void): void => {
    for (const child of parent.children ?? []) {
        if (child.type === "image") apply(child);
        else eachImage(child, apply);
    }
};
 
/** The `.md` is appended rather than required, because a link may omit it and `checkLink` accepts that. */
const isPage = (repoPath: string): boolean => {
    // `content.config.ts` leaves every CLAUDE.md out of the collection, so none has a page.
    Iif (/(?:^|\/)CLAUDE(?:\.md)?$/i.test(repoPath)) return false;
 
    const absolute = resolve(WORKSPACE_ROOT, repoPath);
 
    return /\.mdx?$/i.test(repoPath) ? existsSync(absolute) : existsSync(`${absolute}.md`);
};
 
const isMissing = (url: string): boolean => {
    if (!url.startsWith(REPO_SCHEME)) return false;
 
    const [path = ""] = url.slice(REPO_SCHEME.length).split("#");
 
    return !existsSync(resolve(WORKSPACE_ROOT, decodeURIComponent(path)));
};
 
/**
 * Remark, not rehype: the mdast-to-hast step strips `.md` from a relative href on its way through,
 * and that extension is the only thing distinguishing a page from a directory. Do not move this later.
 */
export default function remarkDocLinks() {
    return (tree: MarkdownNode, file: SourceFile): void => {
        if (!file.path) return;
 
        const from = relative(WORKSPACE_ROOT, file.path);
 
        eachLink(tree, (link) => {
            const href = link.url ?? "";
 
            if (isPlaceholder(href)) return link.children ?? [];
 
            const url = resolveDocLink(href, from, isPage) ?? href;
 
            // `@nuxt/content` rewrites a `repo:…/x.md` href to the relative route `…/x`, which the
            // prerender crawler follows into a 404; the page's broken-links banner still names it.
            if (isMissing(url)) return link.children ?? [];
 
            link.url = url;
 
            return null;
        });
 
        eachImage(tree, (image) => {
            image.url = resolveDocImage(image.url ?? "", from) ?? image.url;
        });
    };
}