remarkjs / remark-math

Compare 5313419 ... +78 ... 63a5f76

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 9 of 61 files from the diff.
Other files ignored by Codecov
.gitignore has changed.
package.json has changed.
screenshot.png was deleted.
lerna.json was deleted.
readme.md has changed.
.travis.yml was deleted.

@@ -0,0 +1,117 @@
Loading
1 +
/**
2 +
 * @typedef {import('hast').Root} Root
3 +
 * @typedef {import('hast').Element} Element
4 +
 *
5 +
 * @typedef {[string, string]} MathNotation
6 +
 *   Markers to use for math.
7 +
 *   See: <http://docs.mathjax.org/en/latest/options/input/tex.html#the-configuration-block>
8 +
 *
9 +
 * @typedef OutputSvgOptions
10 +
 *   <http://docs.mathjax.org/en/latest/options/output/svg.html#the-configuration-block>
11 +
 * @property {number} [scale]
12 +
 * @property {number} [minScale]
13 +
 * @property {boolean} [mtextInheritFont]
14 +
 * @property {boolean} [merrorInheritFont]
15 +
 * @property {boolean} [mathmlSpacing]
16 +
 * @property {Record<string, boolean>} [skipAttributes]
17 +
 * @property {number} [exFactor]
18 +
 * @property {'left'|'center'|'right'} [displayAlign]
19 +
 * @property {string} [displayIndent]
20 +
 * @property {'local'|'global'} [fontCache]
21 +
 * @property {string|null} [localID]
22 +
 * @property {boolean} [internalSpeechTitles]
23 +
 * @property {number} [titleID]
24 +
 *
25 +
 * @typedef OutputCHtmlOptions
26 +
 *   <http://docs.mathjax.org/en/latest/options/output/chtml.html#the-configuration-block>
27 +
 * @property {number} [scale]
28 +
 * @property {number} [minScale]
29 +
 * @property {boolean} [matchFontHeight]
30 +
 * @property {boolean} [mtextInheritFont]
31 +
 * @property {boolean} [merrorInheritFont]
32 +
 * @property {boolean} [mathmlSpacing]
33 +
 * @property {Record<string, boolean>} [skipAttributes]
34 +
 * @property {number} [exFactor]
35 +
 * @property {'left'|'center'|'right'} [displayAlign]
36 +
 * @property {string} [displayIndent]
37 +
 * @property {string} fontURL
38 +
 * @property {boolean} [adaptiveCSS]
39 +
 *
40 +
 * @typedef InputTexOptions
41 +
 *   <http://docs.mathjax.org/en/latest/options/input/tex.html#the-configuration-block>
42 +
 * @property {string[]} [packages]
43 +
 * @property {MathNotation[]} [inlineMath]
44 +
 * @property {MathNotation[]} [displayMath]
45 +
 * @property {boolean} [processEscapes]
46 +
 * @property {boolean} [processEnvironments]
47 +
 * @property {boolean} [processRefs]
48 +
 * @property {RegExp} [digits]
49 +
 * @property {'none'|'ams'|'all'} [tags]
50 +
 * @property {'left'|'right'} [tagSide]
51 +
 * @property {string} [tagIndent]
52 +
 * @property {boolean} [useLabelIds]
53 +
 * @property {string} [multlineWidth]
54 +
 * @property {number} [maxMacros]
55 +
 * @property {number} [maxBuffer]
56 +
 * @property {string} [baseURL]
57 +
 * @property {(jax: any, error: any) => string} [formatError]
58 +
 *
59 +
 * @typedef Options
60 +
 *   Configuration.
61 +
 * @property {InputTexOptions} [tex]
62 +
 *   Configuration for the input TeX.
63 +
 * @property {OutputCHtmlOptions} [chtml]
64 +
 *   Configuration for the output (when CHTML).
65 +
 * @property {OutputSvgOptions} [svg]
66 +
 *   Configuration for the output (when SVG).
67 +
 *
68 +
 * @typedef Renderer
69 +
 * @property {(node: Element, options: {display: boolean}) => void} render
70 +
 * @property {() => Element} [styleSheet]
71 +
 *
72 +
 * @callback CreateRenderer
73 +
 * @param {Options} options
74 +
 * @returns {Renderer}
75 +
 */
76 +
77 +
import {visit, SKIP} from 'unist-util-visit'
78 +
79 +
/**
80 +
 * @param {CreateRenderer} createRenderer
81 +
 */
82 +
export function createPlugin(createRenderer) {
83 +
  /** @type {import('unified').Plugin<[Options?]|void[], Root>} */
84 +
  return (options = {}) =>
85 +
    (tree) => {
86 +
      const renderer = createRenderer(options)
87 +
      let found = false
88 +
      /** @type {Root|Element} */
89 +
      let context = tree
90 +
91 +
      visit(tree, 'element', (node) => {
92 +
        const classes =
93 +
          node.properties && Array.isArray(node.properties.className)
94 +
            ? node.properties.className
95 +
            : []
96 +
        const inline = classes.includes('math-inline')
97 +
        const display = classes.includes('math-display')
98 +
99 +
        if (node.tagName === 'head') {
100 +
          context = node
101 +
        }
102 +
103 +
        if (!inline && !display) {
104 +
          return
105 +
        }
106 +
107 +
        found = true
108 +
        renderer.render(node, {display})
109 +
110 +
        return SKIP
111 +
      })
112 +
113 +
      if (found && renderer.styleSheet) {
114 +
        context.children.push(renderer.styleSheet())
115 +
      }
116 +
    }
117 +
}

@@ -1,10 +1,36 @@
Loading
1 -
const inlinePlugin = require('./inline')
2 -
const blockPlugin = require('./block')
1 +
/**
2 +
 * @typedef {import('mdast').Root} Root
3 +
 * @typedef {import('mdast-util-math').ToOptions} Options
4 +
 *
5 +
 * @typedef {import('mdast-util-math')} DoNotTouchAsThisImportIncludesMathInTree
6 +
 */
3 7
4 -
module.exports = math
8 +
import {math} from 'micromark-extension-math'
9 +
import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math'
5 10
6 -
function math(options) {
7 -
  var settings = options || {}
8 -
  blockPlugin.call(this, settings)
9 -
  inlinePlugin.call(this, settings)
11 +
/**
12 +
 * Plugin to support math.
13 +
 *
14 +
 * @type {import('unified').Plugin<[Options?] | void[], Root, Root>}
15 +
 */
16 +
export default function remarkMath(options = {}) {
17 +
  const data = this.data()
18 +
19 +
  add('micromarkExtensions', math(options))
20 +
  add('fromMarkdownExtensions', mathFromMarkdown())
21 +
  add('toMarkdownExtensions', mathToMarkdown(options))
22 +
23 +
  /**
24 +
   * @param {string} field
25 +
   * @param {unknown} value
26 +
   */
27 +
  function add(field, value) {
28 +
    const list = /** @type {unknown[]} */ (
29 +
      // Other extensions
30 +
      /* c8 ignore next 2 */
31 +
      data[field] ? data[field] : (data[field] = [])
32 +
    )
33 +
34 +
    list.push(value)
35 +
  }
10 36
}

@@ -0,0 +1,64 @@
Loading
1 +
/**
2 +
 * @typedef {import('hast').Element} Element
3 +
 * @typedef {import('mathjax-full/js/core/OutputJax').OutputJax<HTMLElement, Text, Document>} OutputJax
4 +
 * @typedef {import('mathjax-full/js/core/MathDocument.js').MathDocument<HTMLElement, Text, Document>} MathDocument
5 +
 * @typedef {import('mathjax-full/js/input/tex.js').TeX<HTMLElement, Text, Document>} TeX_
6 +
 * @typedef {import('./create-plugin.js').Options} Options
7 +
 * @typedef {import('./create-plugin.js').Renderer} Renderer
8 +
 */
9 +
10 +
import {mathjax} from 'mathjax-full/js/mathjax.js'
11 +
import {RegisterHTMLHandler} from 'mathjax-full/js/handlers/html.js'
12 +
import {TeX} from 'mathjax-full/js/input/tex.js'
13 +
import {AllPackages} from 'mathjax-full/js/input/tex/AllPackages.js'
14 +
import {fromDom} from 'hast-util-from-dom'
15 +
import {toText} from 'hast-util-to-text'
16 +
import {createAdaptor} from './create-adaptor.js'
17 +
18 +
const adaptor = createAdaptor()
19 +
20 +
// To do next major: Keep resultant HTML handler from `register(adaptor)` to
21 +
// allow registering the `AssistiveMmlHandler` as in this demo:
22 +
// <https://github.com/mathjax/MathJax-demos-node/tree/master/direct>
23 +
//
24 +
// To do next major: If registering `AssistiveMmlHandler` is supported through
25 +
// configuration, move HTML handler registration to beginning of transformer and
26 +
// unregister at the end of transformer with
27 +
// `mathjax.handlers.unregister(handler)`.
28 +
// That is to prevent memory leak in `mathjax.handlers` whenever a new instance
29 +
// of the plugin is used.
30 +
/* eslint-disable-next-line new-cap */
31 +
RegisterHTMLHandler(adaptor)
32 +
33 +
/**
34 +
 * @param {Options} options
35 +
 * @param {OutputJax} output
36 +
 * @returns {Renderer}
37 +
 */
38 +
export function createRenderer(options, output) {
39 +
  const input = new TeX(Object.assign({packages: AllPackages}, options.tex))
40 +
  /** @type {MathDocument} */
41 +
  const doc = mathjax.document('', {InputJax: input, OutputJax: output})
42 +
43 +
  return {
44 +
    render(node, options) {
45 +
      const domNode = fromDom(
46 +
        // @ts-expect-error: assume mathml nodes can be handled by
47 +
        // `hast-util-from-dom`.
48 +
        doc.convert(toText(node, {whitespace: 'pre'}), options)
49 +
      )
50 +
      // @ts-expect-error: `fromDom` returns an element for a given element.
51 +
      node.children = [domNode]
52 +
    },
53 +
    styleSheet() {
54 +
      const value = adaptor.textContent(output.styleSheet(doc))
55 +
56 +
      return {
57 +
        type: 'element',
58 +
        tagName: 'style',
59 +
        properties: {},
60 +
        children: [{type: 'text', value}]
61 +
      }
62 +
    }
63 +
  }
64 +
}

@@ -0,0 +1,6 @@
Loading
1 +
import {JSDOM} from 'jsdom'
2 +
import {jsdomAdaptor} from 'mathjax-full/js/adaptors/jsdomAdaptor.js'
3 +
4 +
export function createAdaptor() {
5 +
  return jsdomAdaptor(JSDOM)
6 +
}

@@ -1,10 +1,13 @@
Loading
1 -
const createInput = require('./lib/input')
2 -
const createOutput = require('./lib/output-svg')
3 -
const createRenderer = require('./lib/renderer')
4 -
const createPlugin = require('./lib/core')
1 +
/**
2 +
 * @typedef {import('./lib/create-plugin.js').Options} Options
3 +
 */
5 4
6 -
module.exports = createPlugin('rehypeMathJaxSvg', renderSvg)
5 +
import {SVG} from 'mathjax-full/js/output/svg.js'
6 +
import {createRenderer} from './lib/create-renderer.js'
7 +
import {createPlugin} from './lib/create-plugin.js'
7 8
8 -
function renderSvg(options) {
9 -
  return createRenderer(createInput(), createOutput(options))
10 -
}
9 +
const rehypeMathJaxSvg = createPlugin((options) =>
10 +
  createRenderer(options, new SVG(options.svg))
11 +
)
12 +
13 +
export default rehypeMathJaxSvg

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Learn more Showing 3 files with coverage changes found.

New file packages/rehype-mathjax/lib/create-renderer.js
New
Loading file...
New file packages/rehype-mathjax/lib/create-adaptor.js
New
Loading file...
New file packages/rehype-mathjax/lib/create-plugin.js
New
Loading file...

80 Commits

Hiding 2 contexual commits
Hiding 1 contexual commits
Hiding 1 contexual commits
-7
-7
Hiding 2 contexual commits
+2
+2
+1
+1
Hiding 1 contexual commits
+2
+2
Hiding 1 contexual commits
Hiding 3 contexual commits Hiding 18 contexual commits
-7 Files
+177
+177
Hiding 1 contexual commits
Hiding 3 contexual commits
Hiding 1 contexual commits
Hiding 1 contexual commits
+5
+5
Hiding 1 contexual commits
Hiding 1 contexual commits
-3 Files
-157
-157
Hiding 3 contexual commits Hiding 2 contexual commits
Hiding 1 contexual commits
Files Coverage
packages 100.00%
Project Totals (9 files) 100.00%
Loading