<commit_before>const mjAPI = require("mathjax-node")

module.exports = {handler}

mjAPI.config({MathJax: {}})
mjAPI.start()

function handler(req, res) {
    mjAPI.typeset({
        math: req.query.latex,
        format: "TeX", // "inline-TeX", "MathML"
        mml: false,
        svg: true,
    }, function (data) {
        if (data.errors) {
            res.type('svg')
            res.send(`<svg xmlns="http://www.w3.org/2000/svg"  
     xmlns:xlink="http://www.w3.org/1999/xlink">
    <text x="10" y="15" fill="red">${data.errors.join('<br>')}</text>
</svg>`)
        } else {
            res.type('svg')
            res.send(data.svg)
        }
    })
}<commit_msg>Cache toolbar latex<commit_after>const mjAPI = require("mathjax-node")
const latexCommands = require('./latexCommands')

module.exports = {handler}

mjAPI.config({MathJax: {}})
mjAPI.start()

const cache = {}
latexCommands.map(o => o.label ? o.label.replace(/X/g, '\\square') : o.action)
    .forEach(latex => {
        mjAPI.typeset({
            math: latex,
            format: "TeX", // "inline-TeX", "MathML"
            mml: false,
            svg: true,
        }, function (data) {
            cache[latex] = data.svg
        })
    })

function handler(req, res) {
    res.type('svg')

    if(req.query.latex in cache) {
        console.log('cache')
        res.send(cache[req.query.latex])
    } else {
        mjAPI.typeset({
            math: req.query.latex,
            format: "TeX", // "inline-TeX", "MathML"
            mml: false,
            svg: true,
        }, function (data) {
            if (data.errors) {
                res.send(`<svg xmlns="http://www.w3.org/2000/svg"  
     xmlns:xlink="http://www.w3.org/1999/xlink">
    <text x="10" y="15" fill="red">${data.errors.join('<br>')}</text>
</svg>`)
            } else {
                res.send(data.svg)
            }
        })
    }
}
{'fraction_non_alphanumeric': 0.1886592474827769, 'fraction_numerical': 0.01483836777954425, 'mean_word_length': 1.9226006191950464, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4282682', 'n_tokens_mistral': 694, 'n_tokens_neox': 654, 'n_words': 107}