All files / apps/developer-portal/layers/docs/shared docLinks.ts

94.28% Statements 33/35
92.3% Branches 24/26
100% Functions 6/6
100% Lines 28/28

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      7x   7x     7x   7x 103x   7x 23x   23x 49x 47x     23x       7x     7x 5x   2x       34x                 7x         28x 7x   21x 21x 28x   28x   21x 28x   28x   21x     10x    
import { toCollectionPath } from "./docPages.ts";
 
/** Produced here, consumed by `ProseA`, which alone knows the forge's URL. */
export const REPO_SCHEME = "repo:";
 
const ABSOLUTE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
 
/** A `file.ts:91-94` link narrows a file to its lines; it does not name a different one. */
const LINE_RANGE = /:\d+(?:-\d+)?$/;
 
const segmentsOf = (path: string): string[] =>
    path.split("/").filter((segment) => segment !== "" && segment !== ".");
 
const resolveAgainst = (base: string[], path: string): string => {
    const out = [...base];
 
    for (const segment of segmentsOf(path)) {
        if (segment === "..") out.pop();
        else out.push(segment);
    }
 
    return out.join("/");
};
 
/** Where the build serves a docs folder's images; a folder is listed in the docs layer's `nuxt.config.ts`. */
export const DOC_ASSETS = "/doc-assets";
 
/** A relative image resolved to where the build serves it, or null for one that is already a URL. */
export const resolveDocImage = (src: string, fromPath: string): string | null => {
    if (src === "" || ABSOLUTE.test(src) || src.startsWith("/")) return null;
 
    return `${DOC_ASSETS}/${resolveAgainst(segmentsOf(fromPath).slice(0, -1), src)}`;
};
 
/** A `<file>` in a link is an instruction to whoever copies the doc, not a target. */
export const isPlaceholder = (href: string): boolean => href.includes("<");
 
/**
 * The route for a link, `repo:`-prefixed when its target has no page here, or null for one that is
 * already somebody else's to resolve: an external URL or a bare anchor.
 *
 * `isPage` is asked about every target, not only an extension-less one — a `.md` link may name a
 * file the collection does not have, and routing it anyway is what renders a 200 saying "No such page".
 */
export const resolveDocLink = (
    href: string,
    fromPath: string,
    isPage: (repoPath: string) => boolean,
): string | null => {
    if (href === "" || ABSOLUTE.test(href) || href.startsWith("#") || isPlaceholder(href))
        return null;
 
    const [target = "", ...fragment] = href.split("#");
    const anchor = fragment.length > 0 ? `#${fragment.join("#")}` : "";
    const path = target.replace(LINE_RANGE, "");
 
    Iif (path === "") return null;
 
    const base = href.startsWith("/") ? [] : segmentsOf(fromPath).slice(0, -1);
    const resolved = resolveAgainst(base, path);
 
    Iif (resolved === "") return null;
 
    if (isPage(resolved)) return `/wiki${toCollectionPath(resolved)}${anchor}`;
 
    // `@nuxt/content` turns any non-http href ending in `.md` into a relative route, dropping the scheme.
    return `${REPO_SCHEME}${resolved.replace(/\.(mdx?)$/i, "%2E$1")}`;
};