import Prism from "https://esm.sh/prismjs@1.29.0"; import "https://esm.sh/prismjs@1.29.0/components/prism-jsx.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-typescript.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-tsx.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-diff.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-json.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-bash.js?no-check"; import "https://esm.sh/prismjs@1.29.0/components/prism-yaml.js?no-check"; export { extract as frontMatter } from "$std/front_matter/yaml.ts"; import * as Marked from "https://esm.sh/marked@7.0.2"; import { escape as escapeHtml } from "$std/html/entities.ts"; import { mangle } from "$marked-mangle"; Marked.marked.use(mangle()); const ADMISSION_REG = /^

\[(info|warn|tip)\]:\s/; export interface MarkdownHeading { id: string; html: string; } class DefaultRenderer extends Marked.Renderer { headings: MarkdownHeading[] = []; text(text: string): string { // Smartypants typography enhancement return text .replaceAll("...", "…") .replaceAll("--", "—") .replaceAll("---", "–") .replaceAll(/(\w)'(\w)/g, "$1’$2") .replaceAll(/s'/g, "s’") .replaceAll("'", "’") .replaceAll(/["](.*?)["]/g, "“$1”") .replaceAll(/"(.*?)"/g, "“$1”") .replaceAll(/['](.*?)[']/g, "‘$1’"); } heading( text: string, level: 1 | 2 | 3 | 4 | 5 | 6, raw: string, slugger: Marked.Slugger, ): string { const slug = slugger.slug(raw); this.headings.push({ id: slug, html: text }); return `${text}`; } link(href: string, title: string | null, text: string) { const titleAttr = title ? ` title="${title}"` : ""; if (href.startsWith("#")) { return `${text}`; } if (this.options.baseUrl) { try { href = new URL(href, this.options.baseUrl).href; } catch (_) { // } } return `${text}`; } image(src: string, title: string | null, alt: string | null) { return `${alt ?? `; } code(code: string, info: string | undefined): string { // format: tsx // format: tsx my/file.ts // format: tsx "This is my title" let lang = ""; let title = ""; const match = info?.match(/^([\w_-]+)\s*(.*)?$/); if (match) { lang = match[1].toLocaleLowerCase(); title = match[2] ?? ""; } let out = `

`; if (title) { out += `
${title ? escapeHtml(String(title)) : " "}
`; } const grammar = lang && Object.hasOwnProperty.call(Prism.languages, lang) ? Prism.languages[lang] : undefined; if (grammar === undefined) { out += `
${escapeHtml(code)}
`; } else { const html = Prism.highlight(code, grammar, lang); out += `
${html}
`; } out += `
`; return out; } blockquote(quote: string): string { const match = quote.match(ADMISSION_REG); if (match) { const label: Record = { tip: "Tip", warn: "Warning", info: "Info", }; const type = match[1]; quote = quote.slice(match[0].length); const icon = ``; return `
\n${icon}${ label[type] }${quote}
\n`; } return `
\n${quote}
\n`; } } export interface MarkdownOptions { inline?: boolean; } export function renderMarkdown( input: string, opts: MarkdownOptions = {}, ): { headings: MarkdownHeading[]; html: string } { const renderer = new DefaultRenderer(); const markedOpts: Marked.MarkedOptions = { gfm: true, renderer, }; const html = opts.inline ? Marked.parseInline(input, markedOpts) as string : Marked.parse(input, markedOpts) as string; return { headings: renderer.headings, html }; }