relinked
uses node:path
A page's text with its links following a move: links to from lead to to, and if the page is the one that
moved (name was from), its links to others are written from where it is now. Undefined if nothing changes.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { fileContent, formatOf, linkedName, markdownLinks, relativeLink } from "#codebase/tcmts/cli/files"; import type { DefRecord } from "#codebase/tcmts/core/types"; import path from "node:path"; export function relinked(cb: Codebase, name: string, rec: DefRecord, from: string, to: string): string | undefined { if (formatOf(rec) !== "md") return undefined; const text = fileContent(cb, rec, name.slice(0, Math.max(0, name.lastIndexOf(".")))).toString("utf8"); const was = name === to ? from : name; let out = text; for (const l of markdownLinks(text).reverse()) { if (l.code) continue; const target = linkedName(was, l.target); if (!target) continue; const reaches = target.name === from ? to : target.name; if (reaches === target.name && was === name) continue; const format = path.extname(l.target.replace(/[#?].*$/, "")).slice(1); out = out.slice(0, l.start) + relativeLink(name, reaches, format) + target.rest + out.slice(l.end); } return out === text ? undefined : out; }
uses
- tcmts.cli.codebase.Codebase
- tcmts.cli.files.fileContent
- tcmts.cli.files.formatOf
- tcmts.cli.files.linkedName
- tcmts.cli.files.markdownLinks
- tcmts.cli.files.relativeLink
- tcmts.core.types.DefRecord