Skip to content

Commit

Permalink
Add micromark-build
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 8, 2021
1 parent cc3b6d4 commit 6f1cb4b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
/lib/
/index.js
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
coverage/
*.html
*.json
*.md
File renamed without changes.
File renamed without changes.
50 changes: 29 additions & 21 deletions lib/math-flow.js → dev/lib/math-flow.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import assert from 'assert'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol/codes.js'
import {constants} from 'micromark-util-symbol/constants.js'
import {types} from 'micromark-util-symbol/types.js'

export const mathFlow = {
tokenize: tokenizeMathFenced,
Expand All @@ -9,25 +14,23 @@ function tokenizeMathFenced(effects, ok, nok) {
const self = this
const tail = self.events[self.events.length - 1]
const initialSize =
tail && tail[1].type === 'linePrefix'
tail && tail[1].type === types.linePrefix
? tail[2].sliceSerialize(tail[1], true).length
: 0
let sizeOpen = 0

return start

function start(code) {
/* istanbul ignore if - handled by mm */
if (code !== 36) throw new Error('expected `$`')

assert(code === codes.dollarSign, 'expected `$`')
effects.enter('mathFlow')
effects.enter('mathFlowFence')
effects.enter('mathFlowFenceSequence')
return sequenceOpen(code)
}

function sequenceOpen(code) {
if (code === 36) {
if (code === codes.dollarSign) {
effects.consume(code)
sizeOpen++
return sequenceOpen
Expand All @@ -36,27 +39,27 @@ function tokenizeMathFenced(effects, ok, nok) {
effects.exit('mathFlowFenceSequence')
return sizeOpen < 2
? nok(code)
: factorySpace(effects, metaOpen, 'whitespace')(code)
: factorySpace(effects, metaOpen, types.whitespace)(code)
}

function metaOpen(code) {
if (code === null || code === -5 || code === -4 || code === -3) {
if (code === codes.eof || markdownLineEnding(code)) {
return openAfter(code)
}

effects.enter('mathFlowFenceMeta')
effects.enter('chunkString', {contentType: 'string'})
effects.enter(types.chunkString, {contentType: constants.contentTypeString})
return meta(code)
}

function meta(code) {
if (code === null || code === -5 || code === -4 || code === -3) {
effects.exit('chunkString')
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit(types.chunkString)
effects.exit('mathFlowFenceMeta')
return openAfter(code)
}

if (code === 36) return nok(code)
if (code === codes.dollarSign) return nok(code)
effects.consume(code)
return meta
}
Expand All @@ -67,19 +70,19 @@ function tokenizeMathFenced(effects, ok, nok) {
}

function content(code) {
if (code === null) {
if (code === codes.eof) {
return after(code)
}

if (code === -5 || code === -4 || code === -3) {
effects.enter('lineEnding')
if (markdownLineEnding(code)) {
effects.enter(types.lineEnding)
effects.consume(code)
effects.exit('lineEnding')
effects.exit(types.lineEnding)
return effects.attempt(
{tokenize: tokenizeClosingFence, partial: true},
after,
initialSize
? factorySpace(effects, content, 'linePrefix', initialSize + 1)
? factorySpace(effects, content, types.linePrefix, initialSize + 1)
: content
)
}
Expand All @@ -89,7 +92,7 @@ function tokenizeMathFenced(effects, ok, nok) {
}

function contentContinue(code) {
if (code === null || code === -5 || code === -4 || code === -3) {
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit('mathFlowValue')
return content(code)
}
Expand All @@ -106,7 +109,12 @@ function tokenizeMathFenced(effects, ok, nok) {
function tokenizeClosingFence(effects, ok, nok) {
let size = 0

return factorySpace(effects, closingPrefixAfter, 'linePrefix', 4)
return factorySpace(
effects,
closingPrefixAfter,
types.linePrefix,
constants.tabSize
)

function closingPrefixAfter(code) {
effects.enter('mathFlowFence')
Expand All @@ -115,19 +123,19 @@ function tokenizeMathFenced(effects, ok, nok) {
}

function closingSequence(code) {
if (code === 36) {
if (code === codes.dollarSign) {
effects.consume(code)
size++
return closingSequence
}

if (size < sizeOpen) return nok(code)
effects.exit('mathFlowFenceSequence')
return factorySpace(effects, closingSequenceEnd, 'whitespace')(code)
return factorySpace(effects, closingSequenceEnd, types.whitespace)(code)
}

function closingSequenceEnd(code) {
if (code === null || code === -5 || code === -4 || code === -3) {
if (code === codes.eof || markdownLineEnding(code)) {
effects.exit('mathFlowFence')
return ok(code)
}
Expand Down
59 changes: 29 additions & 30 deletions lib/math-text.js → dev/lib/math-text.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import assert from 'assert'
import {markdownLineEnding} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol/codes.js'
import {types} from 'micromark-util-symbol/types.js'

export const mathText = {
tokenize: tokenizeMathText,
resolve: resolveMathText,
Expand All @@ -12,9 +17,9 @@ function resolveMathText(events) {

// If we start and end with an EOL or a space.
if (
(events[headEnterIndex][1].type === 'lineEnding' ||
(events[headEnterIndex][1].type === types.lineEnding ||
events[headEnterIndex][1].type === 'space') &&
(events[tailExitIndex][1].type === 'lineEnding' ||
(events[tailExitIndex][1].type === types.lineEnding ||
events[tailExitIndex][1].type === 'space')
) {
index = headEnterIndex
Expand All @@ -38,12 +43,15 @@ function resolveMathText(events) {

while (++index <= tailExitIndex) {
if (enter === undefined) {
if (index !== tailExitIndex && events[index][1].type !== 'lineEnding') {
if (
index !== tailExitIndex &&
events[index][1].type !== types.lineEnding
) {
enter = index
}
} else if (
index === tailExitIndex ||
events[index][1].type === 'lineEnding'
events[index][1].type === types.lineEnding
) {
events[enter][1].type = 'mathTextData'

Expand All @@ -64,8 +72,8 @@ function resolveMathText(events) {
function previous(code) {
// If there is a previous code, there will always be a tail.
return (
code !== 36 ||
this.events[this.events.length - 1][1].type === 'characterEscape'
code !== codes.dollarSign ||
this.events[this.events.length - 1][1].type === types.characterEscape
)
}

Expand All @@ -78,21 +86,15 @@ function tokenizeMathText(effects, ok, nok) {
return start

function start(code) {
/* istanbul ignore if - handled by mm */
if (code !== 36) throw new Error('expected `$`')

/* istanbul ignore if - handled by mm */
if (!previous.call(self, self.previous)) {
throw new Error('expected correct previous')
}

assert(code === codes.dollarSign, 'expected `$`')
assert(previous.call(self, self.previous), 'expected correct previous')
effects.enter('mathText')
effects.enter('mathTextSequence')
return openingSequence(code)
}

function openingSequence(code) {
if (code === 36) {
if (code === codes.dollarSign) {
effects.consume(code)
sizeOpen++
return openingSequence
Expand All @@ -103,31 +105,30 @@ function tokenizeMathText(effects, ok, nok) {
}

function gap(code) {
// EOF.
if (code === null) {
if (code === codes.eof) {
return nok(code)
}

// Closing fence?
// Could also be data.
if (code === 36) {
if (code === codes.dollarSign) {
token = effects.enter('mathTextSequence')
size = 0
return closingSequence(code)
}

// Tabs don’t work, and virtual spaces don’t make sense.
if (code === 32) {
if (code === codes.space) {
effects.enter('space')
effects.consume(code)
effects.exit('space')
return gap
}

if (code === -5 || code === -4 || code === -3) {
effects.enter('lineEnding')
if (markdownLineEnding(code)) {
effects.enter(types.lineEnding)
effects.consume(code)
effects.exit('lineEnding')
effects.exit(types.lineEnding)
return gap
}

Expand All @@ -136,15 +137,13 @@ function tokenizeMathText(effects, ok, nok) {
return data(code)
}

// In code.
// In math.
function data(code) {
if (
code === null ||
code === 32 ||
code === 36 ||
code === -5 ||
code === -4 ||
code === -3
code === codes.eof ||
code === codes.space ||
code === codes.dollarSign ||
markdownLineEnding(code)
) {
effects.exit('mathTextData')
return gap(code)
Expand All @@ -157,7 +156,7 @@ function tokenizeMathText(effects, ok, nok) {
// Closing fence.
function closingSequence(code) {
// More.
if (code === 36) {
if (code === codes.dollarSign) {
effects.consume(code)
size++
return closingSequence
Expand Down
8 changes: 8 additions & 0 deletions dev/lib/syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {codes} from 'micromark-util-symbol/codes.js'
import {mathFlow} from './math-flow.js'
import {mathText} from './math-text.js'

export const math = {
flow: {[codes.dollarSign]: mathFlow},
text: {[codes.dollarSign]: mathText}
}
7 changes: 0 additions & 7 deletions lib/syntax.js

This file was deleted.

21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,36 @@
"type": "module",
"main": "index.js",
"files": [
"dev/",
"lib/",
"index.js"
],
"exports": {
"development": "./dev/index.js",
"default": "./index.js"
},
"dependencies": {
"katex": "^0.13.0",
"micromark": "^3.0.0-alpha.2",
"micromark-factory-space": "^1.0.0-alpha.2"
"micromark-factory-space": "^1.0.0-alpha.2",
"micromark-util-character": "^1.0.0-alpha.2",
"micromark-util-symbol": "^1.0.0-alpha.2"
},
"devDependencies": {
"c8": "^7.0.0",
"micromark": "^3.0.0-alpha.2",
"micromark-build": "^1.0.0-alpha.2",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "micromark-build",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'tape'
import katex from 'katex'
import {micromark} from 'micromark'
import {math as syntax, mathHtml as html} from '../index.js'
import {math as syntax, mathHtml as html} from '../dev/index.js'

test('markdown -> html (micromark)', (t) => {
t.equal(
Expand Down

0 comments on commit 6f1cb4b

Please sign in to comment.