RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
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 `<img data-pm-image="${id}" alt="${name}">`;
|
||||
} catch {
|
||||
return `<span class="pm-muted">Image: ${name} (use Add image to import)</span>`;
|
||||
}
|
||||
};
|
||||
|
||||
export function renderMarkdown(source: string): string {
|
||||
return markdown.render(source);
|
||||
}
|
||||
Reference in New Issue
Block a user