import { Marked } from "marked"; const widget_map = { inline: { roll: () => import("~/components/viewer/widgets/inline/RollWidgetInline.vue"), }, display: { roll: () => import("~/components/viewer/widgets/display/RollWidgetDisplay.vue"), } }; const componentCache = { inline: {}, display: {} } // Retorna null si el widget no està registrat (evita cridar // defineAsyncComponent(undefined), que llençaria un TypeError i rebentaria // tot el muntatge de notes). const GetWidget = (type, name) => { const loader = widget_map[type] && widget_map[type][name]; if (!loader) { console.warn(`[Marker] Unknown widget "${name}" of type "${type}"; skipping.`); return null; } if (!componentCache[type][name]) { componentCache[type][name] = defineAsyncComponent(loader) } return componentCache[type][name] } // Escapa valors per inserir-los de forma segura dins d'un atribut HTML. const escapeAttr = (value) => String(value ?? "") .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); const marker = new Marked(); const extension = { name: "widget", level: "block", tokenizer(src) { const rule = /^@(\w+)\n([\s\S]+?)\n@end/; const match = rule.exec(src); if (!match) return; return { type: "widget", raw: match[0], name: match[1], text: match[2], }; }, renderer(token) { return `
`; }, }; const inlineExtension = { name: "widget_inline", level: "inline", start(src) { return src.indexOf("@"); }, tokenizer(src) { const rule = /^@(\w+)\s*\[([^\]]*)\]/; const match = rule.exec(src); if (!match) return; return { type: "widget_inline", raw: match[0], name: match[1], text: match[2], }; }, renderer(token) { return ``; }, }; // `![[Name]]` embeds the entry itself instead of linking it: the note mounts a card with the // character's sheet or the item's description where the token was written. const embedExtension = { name: "embed", level: "inline", start(src) { return src.indexOf("![["); }, tokenizer(src) { const rule = /^!\[\[([^\n]*)\]\]/; const match = rule.exec(src); if (!match) return; return { type: "embed", raw: match[0], link: match[1], }; }, renderer(token) { return ``; }, }; const linkExtension = { name: "link_to", level: "inline", start(src) { return src.indexOf("[["); }, tokenizer(src) { const rule = /^\[\[([^\n]*)\]\]/; const match = rule.exec(src); if (!match) return; return { type: "link_to", raw: match[0], link: match[1], }; }, renderer(token) { // The link shows the entry's name, not a generic "Link", so a note reads as written. The // note looks the target up by this label and turns the span into a preview link. const label = String(token.link ?? "").trim() || token.link; return `${escapeAttr(label)}`; }, }; marker.use({ // The embed comes before the link: at the `!` of `![[…]]` the embed tokenizer is offered first, // so the same label written with the bang does not fall through to the plain link. extensions: [extension, inlineExtension, embedExtension, linkExtension], }); function ParseMarkdown(source) { return marker.parse(source || ""); } // The widgets a note can use, as `{ inline: [...names], display: [...names] }`. The note editor's // `@` autocomplete reads this, so a widget added to `widget_map` is offered there without any other // change. const GetWidgetRegistry = () => { const registry = {}; for (const type of Object.keys(widget_map)) { registry[type] = Object.keys(widget_map[type]); } return registry; }; export { ParseMarkdown, GetWidget, GetWidgetRegistry };