@newtonschool/tex-html-parser
v0.1.0
Published
TypeScript parser/transformer for TeX-to-HTML rendering.
Readme
tex-html-parser
TypeScript parser/transformer that converts TeX into sanitized HTML.
Install
npm install tex-html-parserAPI
import { renderTexStatement } from 'tex-html-parser'
const html = renderTexStatement(tex)- Input:
tex: string - Output:
string(sanitized HTML) renderTexStatementis side-effect free by default.
To opt into MathJax rendering in browsers:
const html = renderTexStatement(tex, { typeset: true })To scope MathJax work to a specific container:
const html = renderTexStatement(tex, {
typeset: true,
typesetTarget: containerElement,
})MathJax loader security notes:
- The package injects a pinned MathJax CDN URL (
jsDelivr) at runtime. - Script loading uses
crossorigin="anonymous"with timeout fallback. - You can set a strict CSP to restrict script sources, or provide
window.MathJaxbefore callingrenderTexStatementto avoid dynamic script injection.
React Usage (dangerouslySetInnerHTML)
import { renderTexStatement } from 'tex-html-parser'
function Statement({ tex }: { tex: string }) {
const html = renderTexStatement(tex)
return <div dangerouslySetInnerHTML={{ __html: html }} />
}Supported TeX Subset
- Paragraph splitting by blank line
- Inline/display math delimiters:
$...$,$$...$$(preserved) - Styles:
\bf,\textbf,\it,\textit,\t,\tt,\texttt,\emph,\underline,\sout,\textsc - Sizes:
\tiny,\scriptsize,\small,\normalsize,\large,\Large,\LARGE,\huge,\Huge - Environments:
itemize,enumerate,lstlisting,center,tabular - Table helpers:
\hline,\cline,\multicolumn,\multirow - Links:
\url,\href \epigraph- Typography replacements for TeX quote/dash tokens
Not Supported (Dropped)
defs.tomlcustom command expansion\includegraphics\def \htmlPixelsInCm- React preview component export
- CSS export
- warnings/meta return object
Unsupported or malformed input is rendered best-effort with escaped fallback text.
Development
npm test
npm run build
npm run test:packagePublish checks:
npm run prepublishOnly