linkRefs

tcmts.share.linkRefs · #tvbqdacw · term · runs anywhere

Highlighted code (highlight's) with its references made links: to the page of the definition or namespace they name, with the definition's name and type to show on hover.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { esc } from "#codebase/tcmts/cli/pages";
import { codeHref } from "#codebase/tcmts/share";

export function linkRefs(cb: Codebase, html: string): string {
  return html
    .replace(/<a class="([^"]*)" data-hash="([a-z2-7]+)">/g, (all, cls: string, h: string) => {
      const name = cb.store.pathsOf(h)[0];
      const rec = cb.store.getDef(h);
      if (!name || !rec) return `<a class="${cls}">`;
      const sig = rec.kind === "term" ? `: ${cb.store.shownSignature(rec)}` : ` (${rec.kind})`;
      return `<a class="${cls}" href="${codeHref(name)}" title="${esc(name + sig)}">`;
    })
    .replace(
      /<a class="([^"]*)" data-namespace="([^"]*)">/g,
      (_, cls: string, ns: string) => `<a class="${cls}" href="${codeHref(ns)}">`,
    )
    .replace(
      /<a class="([^"]*)" data-package-name="[^"]*" data-package-range="[^"]*">/g,
      '<a class="$1">',
    );
}

uses

used by