fix(markdown): accept tilde fences when scanning

Fenced block scanning was backtick-only in FENCED_BLOCK_RE (used by scanDoc) and in the yaml role=tag render extension, while stripFencedBlocks and the validator already accepted tildes. Match both marker styles and require the closing fence to reuse the opening marker.
This commit is contained in:
hyper
2026-09-12 23:45:01 +08:00
parent 264038e27c
commit 08bc97bec5
5 changed files with 51 additions and 7 deletions
+10
View File
@@ -70,6 +70,16 @@ describe("code-block-yaml-tag", () => {
expect(token).toBeUndefined();
});
test("supports tilde fences", () => {
const html = render("~~~yaml role=tag\ntag: md-deck\nbody: ./cards.csv\n~~~");
expect(html).toContain("<md-deck>./cards.csv</md-deck>");
});
test("does not swallow plain tilde yaml code blocks", () => {
const token = ext.tokenizer("~~~yaml\nsize: 54x86\n~~~");
expect(token).toBeUndefined();
});
test("does not swallow yaml blocks without role=tag", () => {
const token = ext.tokenizer("```yaml tag=md-deck\nsize: 54x86\n```");
expect(token).toBeUndefined();
+5 -5
View File
@@ -13,7 +13,7 @@ import { parseBlockAttrs } from "./block-attrs.js";
* `role=tag` on the info string marks the block (and lets the content
* scanner keep it intact). `tag=` / `id=` / any other `key=value` fence
* attributes override the same keys in the YAML body, so simple cases
* stay on one line.
* stay on one line. Both backtick and tilde fences are accepted.
*/
export default function markedCodeBlockYamlTag(): MarkedExtension {
return {
@@ -22,13 +22,13 @@ export default function markedCodeBlockYamlTag(): MarkedExtension {
name: "code-block-yaml-tag",
level: "block",
start(src: string) {
return src.match(/^```yaml\s+role=tag(?:\s|\n)/m)?.index;
return src.match(/^(?:`{3,}|~{3,})yaml\s+role=tag(?:\s|\n)/m)?.index;
},
tokenizer(src: string) {
const rule = /^```yaml\s+role=tag([^\n]*)\n([\s\S]*?)\n```/;
const rule = /^([`~])\1{2,}yaml\s+role=tag([^\n]*)\n([\s\S]*?)\n\1{3,}/;
const match = rule.exec(src);
if (match) {
const yamlContent = match[2]?.trim() || "";
const yamlContent = match[3]?.trim() || "";
let yamlProps: Record<string, unknown> = {};
try {
yamlProps =
@@ -39,7 +39,7 @@ export default function markedCodeBlockYamlTag(): MarkedExtension {
}
// Fence attributes override YAML body values.
const attrs = parseBlockAttrs(match[1] || "");
const attrs = parseBlockAttrs(match[2] || "");
const fenceProps: Record<string, unknown> = {
...(attrs.id ? { id: attrs.id } : {}),
...attrs.extra,