Skip to content

Commit

Permalink
Add support for CHTML
Browse files Browse the repository at this point in the history
Closes GH-43.
Closes GH-42.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
TANIGUCHI Masaya committed Apr 24, 2020
1 parent 53c01b4 commit c5ab344
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 215 deletions.
31 changes: 0 additions & 31 deletions packages/rehype-mathjax/browser-renderer.js

This file was deleted.

36 changes: 36 additions & 0 deletions packages/rehype-mathjax/chtml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const visit = require('unist-util-visit')
const toText = require('hast-util-to-text')
const ChtmlRenderer = require('./renderer/chtml')

module.exports = rehypeMathJaxChtml

function rehypeMathJaxChtml(options = {}) {
return transformMath

function transformMath(tree) {
const renderer = new ChtmlRenderer(options)

let found = false

visit(tree, 'element', onelement)

if (found) {
tree.children.push(renderer.styleSheet)
}

function onelement(element) {
const classes = element.properties.className || []
const inline = classes.includes('math-inline')
const display = classes.includes('math-display')

if (!inline && !display) {
return
}

found = true
element.children = [renderer.render(toText(element), {display: display})]

return visit.SKIP
}
}
}
35 changes: 1 addition & 34 deletions packages/rehype-mathjax/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
const visit = require('unist-util-visit')
const toText = require('hast-util-to-text')
const renderer = require('./renderer')

module.exports = rehypeMathJax

function rehypeMathJax() {
return transformMath
}

function transformMath(tree) {
let found = false

visit(tree, 'element', onelement)

if (found) {
tree.children.push(renderer.styleSheet())
}

function onelement(element) {
const classes = element.properties.className || []
const inline = classes.includes('math-inline')
const display = classes.includes('math-display')

if (!inline && !display) {
return
}

found = true
element.children = [renderer.render(toText(element), {display: display})]

return visit.SKIP
}
}
module.exports = require('./svg')
2 changes: 1 addition & 1 deletion packages/rehype-mathjax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "https://opencollective.com/unified"
},
"browser": {
"./renderer": "./browser-renderer"
"./renderer/node": "./renderer/browser"
},
"author": "TANIGUCHI Masaya <taniguchi.masaya@yandex.com> (https://docs.casa)",
"files": [
Expand Down
32 changes: 20 additions & 12 deletions packages/rehype-mathjax/readme.md

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions packages/rehype-mathjax/renderer.js

This file was deleted.

32 changes: 32 additions & 0 deletions packages/rehype-mathjax/renderer/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fromDom = require('hast-util-from-dom')
const {browserAdaptor} = require('mathjax-full/js/adaptors/browserAdaptor')
const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html')

class AbstractRenderer {
constructor() {
this.adaptor = browserAdaptor()
RegisterHTMLHandler(this.adaptor)
}

get styleSheet() {
return {
type: 'element',
tagName: 'style',
properties: {},
children: [
{
type: 'text',
value: this.adaptor.textContent(
this.OutputJax.styleSheet(this.mathDocument)
)
}
]
}
}

render(latex, options) {
return fromDom(this.mathDocument.convert(latex, options))
}
}

module.exports = AbstractRenderer
19 changes: 19 additions & 0 deletions packages/rehype-mathjax/renderer/chtml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {mathjax} = require('mathjax-full/js/mathjax')
const {TeX} = require('mathjax-full/js/input/tex')
const {CHTML} = require('mathjax-full/js/output/chtml')
const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages')
const AbstractRenderer = require('./node')

class ChtmlRenderer extends AbstractRenderer {
constructor(options) {
super()
this.InputJax = new TeX({packages: AllPackages})
this.OutputJax = new CHTML(options)
this.mathDocument = mathjax.document('', {
InputJax: this.InputJax,
OutputJax: this.OutputJax
})
}
}

module.exports = ChtmlRenderer
33 changes: 33 additions & 0 deletions packages/rehype-mathjax/renderer/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fromDom = require('hast-util-from-dom')
const {JSDOM} = require('jsdom')
const {jsdomAdaptor} = require('mathjax-full/js/adaptors/jsdomAdaptor')
const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html')

class AbstractRenderer {
constructor() {
this.adaptor = jsdomAdaptor(JSDOM)
RegisterHTMLHandler(this.adaptor)
}

get styleSheet() {
return {
type: 'element',
tagName: 'style',
properties: {},
children: [
{
type: 'text',
value: this.adaptor.textContent(
this.OutputJax.styleSheet(this.mathDocument)
)
}
]
}
}

render(latex, options) {
return fromDom(this.mathDocument.convert(latex, options))
}
}

module.exports = AbstractRenderer
19 changes: 19 additions & 0 deletions packages/rehype-mathjax/renderer/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const {mathjax} = require('mathjax-full/js/mathjax')
const {TeX} = require('mathjax-full/js/input/tex')
const {SVG} = require('mathjax-full/js/output/svg')
const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages')
const AbstractRenderer = require('./node')

class SvgRenderer extends AbstractRenderer {
constructor(options) {
super()
this.InputJax = new TeX({packages: AllPackages})
this.OutputJax = new SVG(options)
this.mathDocument = mathjax.document('', {
InputJax: this.InputJax,
OutputJax: this.OutputJax
})
}
}

module.exports = SvgRenderer
36 changes: 36 additions & 0 deletions packages/rehype-mathjax/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const visit = require('unist-util-visit')
const toText = require('hast-util-to-text')
const SvgRenderer = require('./renderer/svg')

module.exports = rehypeMathJaxSVG

function rehypeMathJaxSVG(options = {}) {
return transformMath

function transformMath(tree) {
const renderer = new SvgRenderer(options)

let found = false

visit(tree, 'element', onelement)

if (found) {
tree.children.push(renderer.styleSheet)
}

function onelement(element) {
const classes = element.properties.className || []
const inline = classes.includes('math-inline')
const display = classes.includes('math-display')

if (!inline && !display) {
return
}

found = true
element.children = [renderer.render(toText(element), {display: display})]

return visit.SKIP
}
}
}
Loading

0 comments on commit c5ab344

Please sign in to comment.