feat(markdown): support yaml role=tag info string

Accept yaml role=tag alongside legacy yaml/tag so the block
body gets yaml syntax highlighting, matching the CLI role=
attribute convention. Copy button now emits the role=tag form.
This commit is contained in:
2026-09-03 10:10:06 +08:00
parent f1ddc8fb8e
commit d75df5280f
6 changed files with 83 additions and 22 deletions
+29 -6
View File
@@ -28,10 +28,7 @@ describe("code-block-yaml-tag", () => {
" size: 63x88",
" layers:",
" - prop: title",
" x1: 1",
" y1: 1",
" x2: 5",
" y2: 1",
" pos: 1,1-5,1",
" font: 12",
"```",
].join("\n"),
@@ -47,12 +44,38 @@ describe("code-block-yaml-tag", () => {
expect(config.layers).toHaveLength(1);
expect(config.layers[0]).toMatchObject({
prop: "title",
x1: 1,
y1: 1,
pos: "1,1-5,1",
font: 12,
});
});
test("supports yaml role=tag info string (highlight-friendly)", () => {
const html = render(
[
"```yaml role=tag",
"tag: md-deck",
"body: ./cards.csv",
"data-config:",
" grid: 5x5",
" layers:",
" - template: |",
" **{{name}}**",
" pos: 1,1-5,8",
"```",
].join("\n"),
);
expect(html).toContain("<md-deck data-config=");
expect(html).toContain("./cards.csv</md-deck>");
const m = html.match(/data-config="([^"]*)"/);
const config = JSON.parse((m![1] || "").replace(/&quot;/g, '"'));
expect(config.layers[0].template).toBe("**{{name}}**\n");
});
test("does not swallow plain yaml code blocks", () => {
const token = ext.tokenizer("```yaml\nsize: 54x86\n```");
expect(token).toBeUndefined();
});
test("handles missing tag and body", () => {
const html = render("```yaml/tag\nclass: foo\n```");
expect(html).toContain("<tag-unknown class=\"foo\"></tag-unknown>");
+7 -2
View File
@@ -8,10 +8,15 @@ export default function markedCodeBlockYamlTag(): MarkedExtension {
name: "code-block-yaml-tag",
level: "block",
start(src: string) {
return src.match(/^```yaml\/tag\s*\n/m)?.index;
return (
src.match(/^```yaml\/tag\s*\n/m)?.index ??
src.match(/^```yaml\s+role=tag(?:\s|\n)/m)?.index
);
},
tokenizer(src: string) {
const rule = /^```yaml\/tag\s*\n([\s\S]*?)\n```/;
// Both `yaml/tag` (legacy) and `yaml role=tag` (highlight-friendly)
// info strings identify a yaml-defined tag block.
const rule = /^```yaml(?:\/tag|\s+role=tag[^\n]*)\n([\s\S]*?)\n```/;
const match = rule.exec(src);
if (match) {
const yamlContent = match[1]?.trim() || "";