fix(yaml/tag): prevent yaml role=tag from being stripped & generalize

data-config
This commit is contained in:
2026-09-03 10:39:05 +08:00
parent 5fc2bc5262
commit 80e2b5b209
3 changed files with 14 additions and 16 deletions
+6 -16
View File
@@ -38,21 +38,13 @@ export default function markedCodeBlockYamlTag(): MarkedExtension {
delete (rest as Record<string, unknown>).body;
}
// A structured `data-config` object is serialized to JSON and
// passed as a single attribute (e.g. md-deck layers/templates).
let configAttr = "";
if ("data-config" in rest) {
const rawConfig = rest["data-config"];
delete (rest as Record<string, unknown>)["data-config"];
if (rawConfig !== undefined && rawConfig !== null) {
const json = JSON.stringify(rawConfig).replace(/"/g, "&quot;");
configAttr = ` data-config="${json}"`;
}
}
// `data-*` props may hold structured YAML values, so they are
// serialized to JSON strings (e.g. md-deck layers/templates).
const propsStr = Object.entries(rest)
.map(([key, value]) => {
const strValue = String(value);
const strValue = key.startsWith("data-")
? JSON.stringify(value)
: String(value);
if (strValue.includes(" ") || strValue.includes('"')) {
return `${key}="${strValue.replace(/"/g, "&quot;")}"`;
}
@@ -65,15 +57,13 @@ export default function markedCodeBlockYamlTag(): MarkedExtension {
raw: match[0],
tagName,
props: propsStr,
config: configAttr,
content,
};
}
},
renderer(token: any) {
const propsAttr = token.props ? ` ${token.props}` : "";
const configAttr = token.config || "";
return `<${token.tagName}${propsAttr}${configAttr}>${token.content || ""}</${token.tagName}>\n`;
return `<${token.tagName}${propsAttr}>${token.content || ""}</${token.tagName}>\n`;
},
},
],