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