45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
/**
|
|
* marked-directive extension: :cmd[command]{label=Display text} inline syntax.
|
|
*
|
|
* Renders a clickable span that dispatches the command to the journal stream
|
|
* when clicked. The CommandLinkManager component handles the actual dispatch.
|
|
*
|
|
* Usage in markdown:
|
|
* :cmd[roll 1d20+5]{label=Roll initiative}
|
|
* :cmd[spark dungeon room-type]{label=Random room}
|
|
* :cmd[stat set strength=18]{label=Set strength}
|
|
*
|
|
* If no label is provided, the command text itself is shown.
|
|
*/
|
|
|
|
export const cmdDirective = {
|
|
level: "inline" as const,
|
|
marker: ":",
|
|
renderer(token: any) {
|
|
// Only handle :cmd[...], not other :directives
|
|
if (token.meta.name !== "cmd") return false;
|
|
|
|
const command = (token.text || "").trim();
|
|
if (!command) return "";
|
|
|
|
const label = (token.attrs?.label as string) || command;
|
|
|
|
return `<span class="cmd-link" data-cmd="${escapeAttr(command)}" role="button" tabindex="0">${escapeHtml(label)}</span>`;
|
|
},
|
|
};
|
|
|
|
function escapeHtml(s: string): string {
|
|
return s
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">");
|
|
}
|
|
|
|
function escapeAttr(s: string): string {
|
|
return s
|
|
.replace(/&/g, "&")
|
|
.replace(/"/g, """)
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">");
|
|
}
|