diff --git a/.gitignore b/.gitignore index fdefc8c..b657905 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store *.log -.nyc_output/ coverage/ node_modules/ +/lib/ +/index.js yarn.lock diff --git a/.prettierignore b/.prettierignore index 2ac20d2..619aa6b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,3 @@ coverage/ *.html -*.json *.md diff --git a/index.js b/dev/index.js similarity index 100% rename from index.js rename to dev/index.js diff --git a/lib/html.js b/dev/lib/html.js similarity index 100% rename from lib/html.js rename to dev/lib/html.js diff --git a/lib/math-flow.js b/dev/lib/math-flow.js similarity index 63% rename from lib/math-flow.js rename to dev/lib/math-flow.js index 6a3b640..ca6c1a1 100644 --- a/lib/math-flow.js +++ b/dev/lib/math-flow.js @@ -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, @@ -9,7 +14,7 @@ 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 @@ -17,9 +22,7 @@ function tokenizeMathFenced(effects, ok, nok) { 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') @@ -27,7 +30,7 @@ function tokenizeMathFenced(effects, ok, nok) { } function sequenceOpen(code) { - if (code === 36) { + if (code === codes.dollarSign) { effects.consume(code) sizeOpen++ return sequenceOpen @@ -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 } @@ -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 ) } @@ -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) } @@ -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') @@ -115,7 +123,7 @@ function tokenizeMathFenced(effects, ok, nok) { } function closingSequence(code) { - if (code === 36) { + if (code === codes.dollarSign) { effects.consume(code) size++ return closingSequence @@ -123,11 +131,11 @@ function tokenizeMathFenced(effects, ok, nok) { 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) } diff --git a/lib/math-text.js b/dev/lib/math-text.js similarity index 72% rename from lib/math-text.js rename to dev/lib/math-text.js index 3e9215a..24e5d58 100644 --- a/lib/math-text.js +++ b/dev/lib/math-text.js @@ -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, @@ -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 @@ -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' @@ -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 ) } @@ -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 @@ -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 } @@ -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) @@ -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 diff --git a/dev/lib/syntax.js b/dev/lib/syntax.js new file mode 100644 index 0000000..1edfa2f --- /dev/null +++ b/dev/lib/syntax.js @@ -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} +} diff --git a/lib/syntax.js b/lib/syntax.js deleted file mode 100644 index 98c4138..0000000 --- a/lib/syntax.js +++ /dev/null @@ -1,7 +0,0 @@ -import {mathFlow} from './math-flow.js' -import {mathText} from './math-text.js' - -export const math = { - flow: {36: mathFlow}, - text: {36: mathText} -} diff --git a/package.json b/package.json index e519c0e..d60d7b2 100644 --- a/package.json +++ b/package.json @@ -27,16 +27,24 @@ "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", @@ -44,16 +52,11 @@ "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, diff --git a/test/index.js b/test/index.js index 9852261..61e51ec 100644 --- a/test/index.js +++ b/test/index.js @@ -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(