Skip to content

Commit

Permalink
Rewrite code
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 8, 2019
1 parent e89144b commit 7088469
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 196 deletions.
110 changes: 50 additions & 60 deletions packages/rehype-katex/index.js
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
}
}
}
2 changes: 1 addition & 1 deletion packages/rehype-katex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
],
"main": "index.js",
"dependencies": {
"hast-util-to-text": "^1.0.0",
"katex": "^0.10.0",
"rehype-parse": "^6.0.0",
"unified": "^8.0.0",
"unist-util-position": "^3.0.0",
"unist-util-visit": "^2.0.0"
},
"peerDependencies": {
Expand Down
27 changes: 9 additions & 18 deletions packages/rehype-katex/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,21 @@ Transform `<span class=inlineMath>` and `<div class=math>` with [KaTeX][].

#### `options`

##### `options.throwOnError`

Throw if a KaTeX parse error occurs (`boolean`, default: `false`).
See [KaTeX options][katex-options].

##### `options.errorColor`

Color to render invalid LaTeX as (`string`, default: `#cc0000`).
See [KaTeX options][katex-options].

##### `options.macros`
##### `options.inlineMathDoubleDisplay`

A collection of custom macros (`Object`, default: `{}`).
See [KaTeX options][katex-options].
*EXPERIMENTAL*.
If an element has `inlineMathDouble` class, set `displayMode` of KaTeX `true`
(default: `false`).

##### `options.strict`
##### `options.throwOnError`

Throw if a KaTeX parse error occurs (`boolean`, default: `false`).
See [KaTeX options][katex-options].

##### `options.inlineMathDoubleDisplay`
##### `options.<*>`

*EXPERIMENTAL*.
If an element has `inlineMathDouble` class, set `displayMode` of KaTeX `true`
(default: `false`).
All other options, except for `displayMode`, are passed to
[KaTeX][katex-options].

## Security

Expand Down
8 changes: 4 additions & 4 deletions packages/rehype-katex/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ test('rehype-katex', function(t) {
{
message:
'KaTeX parse error: Undefined control sequence: \\alpa at position 1: \\̲a̲l̲p̲a̲',
name: '2:4',
name: '2:4-2:41',
reason:
'KaTeX parse error: Undefined control sequence: \\alpa at position 1: \\̲a̲l̲p̲a̲',
line: 2,
column: 4,
location: {
start: {line: 2, column: 4, offset: 16},
end: {line: null, column: null}
end: {line: 2, column: 41, offset: 53}
},
source: null,
ruleId: null,
source: 'rehype-katex',
ruleId: 'parseerror',
fatal: false
}
],
Expand Down
75 changes: 33 additions & 42 deletions packages/remark-html-katex/index.js
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
}
}
1 change: 0 additions & 1 deletion packages/remark-html-katex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"katex": "^0.10.0",
"rehype-parse": "^6.0.0",
"unified": "^8.0.0",
"unist-util-position": "^3.0.0",
"unist-util-visit": "^2.0.0"
},
"peerDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions packages/remark-html-katex/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,18 @@ test('remark-html-katex', function(t) {
{
message:
'KaTeX parse error: Undefined control sequence: \\alpa at position 1: \\̲a̲l̲p̲a̲',
name: '2:1',
name: '2:1-2:8',
reason:
'KaTeX parse error: Undefined control sequence: \\alpa at position 1: \\̲a̲l̲p̲a̲',
line: 2,
column: 1,
location: {
start: {line: 2, column: 1, offset: 6},
end: {line: null, column: null}
end: {line: 2, column: 8, offset: 13},
indent: []
},
source: null,
ruleId: null,
source: 'remark-html-katex',
ruleId: 'parseerror',
fatal: false
}
],
Expand Down
Loading

0 comments on commit 7088469

Please sign in to comment.