linkStrings

tcmts.share.linkStrings · #2m4mgzkz · function · runs anywhere

Highlighted code (highlight's) with the strings targets names made links to them, as references are (linkRefs): a manifest's names and files.

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

export function linkStrings(html: string, targets: Map<string, string>): string {
  const plain = (s: string) =>
    s
      .replace(/&quot;/g, '"')
      .replace(/&#39;/g, "'")
      .replace(/&lt;/g, "<")
      .replace(/&gt;/g, ">")
      .replace(/&amp;/g, "&");
  return html.replace(
    /<span class="s">((?:&quot;|&#39;|")([^<]*?)(?:&quot;|&#39;|"))<\/span>/g,
    (all, quoted: string, inner: string) => {
      const name = targets.get(plain(inner));
      return name
        ? `<a class="ref s" href="${codeHref(name)}" title="${esc(name)}">${quoted}</a>`
        : all;
    },
  );
}

uses

used by