function mdtex() {
const replacements = [
{
regex: /\$\$([^\$]*)\$\$/,
regexFn: id => () => id,
tag: tex.block
},
{
regex: /(?:^|[^\$])\$(?!\$)([^\$]+)\$(?!\$)/,
regexFn: id => match => `${match[0] === '$' ? '' : match[0]}${id}`,
tag: tex
}
];
const toReplace = [];
const id = DOM.uid("mdtex").id;
arguments[0] = arguments[0].map((arg, i) =>
replacements.reduce((arg, { regex, _ }, index) => {
const matches = matchAll(arguments[0].raw[i], new RegExp(regex, "g"));
let match = matches.nextRaw();
while (match) {
toReplace.push({ match: match[1], replacementsIndex: index });
arg = arg.replace(
regex,
replacements[index].regexFn(`${id}-${toReplace.length - 1}-${index}`)
);
match = matches.nextRaw();
}
return arg;
}, arg)
);
const node = inlinemd.apply(null, arguments);
node.innerHTML = toReplace.reduce(
(html, { match, replacementsIndex }, index) =>
html.replace(
`${id}-${index}-${replacementsIndex}`,
_ => replacements[replacementsIndex].tag`${match}`.outerHTML
),
node.innerHTML
);
return node;
}