import remarkMath from 'remark-math';
export default function math({ katexOptions } = {}) {
    let katex;
    return {
        remark: (p) => p.use(remarkMath),
        viewerEffect({ $el }) {
            const renderMath = async (selector, displayMode) => {
                const els = $el.querySelectorAll(selector);
                if (els.length === 0)
                    return;
                if (!katex) {
                    katex = await import('katex');
                }
                els.forEach((el) => {
                    katex.render(el.innerText, el, {
                        ...katexOptions,
                        throwOnError: false,
                        displayMode,
                    });
                });
            };
            renderMath('.math.math-inline', false);
            renderMath('.math.math-display', true);
        },
    };
}
