Skip to content

Commit

Permalink
[remark-sub-super] Start migrating to micromark
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab committed Aug 2, 2024
1 parent f6b6fe4 commit 435116a
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"scripts": {
"pretest": "lerna run pretest --scope zmarkdown",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules DEST=/tmp jest packages/remark-kbd packages/remark-iframes packages/remark-ping packages/micromark-extension-kbd packages/micromark-extension-iframes packages/micromark-extension-ping",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules DEST=/tmp jest packages/remark-kbd packages/remark-iframes packages/remark-ping packages/micromark-extension-kbd packages/micromark-extension-iframes packages/micromark-extension-ping packages/micromark-extension-sub-super",
"lint": "eslint .",
"posttest": "lerna run posttest --scope zmarkdown",
"build": "lerna run build",
Expand Down
3 changes: 3 additions & 0 deletions packages/micromark-extension-sub-super/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__tests__/
specs/
.npmignore
46 changes: 46 additions & 0 deletions packages/micromark-extension-sub-super/__tests__/spec.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { micromark } from 'micromark'
import micromarkSubSuper from '../lib/index'
import micromarkSubSuperHtml from '../lib/html'

const specificationTests = {
'works - sub': ['CO~2~', '<p>CO<sub>2</sub></p>'],
'works - super': ['a^2^ + b^2^ = c^2^', '<p>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></p>'],
'inside words': ['Literally s^e^lfies tbh lo-fi.', '<p>Literally s<sup>e</sup>lfies tbh lo-fi.</p>'],
'needs content - sub': ['a~~', '<p>a~~</p>'],
'needs content - super': ['b^^', '<p>b^^</p>'],
'space isn\'t content - sub': ['a~ ~', '<p>a~ ~</p>'],
'space isn\'t content - super': ['b^ ^', '<p>b^ ^</p>'],
'double entry': ['^^foo^^', '<p>^<sup>foo</sup>^</p>'],
'more than one char': ['a^1+1^ + b^1+1^ = c^1+1^', '<p>a<sup>1+1</sup> + b<sup>1+1</sup> = c<sup>1+1</sup></p>'],
'does not start with space - sub': ['a~ ~ + b~ ~', '<p>a~ ~ + b~ ~</p>'],
'does not start with space - super': ['a^ ^ + b^ ^', '<p>a^ ^ + b^ ^</p>'],
'cannot contain block': ['a~b\n\nc~', '<p>a~b</p>\n<p>c~</p>'],
'escaped - sub': ['a\\~no\\~', '<p>a~no~</p>'],
'escaped - super': ['a\\^no\\^', '<p>a^no^</p>'],
'escaped inside': ['a^\\^^', '<p>a<sup>^</sup></p>'],
'lone tilde': ['a ~ b', '<p>a ~ b</p>'],
'can contain inline - super': ['my ^*important*^ superscript', '<p>my <sup><em>important</em></sup> superscript</p>'],
'can contain inline - sub': ['my ~*important*~ subscript', '<p>my <sub><em>important</em></sub> subscript</p>'],
'can be contained': ['my *im~por~tant* subscript', '<p>my <em>im<sub>por</sub>tant</em> subscript</p>'],
'can be self-contained': ['2^2^2^^ = 16', '<p>2<sup>2<sup>2</sup></sup> = 16</p>'],
'can be cross-contained': ['remark-~sub-^super^~', '<p>remark-<sub>sub-<sup>super</sup></sub></p>']
}

const renderString = (fixture) =>
micromark(fixture, {
extensions: [micromarkSubSuper()],
htmlExtensions: [micromarkSubSuperHtml]
})

describe('conforms to the specification', () => {
for (const test in specificationTests) {
const jestFunction = (!specificationTests[test][2]) ? it : it.skip

jestFunction(test, () => {
const [input, expectedOutput] = specificationTests[test]
const output = renderString(input)

expect(output).toEqual(expectedOutput)
})
}
})
26 changes: 26 additions & 0 deletions packages/micromark-extension-sub-super/lib/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
enter: {
subString: enterSubData,
superString: enterSuperData
},
exit: {
subString: exitSubData,
superString: exitSuperData
}
}

function enterSubData () {
this.tag('<sub>')
}

function enterSuperData () {
this.tag('<sup>')
}

function exitSubData () {
this.tag('</sub>')
}

function exitSuperData () {
this.tag('</sup>')
}
84 changes: 84 additions & 0 deletions packages/micromark-extension-sub-super/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { markdownLineEnding } from 'micromark-util-character'
import { codes } from 'micromark-util-symbol'

export default function micromarkSubSuper (options = {}) {
// By default, use characters U+94 (`^`) and U+126 (`~`)
const unicodeSubChar = options.subCharCode || 126
const unicodeSuperChar = options.superCharCode || 94

const call = {
name: 'subSuper',
tokenize: tokenizeFactory(unicodeSubChar, unicodeSuperChar)
}

// Inject a hook called on the given characters
return {
text: {
[unicodeSubChar]: call,
[unicodeSuperChar]: call
}
}
}

function tokenizeFactory (subCharCode, superCharCode) {
return tokenizeSubSuper

function tokenizeSubSuper (effects, ok, nok) {
return start

function start (code) {
// We should not have entered here at all
if (code !== subCharCode && code !== superCharCode) return nok(code)

effects.enter('subSuperCall')
effects.enter('subSuperSequence')
effects.consume(code)
effects.exit('subSuperSequence')

Check failure on line 37 in packages/micromark-extension-sub-super/lib/index.js

View workflow job for this annotation

GitHub Actions / Check linting problems

Trailing spaces not allowed
if (code === subCharCode) effects.enter('subString')
else if (code === superCharCode) effects.enter('superString')
effects.enter('data')

return afterStart
}

function afterStart (code) {
if (code === subCharCode ||
code === superCharCode ||
code === codes.space) return nok(code)

return content(code)
}

function content (code) {
if (code === subCharCode) return subEnd(code)
else if (code === superCharCode) return superEnd(code)

if (code === codes.eof || markdownLineEnding(code)) return nok(code)

effects.consume(code)
return content
}

function subEnd (code) {
effects.exit('data')
effects.exit('subString')
return end(code)
}

function superEnd (code) {
effects.exit('data')
effects.exit('superString')
return end(code)
}

function end(code) {

Check failure on line 75 in packages/micromark-extension-sub-super/lib/index.js

View workflow job for this annotation

GitHub Actions / Check linting problems

Missing space before function parentheses
effects.enter('subSuperSequence')
effects.consume(code)
effects.exit('subSuperSequence')
effects.exit('subSuperCall')

return ok
}
}
}
44 changes: 44 additions & 0 deletions packages/micromark-extension-sub-super/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "micromark-sub-super",
"version": "0.0.0",
"description": "Add Markdown syntax to handle subscript and superscript",
"type": "module",
"keywords": [
"micromark",
"subscript",
"superscript",
"plugin",
"extension"
],
"author": "Stalone <[email protected]>",
"homepage": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/micromark-extension-sub-super",
"license": "MIT",
"main": "lib/index.js",
"module": "lib/index.js",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/zestedesavoir/zmarkdown.git#master"
},
"scripts": {
"pretest": "eslint .",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
},
"bugs": {
"url": "https://github.com/zestedesavoir/zmarkdown/issues"
},
"dependencies": {
"micromark-util-character": "^2.1.0",
"micromark-util-symbol": "^2.0.0"
},
"devDependencies": {
"micromark": "^4.0.0"
}
}
Loading

0 comments on commit 435116a

Please sign in to comment.