Skip to content

Commit

Permalink
Add improved internal types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 9, 2021
1 parent 0b77136 commit 6899b07
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/rehype-mathjax/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ const rehypeMathJaxBrowser =
}
)
)

export default rehypeMathJaxBrowser
2 changes: 1 addition & 1 deletion packages/rehype-mathjax/lib/create-adaptor.browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {browserAdaptor} from 'mathjax-full/js/adaptors/browserAdaptor.js'

export {browserAdaptor}
export {browserAdaptor as createAdaptor}
8 changes: 7 additions & 1 deletion packages/rehype-mathjax/lib/create-input.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList
* @typedef {import('mathjax-full/js/input/tex.js').TeX<HTMLElement, Text, Document>} 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))
Expand Down
9 changes: 7 additions & 2 deletions packages/rehype-mathjax/lib/create-output-chtml.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList
* @typedef {import('mathjax-full/js/output/chtml.js').CHTML<HTMLElement, Text, Document>} 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)
}
9 changes: 7 additions & 2 deletions packages/rehype-mathjax/lib/create-output-svg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* @typedef {import('mathjax-full/js/util/Options.js').OptionList} OptionList
* @typedef {import('mathjax-full/js/output/svg.js').SVG<HTMLElement, Text, Document>} 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)
}
14 changes: 8 additions & 6 deletions packages/rehype-mathjax/lib/create-renderer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('mathjax-full/js/core/OutputJax').OutputJax<any, any, any>} OutputJax
* @typedef {import('mathjax-full/js/core/OutputJax').OutputJax<HTMLElement, Text, Document>} OutputJax
* @typedef {import('mathjax-full/js/core/MathDocument.js').MathDocument<HTMLElement, Text, Document>} 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'
Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions packages/rehype-mathjax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6899b07

Please sign in to comment.