Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 19, 2021
1 parent c429a20 commit 5c22a0a
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 213 deletions.
5 changes: 2 additions & 3 deletions .gitignore
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
.DS_Store
*.log
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
83 changes: 0 additions & 83 deletions from-markdown.js

This file was deleted.

158 changes: 156 additions & 2 deletions index.js
@@ -1,2 +1,156 @@
exports.fromMarkdown = require('./from-markdown.js')
exports.toMarkdown = require('./to-markdown.js')
import repeat from 'repeat-string'
import {longestStreak} from 'longest-streak'
import safe from 'mdast-util-to-markdown/lib/util/safe.js'

export const mathFromMarkdown = {
enter: {
mathFlow: enterMathFlow,
mathFlowFenceMeta: enterMathFlowMeta,
mathText: enterMathText
},
exit: {
mathFlow: exitMathFlow,
mathFlowFence: exitMathFlowFence,
mathFlowFenceMeta: exitMathFlowMeta,
mathFlowValue: exitMathData,
mathText: exitMathText,
mathTextData: exitMathData
}
}

export const mathToMarkdown = {
unsafe: [
{character: '\r', inConstruct: ['mathFlowMeta']},
{character: '\r', inConstruct: ['mathFlowMeta']},
{character: '$', inConstruct: ['mathFlowMeta', 'phrasing']},
{atBreak: true, character: '$', after: '\\$'}
],
handlers: {math, inlineMath}
}

inlineMath.peek = inlineMathPeek

function enterMathFlow(token) {
this.enter(
{
type: 'math',
meta: null,
value: '',
data: {
hName: 'div',
hProperties: {className: ['math', 'math-display']},
hChildren: [{type: 'text', value: ''}]
}
},
token
)
}

function enterMathFlowMeta() {
this.buffer()
}

function exitMathFlowMeta() {
var data = this.resume()
this.stack[this.stack.length - 1].meta = data
}

function exitMathFlowFence() {
// Exit if this is the closing fence.
if (this.getData('mathFlowInside')) return
this.buffer()
this.setData('mathFlowInside', true)
}

function exitMathFlow(token) {
var data = this.resume().replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
var node = this.exit(token)
node.value = data
node.data.hChildren[0].value = data
this.setData('mathFlowInside')
}

function enterMathText(token) {
this.enter(
{
type: 'inlineMath',
value: '',
data: {
hName: 'span',
hProperties: {className: ['math', 'math-inline']},
hChildren: [{type: 'text', value: ''}]
}
},
token
)
this.buffer()
}

function exitMathText(token) {
var data = this.resume()
var node = this.exit(token)
node.value = data
node.data.hChildren[0].value = data
}

function exitMathData(token) {
this.config.enter.data.call(this, token)
this.config.exit.data.call(this, token)
}

function math(node, _, context) {
var raw = node.value || ''
var fence = repeat('$', Math.max(longestStreak(raw, '$') + 1, 2))
var exit = context.enter('mathFlow')
var value = fence
var subexit

if (node.meta) {
subexit = context.enter('mathFlowMeta')
value += safe(context, node.meta, {before: '$', after: ' ', encode: ['$']})
subexit()
}

value += '\n'

if (raw) {
value += raw + '\n'
}

value += fence
exit()
return value
}

function inlineMath(node) {
var value = node.value || ''
var size = 1
var pad = ''
var sequence

// If there is a single dollar sign on its own in the math, use a fence of
// two.
// If there are two in a row, use one.
while (
new RegExp('(^|[^$])' + repeat('\\$', size) + '([^$]|$)').test(value)
) {
size++
}

// If this is not just spaces or eols (tabs don’t count), and either the first
// or last character are a space, eol, or dollar sign, then pad with spaces.
if (
/[^ \r\n]/.test(value) &&
(/[ \r\n$]/.test(value.charAt(0)) ||
/[ \r\n$]/.test(value.charAt(value.length - 1)))
) {
pad = ' '
}

sequence = repeat('$', size)
return sequence + pad + value + pad + sequence
}

function inlineMathPeek() {
return '$'
}
28 changes: 13 additions & 15 deletions package.json
Expand Up @@ -26,38 +26,33 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"from-markdown.js",
"index.js",
"to-markdown.js"
"index.js"
],
"dependencies": {
"longest-streak": "^2.0.0",
"longest-streak": "^3.0.0",
"mdast-util-to-markdown": "^0.6.0",
"repeat-string": "^1.0.0"
},
"devDependencies": {
"c8": "^7.0.0",
"mdast-util-from-markdown": "^0.8.0",
"micromark-extension-math": "^0.1.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
Expand All @@ -68,7 +63,10 @@
},
"xo": {
"prettier": true,
"esnext": false
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
13 changes: 8 additions & 5 deletions readme.md
Expand Up @@ -18,6 +18,9 @@ You probably shouldn’t use this package directly, but instead use

## Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.

[npm][]:

```sh
Expand Down Expand Up @@ -90,13 +93,13 @@ $$

## API

### `math.fromMarkdown`
This package exports the following identifier: `mathFromMarkdown`,
`mathToMarkdown`.
There is no default export.

### `math.toMarkdown`
### `mathFromMarkdown`

> Note: the separate extensions are also available at
> `mdast-util-math/from-markdown` and
> `mdast-util-math/to-markdown`.
### `mathToMarkdown`

Support math.
These exports are extensions, respectively for
Expand Down

0 comments on commit 5c22a0a

Please sign in to comment.