dragoman 0.6.1

Server for scholarly metadata in commonmeta format stored in a SQLite database.
/**
 * The maths renderer, fetched when a formula first needs it.
 *
 * KaTeX is a third of the bundle — 265kB of JavaScript, 23kB of CSS and
 * nineteen fonts — and most of what this server shows has no maths in it at
 * all: of fifty titles sampled from the store, none carried a formula. Loading
 * it with the app made every reader of a list of blog posts download a TeX
 * engine to render no TeX.
 *
 * So it is imported on demand. Until it arrives a formula is shown as it was
 * written, `$E = mc^2$` and all, which is what the source says and is no worse
 * than the blank the alternative would leave; the import is requested at the
 * same moment, and the state here is read during a component's render, so the
 * formula redraws itself when the engine lands.
 */
let renderer = $state(null)
let loading = false

/** The engine, or `null` while it is still on its way. */
export function mathRenderer() {
  return renderer
}

/** Ask for the engine, once. Returns nothing: the state above is the answer. */
export function needMath() {
  if (renderer || loading) return
  loading = true
  Promise.all([import('katex'), import('katex/dist/katex.min.css')])
    .then(([module]) => {
      renderer = module.default ?? module
    })
    .catch(() => {
      // No maths rendering, then: the formulas stay as they were written, and
      // a later formula asks again.
      loading = false
    })
}