fix(yaml/tag): prevent yaml role=tag from being stripped & generalize
data-config
This commit is contained in:
@@ -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, """);
|
||||
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, """)}"`;
|
||||
}
|
||||
@@ -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`;
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user