All checks were successful
Deploy to Netlify / Deploy to Netlify (push) Successful in 1m39s
33 lines
988 B
TypeScript
33 lines
988 B
TypeScript
export function extractLatexImages(html: string) {
|
|
const imgRegex =
|
|
/<img[^>]+src="(https:\/\/latex\.codecogs\.com\/gif\.latex\?(=?.*?))"[^>]*>/g;
|
|
let parts = [];
|
|
let latexMatches: string[] = [];
|
|
let lastIndex = 0;
|
|
|
|
html.replace(imgRegex, (match, _, latex, index) => {
|
|
parts.push(html.slice(lastIndex, index)); // Add HTML before image
|
|
latexMatches.push(decodeURIComponent(latex)); // Extract and decode LaTeX
|
|
lastIndex = index + match.length;
|
|
return "";
|
|
});
|
|
|
|
parts.push(html.slice(lastIndex)); // Add remaining HTML after last image
|
|
|
|
return { parts, latexMatches };
|
|
}
|
|
|
|
export function renderLatex(html: string) {
|
|
const { parts, latexMatches } = extractLatexImages(html);
|
|
const outputHtml = parts
|
|
.map((part, i) => {
|
|
if (!latexMatches[i]) {
|
|
return part;
|
|
}
|
|
return `${part}$$${latexMatches[i]}$$`;
|
|
})
|
|
.join("");
|
|
// Remove all "\," from string
|
|
const regex = /\\,/g;
|
|
return outputHtml.replace(regex, " ");
|
|
}
|