import MarkdownIt from "markdown-it";
import type { MarkdownIt as MarkdownRenderer, StateCore } from "markdown-it";
import { highlight } from "sugar-high";
import { linkTextReferences } from "./references";
export type MarkdownContext = {
owner: string;
repo: string;
ref: string;
/** Directory of the document inside the repo, used to resolve relative links. */
basePath: string;
};
const ATTACHMENT_PREFIX = "/attachments/";
const ABSOLUTE_URL = /^(?:[a-z][a-z0-9+.-]*:|\/\/|#)/i;
function createRenderer(): MarkdownRenderer {
const md = new MarkdownIt({
html: false,
linkify: true,
typographer: false,
highlight: (code) => `<pre class="sh-code"><code>${highlight(code)}</code></pre>`,
});
md.validateLink = (url) =>
!/^(javascript|vbscript|data):/i.test(url.trim()) || /^data:image\/(png|gif|jpe?g|webp);/i.test(url.trim());
return md;
}
const renderer = createRenderer();
function resolveRepoPath(basePath: string, target: string): string {
const parts = (target.startsWith("/") ? [] : basePath.split("/").filter(Boolean)).concat(target.split("/"));
const out: string[] = [];
for (const part of parts) {
if (!part || part === ".") continue;
if (part === "..") out.pop();
else out.push(part);
}
return out.join("/");
}