From 6899b0703a0491b3f883fb431fb1fd1c47bfa4e2 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 9 Aug 2021 13:39:28 +0200 Subject: [PATCH] Add improved internal types --- packages/rehype-mathjax/browser.js | 1 + .../rehype-mathjax/lib/create-adaptor.browser.js | 2 +- packages/rehype-mathjax/lib/create-input.js | 8 +++++++- packages/rehype-mathjax/lib/create-output-chtml.js | 9 +++++++-- packages/rehype-mathjax/lib/create-output-svg.js | 9 +++++++-- packages/rehype-mathjax/lib/create-renderer.js | 14 ++++++++------ packages/rehype-mathjax/package.json | 1 + 7 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/rehype-mathjax/browser.js b/packages/rehype-mathjax/browser.js index b88a8ba..a1bd647 100644 --- a/packages/rehype-mathjax/browser.js +++ b/packages/rehype-mathjax/browser.js @@ -36,4 +36,5 @@ const rehypeMathJaxBrowser = } ) ) + export default rehypeMathJaxBrowser diff --git a/packages/rehype-mathjax/lib/create-adaptor.browser.js b/packages/rehype-mathjax/lib/create-adaptor.browser.js index 8096f1f..cd44dfc 100644 --- a/packages/rehype-mathjax/lib/create-adaptor.browser.js +++ b/packages/rehype-mathjax/lib/create-adaptor.browser.js @@ -1,3 +1,3 @@ import {browserAdaptor} from 'mathjax-full/js/adaptors/browserAdaptor.js' -export {browserAdaptor} +export {browserAdaptor as createAdaptor} diff --git a/packages/rehype-mathjax/lib/create-input.js b/packages/rehype-mathjax/lib/create-input.js index 78e6f5d..30b3d00 100644 --- a/packages/rehype-mathjax/lib/create-input.js +++ b/packages/rehype-mathjax/lib/create-input.js @@ -1,8 +1,14 @@ +/** + * @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList + * @typedef {import('mathjax-full/js/input/tex.js').TeX} TeX_ + */ + import {TeX} from 'mathjax-full/js/input/tex.js' import {AllPackages} from 'mathjax-full/js/input/tex/AllPackages.js' /** - * @param {unknown} options + * @param {OptionList} options + * @returns {TeX_} */ export function createInput(options) { return new TeX(Object.assign({packages: AllPackages}, options)) diff --git a/packages/rehype-mathjax/lib/create-output-chtml.js b/packages/rehype-mathjax/lib/create-output-chtml.js index afc725b..cb6fdd9 100644 --- a/packages/rehype-mathjax/lib/create-output-chtml.js +++ b/packages/rehype-mathjax/lib/create-output-chtml.js @@ -1,9 +1,14 @@ +/** + * @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList + * @typedef {import('mathjax-full/js/output/chtml.js').CHTML} CHTML_ + */ + import {CHTML} from 'mathjax-full/js/output/chtml.js' /** - * @param {unknown} options + * @param {OptionList} options + * @returns {CHTML_} */ export function createOutputChtml(options) { - // @ts-expect-error: assume options work (mathjax types are not exported) return new CHTML(options) } diff --git a/packages/rehype-mathjax/lib/create-output-svg.js b/packages/rehype-mathjax/lib/create-output-svg.js index 7fcc71c..c2763f4 100644 --- a/packages/rehype-mathjax/lib/create-output-svg.js +++ b/packages/rehype-mathjax/lib/create-output-svg.js @@ -1,9 +1,14 @@ +/** + * @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList + * @typedef {import('mathjax-full/js/output/svg.js').SVG} SVG_ + */ + import {SVG} from 'mathjax-full/js/output/svg.js' /** - * @param {unknown} options + * @param {OptionList} options + * @returns {SVG_} */ export function createOutputSvg(options) { - // @ts-expect-error: assume options work (mathjax types are not exported) return new SVG(options) } diff --git a/packages/rehype-mathjax/lib/create-renderer.js b/packages/rehype-mathjax/lib/create-renderer.js index 50e19ea..b953444 100644 --- a/packages/rehype-mathjax/lib/create-renderer.js +++ b/packages/rehype-mathjax/lib/create-renderer.js @@ -1,9 +1,8 @@ /** * @typedef {import('hast').Element} Element - * @typedef {import('mathjax-full/js/core/OutputJax').OutputJax} OutputJax + * @typedef {import('mathjax-full/js/core/OutputJax').OutputJax} OutputJax + * @typedef {import('mathjax-full/js/core/MathDocument.js').MathDocument} MathDocument * @typedef {import('./create-plugin.js').CreateRenderer} CreateRenderer - * - * For some reason MathJax types can’t be imported. */ import {mathjax} from 'mathjax-full/js/mathjax.js' @@ -34,13 +33,16 @@ RegisterHTMLHandler(adaptor) */ export function createRenderer(inputOptions, output) { const input = createInput(inputOptions) + /** @type {MathDocument} */ const doc = mathjax.document('', {InputJax: input, OutputJax: output}) return { render(node, options) { - const domNode = doc.convert(toText(node), options) - // @ts-expect-error: assume no `doctypes` - node.children = [fromDom(domNode)] + // @ts-expect-error: assume mathml nodes can be handled by + // `hast-util-from-dom`. + const domNode = fromDom(doc.convert(toText(node), options)) + // @ts-expect-error: `fromDom` returns an element for a given element. + node.children = [domNode] }, styleSheet() { const value = adaptor.textContent(output.styleSheet(doc)) diff --git a/packages/rehype-mathjax/package.json b/packages/rehype-mathjax/package.json index 19d5378..2e1a654 100644 --- a/packages/rehype-mathjax/package.json +++ b/packages/rehype-mathjax/package.json @@ -50,6 +50,7 @@ "dependencies": { "@types/hast": "^2.0.0", "@types/mathjax": "^0.0.36", + "@types/web": "^0.0.15", "hast-util-from-dom": "^4.0.0", "hast-util-to-text": "^3.0.0", "jsdom": "^16.0.0",