export const COLLAPSED_LINES = 28;
const PRE = /<pre(?:\s[^>]*)?>(?:(?!<pre[\s>])[\s\S])*?<\/pre>/g;
const MERMAID = /^<pre\s[^>]*\bclass="mermaid"/;
const lineCount = (pre: string): number =>
pre
.replace(/<[^>]*>/g, "")
.replace(/\n$/, "")
.split("\n").length;
export const collapseCodeblocks = (html: string): string =>
html.replace(PRE, (pre) => {
if (MERMAID.test(pre)) return pre;
const lines = lineCount(pre);
return lines < COLLAPSED_LINES
? pre
: `<div data-codeblock="${lines}"><div data-codeblock-toolbar></div>${pre}</div>`;
});