Files
ttrpg-tools/src/markdown/cmd.ts
T
hypercross d690d5922e refactor: simplify command dispatch and update cmd-link
- Simplify `dispatchCommand` by removing role-based logic and ensuring
  commands are prefixed with a slash.
- Change `:cmd[]` directive output from a `span` to an `a` tag.
- Update `.cmd-link` styles to improve link appearance and prevent
  text selection.
2026-07-12 14:18:01 +08:00

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 `<a class="cmd-link" data-cmd="${escapeAttr(command)}">${escapeHtml(label)}</a>`;
},
};
function escapeHtml(s: string): string {
return s
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}
function escapeAttr(s: string): string {
return s
.replace(/&/g, "&amp;")
.replace(/"/g, "&quot;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}