ATXRenderer = function () {
const mapping = {
h1: "h2",
h2: "h3"
};
function getTagName(token) {
return token.markup == "-" || token.markup == "="
? "h2"
: mapping[token.tag] ?? token.tag;
}
function open(tokens, idx) {
const token = tokens[idx];
const tag = getTagName(token);
return `<${tag}>`;
}
function close(tokens, idx) {
const token = tokens[idx];
const tag = getTagName(token);
return `</${tag}>`;
}
return function (md) {
md.core.ruler.push("wrap_first_word_in_h1", function (state) {
state.tokens.forEach((token, i) => {
if (token.type === "heading_open" && token.tag === "h1") {
const inlineToken = state.tokens[i + 1];
if (inlineToken.type === "inline") {
const textToken = inlineToken.children.find(
(child) => child.type === "text"
);
if (textToken) {
const words = textToken.content.split(" ");
if (words.length > 0) {
const firstWord = words.shift();
const firstWordToken = new state.Token("html_inline", "", 0);
firstWordToken.content = `<span>${firstWord}</span>`;
textToken.content = textToken.content.substr(firstWord.length);
inlineToken.children.unshift(firstWordToken);
}
}
}
}
});
});
md.renderer.rules.heading_open = open;
md.renderer.rules.heading_close = close;
};
}