Files
dragonroll/frontend/app/services/Marker.js
Aran Roig 1ec29121e1
Some checks failed
Test / test (push) Failing after 2s
Build and Deploy Nuxt / build (push) Successful in 1m51s
Deploy deepseek 2
2026-10-19 19:04:28 +02:00

165 lines
4.1 KiB
JavaScript

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, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
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 `<div class="vue-component-display" data-component="${escapeAttr(token.name)}" data-content="${escapeAttr(token.text)}"></div>`;
},
};
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 `<span class="vue-component-inline" data-component="${escapeAttr(token.name)}" data-content="${escapeAttr(token.text)}"></span>`;
},
};
// `![[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 `<span class="vue-embed" data-href="${escapeAttr(token.link)}"></span>`;
},
};
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 `<span class="vue-link" data-href="${escapeAttr(token.link)}" role="link" tabindex="0">${escapeAttr(label)}</span>`;
},
};
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 };