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 ``; }, }; 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) { return `Link`; }, }; marker.use({ extensions: [extension, inlineExtension, linkExtension], }); function ParseMarkdown(source) { return marker.parse(source || ""); } export { ParseMarkdown, GetWidget };