-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Showing
13 changed files
with
237 additions
and
196 deletions.
There are no files selected for viewing
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,77 +1,67 @@ | ||
const visit = require('unist-util-visit') | ||
const katex = require('katex') | ||
const katex = require('katex').renderToString | ||
const unified = require('unified') | ||
const parse = require('rehype-parse') | ||
const position = require('unist-util-position') | ||
const toText = require('hast-util-to-text') | ||
|
||
module.exports = plugin | ||
module.exports = rehypeKatex | ||
|
||
function parseMathHtml(html) { | ||
return unified() | ||
.use(parse, {fragment: true, position: false}) | ||
.parse(html) | ||
} | ||
const assign = Object.assign | ||
|
||
function hasClass(element, className) { | ||
return ( | ||
element.properties.className && | ||
element.properties.className.includes(className) | ||
) | ||
} | ||
const parseHtml = unified().use(parse, {fragment: true, position: false}) | ||
|
||
function isTag(element, tag) { | ||
return element.tagName === tag | ||
} | ||
const source = 'rehype-katex' | ||
|
||
function plugin(opts) { | ||
if (opts == null) opts = {} | ||
if (opts.throwOnError == null) opts.throwOnError = false | ||
if (opts.errorColor == null) opts.errorColor = '#cc0000' | ||
if (opts.macros == null) opts.macros = {} | ||
return transform | ||
function rehypeKatex(options) { | ||
const opts = options || {} | ||
const double = opts.inlineMathDoubleDisplay || false | ||
const throwOnError = opts.throwOnError || false | ||
|
||
function transform(node, file) { | ||
visit(node, 'element', function(element) { | ||
const isInlineMath = | ||
isTag(element, 'span') && hasClass(element, 'inlineMath') | ||
const isMath = | ||
(opts.inlineMathDoubleDisplay && | ||
hasClass(element, 'inlineMathDouble')) || | ||
(isTag(element, 'div') && hasClass(element, 'math')) | ||
return transformMath | ||
|
||
if (isInlineMath || isMath) { | ||
let renderedValue | ||
try { | ||
renderedValue = katex.renderToString(element.children[0].value, { | ||
displayMode: isMath, | ||
macros: opts.macros, | ||
strict: opts.strict | ||
}) | ||
} catch (error) { | ||
if (opts.throwOnError) { | ||
throw error | ||
} else { | ||
file.message(error.message, position.start(element)) | ||
function transformMath(tree, file) { | ||
visit(tree, 'element', onelement) | ||
|
||
function onelement(element) { | ||
const tagName = element.tagName | ||
const classes = element.properties.className || [] | ||
const inline = tagName === 'span' && classes.includes('inlineMath') | ||
const displayMode = | ||
(double && | ||
tagName === 'span' && | ||
classes.includes('inlineMathDouble')) || | ||
(tagName === 'div' && classes.includes('math')) | ||
|
||
renderedValue = katex.renderToString(element.children[0].value, { | ||
displayMode: isMath, | ||
macros: opts.macros, | ||
throwOnError: false, | ||
errorColor: opts.errorColor, | ||
strict: 'ignore' | ||
}) | ||
} | ||
} | ||
if (!inline && !displayMode) { | ||
return | ||
} | ||
|
||
const value = toText(element) | ||
|
||
let result | ||
|
||
const inlineMathAst = parseMathHtml(renderedValue).children[0] | ||
try { | ||
result = katex( | ||
value, | ||
assign({}, options, {displayMode: displayMode, throwOnError: true}) | ||
) | ||
} catch (error) { | ||
const fn = throwOnError ? 'fail' : 'message' | ||
const origin = [source, error.name.toLowerCase()].join(':') | ||
|
||
Object.assign(element.properties, { | ||
className: element.properties.className | ||
}) | ||
file[fn](error.message, element.position, origin) | ||
|
||
element.children = [inlineMathAst] | ||
result = katex( | ||
value, | ||
assign({}, options, { | ||
displayMode: displayMode, | ||
throwOnError: false, | ||
strict: 'ignore' | ||
}) | ||
) | ||
} | ||
}) | ||
return node | ||
|
||
element.children = parseHtml.parse(result).children | ||
} | ||
} | ||
} |
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
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
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
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,60 +1,51 @@ | ||
const visit = require('unist-util-visit') | ||
const katex = require('katex') | ||
const katex = require('katex').renderToString | ||
const unified = require('unified') | ||
const parse = require('rehype-parse') | ||
const position = require('unist-util-position') | ||
|
||
module.exports = plugin | ||
module.exports = htmlKatex | ||
|
||
function parseMathHtml(html) { | ||
return unified() | ||
.use(parse, { | ||
fragment: true, | ||
position: false | ||
}) | ||
.parse(html) | ||
} | ||
const assign = Object.assign | ||
|
||
const parseHtml = unified().use(parse, {fragment: true, position: false}) | ||
|
||
const source = 'remark-html-katex' | ||
|
||
function htmlKatex(options) { | ||
const opts = options || {} | ||
const throwOnError = opts.throwOnError || false | ||
|
||
function plugin(opts) { | ||
if (opts == null) opts = {} | ||
if (opts.throwOnError == null) opts.throwOnError = false | ||
if (opts.errorColor == null) opts.errorColor = '#cc0000' | ||
if (opts.macros == null) opts.macros = {} | ||
return transform | ||
|
||
function transform(node, file) { | ||
function renderContent(element) { | ||
let renderedValue | ||
const isMath = element.type === 'math' | ||
function transform(tree, file) { | ||
visit(tree, ['inlineMath', 'math'], onmath) | ||
|
||
function onmath(node) { | ||
const displayMode = node.type === 'math' | ||
let result | ||
|
||
try { | ||
renderedValue = katex.renderToString(element.value, { | ||
macros: opts.macros, | ||
displayMode: isMath, | ||
strict: opts.strict | ||
}) | ||
result = katex( | ||
node.value, | ||
assign({}, options, {displayMode: displayMode, throwOnError: true}) | ||
) | ||
} catch (error) { | ||
if (opts.throwOnError) { | ||
throw error | ||
} else { | ||
file.message(error.message, position.start(element)) | ||
|
||
renderedValue = katex.renderToString(element.value, { | ||
displayMode: isMath, | ||
macros: opts.macros, | ||
const fn = throwOnError ? 'fail' : 'message' | ||
const origin = [source, error.name.toLowerCase()].join(':') | ||
|
||
file[fn](error.message, node.position, origin) | ||
|
||
result = katex( | ||
node.value, | ||
assign({}, options, { | ||
displayMode: displayMode, | ||
throwOnError: false, | ||
errorColor: opts.errorColor, | ||
strict: 'ignore' | ||
}) | ||
} | ||
) | ||
} | ||
|
||
const childAst = parseMathHtml(renderedValue).children[0] | ||
element.data.hChildren = [childAst] | ||
node.data.hChildren = parseHtml.parse(result).children | ||
} | ||
|
||
visit(node, 'inlineMath', renderContent) | ||
visit(node, 'math', renderContent) | ||
|
||
return node | ||
} | ||
} |
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
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
Oops, something went wrong.