Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 8, 2021
1 parent 07bd6ff commit 31ce5cf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 73 deletions.
96 changes: 41 additions & 55 deletions lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,52 @@ import katex from 'katex'
export function mathHtml(options) {
return {
enter: {
mathFlow: onentermathflow,
mathFlowFenceMeta: onentermathflowmeta,
mathText: onentermathtext
mathFlow() {
this.lineEndingIfNeeded()
this.tag('<div class="math math-display">')
},
mathFlowFenceMeta() {
this.buffer()
},
mathText() {
// Double?
this.tag('<span class="math math-inline">')
this.buffer()
}
},
exit: {
mathFlow: onexitmathflow,
mathFlowFence: onexitmathfencedfence,
mathFlowFenceMeta: onexitmathflowmeta,
mathFlowValue: onexitmathdata,
mathText: onexitmathtext,
mathTextData: onexitmathdata
mathFlow() {
const value = this.resume()
this.tag(math(value.replace(/(?:\r?\n|\r)$/, ''), true))
this.tag('</div>')
this.setData('mathFlowOpen')
this.setData('slurpOneLineEnding')
},
mathFlowFence() {
// After the first fence.
if (!this.getData('mathFlowOpen')) {
this.setData('mathFlowOpen', true)
this.setData('slurpOneLineEnding', true)
this.buffer()
}
},
mathFlowFenceMeta() {
this.resume()
},
mathFlowValue(token) {
this.raw(this.sliceSerialize(token))
},
mathText() {
const value = this.resume()
this.tag(math(value, false))
this.tag('</span>')
},
mathTextData(token) {
this.raw(this.sliceSerialize(token))
}
}
}

function onentermathflow() {
this.lineEndingIfNeeded()
this.tag('<div class="math math-display">')
}

function onentermathflowmeta() {
this.buffer()
}

function onexitmathflowmeta() {
this.resume()
}

function onexitmathfencedfence() {
// After the first fence.
if (!this.getData('mathFlowOpen')) {
this.setData('mathFlowOpen', true)
this.setData('slurpOneLineEnding', true)
this.buffer()
}
}

function onexitmathflow() {
var value = this.resume()
this.tag(math(value.replace(/(?:\r?\n|\r)$/, ''), true))
this.tag('</div>')
this.setData('mathFlowOpen')
this.setData('slurpOneLineEnding')
}

function onentermathtext() {
// Double?
this.tag('<span class="math math-inline">')
this.buffer()
}

function onexitmathtext() {
var value = this.resume()
this.tag(math(value, false))
this.tag('</span>')
}

function onexitmathdata(token) {
this.raw(this.sliceSerialize(token))
}

function math(value, displayMode) {
return katex.renderToString(
value,
Expand Down
6 changes: 3 additions & 3 deletions lib/math-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export const mathFlow = {
}

function tokenizeMathFenced(effects, ok, nok) {
var self = this
const self = this
const tail = self.events[self.events.length - 1]
const initialSize =
tail && tail[1].type === 'linePrefix'
? tail[2].sliceSerialize(tail[1], true).length
: 0
var sizeOpen = 0
let sizeOpen = 0

return start

Expand Down Expand Up @@ -104,7 +104,7 @@ function tokenizeMathFenced(effects, ok, nok) {
}

function tokenizeClosingFence(effects, ok, nok) {
var size = 0
let size = 0

return factorySpace(effects, closingPrefixAfter, 'linePrefix', 4)

Expand Down
16 changes: 8 additions & 8 deletions lib/math-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export const mathText = {
}

function resolveMathText(events) {
var tailExitIndex = events.length - 4
var headEnterIndex = 3
var index
var enter
let tailExitIndex = events.length - 4
let headEnterIndex = 3
let index
let enter

// If we start and end with an EOL or a space.
if (
Expand Down Expand Up @@ -70,10 +70,10 @@ function previous(code) {
}

function tokenizeMathText(effects, ok, nok) {
var self = this
var sizeOpen = 0
var size
var token
const self = this
let sizeOpen = 0
let size
let token

return start

Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/no-this-assignment": "off"
}
},
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import katex from 'katex'
import {micromark} from 'micromark'
import {math as syntax, mathHtml as html} from '../index.js'

test('markdown -> html (micromark)', function (t) {
test('markdown -> html (micromark)', (t) => {
t.equal(
micromark('a \\$b$', {extensions: [syntax], htmlExtensions: [html()]}),
'<p>a $b$</p>',
'should support an escaped dollar sign which would otherwise open math'
)

t.throws(
function () {
() => {
micromark('a $b\\$', {extensions: [syntax], htmlExtensions: [html()]})
},
/KaTeX parse error: Unexpected character: '\\' at position 2/,
Expand All @@ -27,7 +27,7 @@ test('markdown -> html (micromark)', function (t) {
)

t.throws(
function () {
() => {
micromark('a $$ $ $$', {extensions: [syntax], htmlExtensions: [html()]})
},
/KaTeX parse error: Can't use function '\$' in math mode at position 1/,
Expand Down Expand Up @@ -191,7 +191,7 @@ test('markdown -> html (micromark)', function (t) {
)

t.throws(
function () {
() => {
micromark('$$\na\n$$ b', {
extensions: [syntax],
htmlExtensions: [html()]
Expand Down

0 comments on commit 31ce5cf

Please sign in to comment.