import MarkdownIt from "markdown-it"; import { imagePath } from "../storage/images.ts"; const markdown = new MarkdownIt({ html: false, linkify: false, breaks: true }); markdown.validateLink = (url) => /^(https?:|mailto:|#|pm-image:)/i.test(url); markdown.renderer.rules.link_open = (tokens, index, options, _env, self) => { const token = tokens[index]; const href = String(token.attrGet("href") ?? ""); if (!/^(https?:|mailto:|#)/i.test(href)) token.attrs = []; else { token.attrSet("target", "_blank"); token.attrSet("rel", "noopener noreferrer"); } return self.renderToken(tokens, index, options); }; // Image sources come only from the validated workspace image store, never from Markdown URLs. markdown.renderer.rules.image = (tokens, index) => { const token = tokens[index]; const id = String(token.attrGet("src") ?? "").replace(/^pm-image:/, ""); const name = markdown.utils.escapeHtml(token.content || "Image"); try { imagePath(id); return `${name}`; } catch { return `Image: ${name} (use Add image to import)`; } }; export function renderMarkdown(source: string): string { return markdown.render(source); }