-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
TANIGUCHI Masaya
committed
Apr 24, 2020
1 parent
53c01b4
commit c5ab344
Showing
12 changed files
with
328 additions
and
215 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.