Skip to content

Commit 687f06b

Browse files
committed
[remark-sub-super] Migrate to micromark
1 parent ee30198 commit 687f06b

File tree

8 files changed

+482
-1
lines changed

8 files changed

+482
-1
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"scripts": {
5959
"pretest": "lerna run pretest --scope zmarkdown",
60-
"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",
60+
"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",
6161
"lint": "eslint .",
6262
"posttest": "lerna run posttest --scope zmarkdown",
6363
"build": "lerna run build",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__tests__/
2+
specs/
3+
.npmignore
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { micromark } from 'micromark'
2+
import micromarkSubSuper from '../lib/index'
3+
import micromarkSubSuperHtml from '../lib/html'
4+
5+
const specificationTests = {
6+
'works - sub': ['CO~2~', '<p>CO<sub>2</sub></p>'],
7+
'works - super': ['a^2^ + b^2^ = c^2^', '<p>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></p>'],
8+
'inside words': ['Literally s^e^lfies tbh lo-fi.', '<p>Literally s<sup>e</sup>lfies tbh lo-fi.</p>'],
9+
'needs content - sub': ['a~~', '<p>a~~</p>'],
10+
'needs content - super': ['b^^', '<p>b^^</p>'],
11+
'space isn\'t content - sub': ['a~ ~', '<p>a~ ~</p>'],
12+
'space isn\'t content - super': ['b^ ^', '<p>b^ ^</p>'],
13+
'double entry': ['^^foo^^', '<p>^<sup>foo</sup>^</p>'],
14+
'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>'],
15+
'does not start with space - sub': ['a~ ~ + b~ ~', '<p>a~ ~ + b~ ~</p>'],
16+
'does not start with space - super': ['a^ ^ + b^ ^', '<p>a^ ^ + b^ ^</p>'],
17+
'cannot contain block': ['a~b\n\nc~', '<p>a~b</p>\n<p>c~</p>'],
18+
'escaped - sub': ['a\\~no\\~', '<p>a~no~</p>'],
19+
'escaped - super': ['a\\^no\\^', '<p>a^no^</p>'],
20+
'escaped inside': ['a^\\^^', '<p>a<sup>^</sup></p>', true],
21+
'lone tilde': ['a ~ b', '<p>a ~ b</p>'],
22+
'can contain inline - super': ['my ^*important*^ superscript', '<p>my <sup><em>important</em></sup> superscript</p>'],
23+
'can contain inline - sub': ['my ~*important*~ subscript', '<p>my <sub><em>important</em></sub> subscript</p>'],
24+
'can be contained': ['my *im~por~tant* subscript', '<p>my <em>im<sub>por</sub>tant</em> subscript</p>'],
25+
'can be self-contained': ['2^2^2^^ = 16', '<p>2<sup>2<sup>2</sup></sup> = 16</p>', true],
26+
'can be cross-contained': ['remark-~sub-^super^~', '<p>remark-<sub>sub-<sup>super</sup></sub></p>']
27+
}
28+
29+
const renderString = (fixture) =>
30+
micromark(fixture, {
31+
extensions: [micromarkSubSuper()],
32+
htmlExtensions: [micromarkSubSuperHtml]
33+
})
34+
35+
describe('conforms to the specification', () => {
36+
for (const test in specificationTests) {
37+
const jestFunction = (!specificationTests[test][2]) ? it : it.skip
38+
39+
jestFunction(test, () => {
40+
const [input, expectedOutput] = specificationTests[test]
41+
const output = renderString(input)
42+
43+
expect(output).toEqual(expectedOutput)
44+
})
45+
}
46+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default {
2+
enter: {
3+
subString: enterSubData,
4+
superString: enterSuperData
5+
},
6+
exit: {
7+
subString: exitSubData,
8+
superString: exitSuperData
9+
}
10+
}
11+
12+
function enterSubData () {
13+
this.tag('<sub>')
14+
}
15+
16+
function enterSuperData () {
17+
this.tag('<sup>')
18+
}
19+
20+
function exitSubData () {
21+
this.tag('</sub>')
22+
}
23+
24+
function exitSuperData () {
25+
this.tag('</sup>')
26+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { markdownLineEnding } from 'micromark-util-character'
2+
import { codes } from 'micromark-util-symbol'
3+
4+
export default function micromarkSubSuper (options = {}) {
5+
// By default, use characters U+94 (`^`) and U+126 (`~`)
6+
const unicodeSubChar = options.subCharCode || 126
7+
const unicodeSuperChar = options.superCharCode || 94
8+
9+
const call = {
10+
name: 'subSuper',
11+
tokenize: tokenizeFactory(unicodeSubChar, unicodeSuperChar)
12+
}
13+
14+
// Inject a hook called on the given characters
15+
return {
16+
text: {
17+
[unicodeSubChar]: call,
18+
[unicodeSuperChar]: call
19+
}
20+
}
21+
}
22+
23+
function tokenizeFactory (subCharCode, superCharCode) {
24+
const CONSTRUCT_SUB_NAME = 'subString'
25+
const CONSTRUCT_SUPER_NAME = 'superString'
26+
27+
return tokenizeSubSuper
28+
29+
function tokenizeSubSuper (effects, ok, nok) {
30+
let constructSequenceName
31+
return start
32+
33+
function isMatchingConstruct (code) {
34+
return (
35+
(constructSequenceName !== CONSTRUCT_SUB_NAME && code === superCharCode) ||
36+
(constructSequenceName !== CONSTRUCT_SUPER_NAME && code === subCharCode)
37+
)
38+
}
39+
40+
function start (code) {
41+
// We should not have entered here at all
42+
if (code !== subCharCode && code !== superCharCode) return nok(code)
43+
44+
effects.enter('subSuperCall')
45+
effects.enter('subSuperSequence')
46+
effects.consume(code)
47+
effects.exit('subSuperSequence')
48+
49+
if (code === subCharCode) constructSequenceName = CONSTRUCT_SUB_NAME
50+
else if (code === superCharCode) constructSequenceName = CONSTRUCT_SUPER_NAME
51+
52+
effects.enter(constructSequenceName)
53+
effects.enter('chunkText', { contentType: 'text' })
54+
55+
return afterStart
56+
}
57+
58+
function afterStart (code) {
59+
if (isMatchingConstruct(code) || code === codes.space) return nok(code)
60+
61+
return content(code)
62+
}
63+
64+
function content (code) {
65+
if (isMatchingConstruct(code)) return end(code)
66+
if (code === codes.eof || markdownLineEnding(code)) return nok(code)
67+
68+
effects.consume(code)
69+
return content
70+
}
71+
72+
function end (code) {
73+
effects.exit('chunkText')
74+
effects.exit(constructSequenceName)
75+
effects.enter('subSuperSequence')
76+
effects.consume(code)
77+
effects.exit('subSuperSequence')
78+
effects.exit('subSuperCall')
79+
80+
return ok
81+
}
82+
}
83+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "micromark-sub-super",
3+
"version": "0.0.0",
4+
"description": "Add Markdown syntax to handle subscript and superscript",
5+
"type": "module",
6+
"keywords": [
7+
"micromark",
8+
"subscript",
9+
"superscript",
10+
"plugin",
11+
"extension"
12+
],
13+
"author": "Stalone <stalone+zmd@boxph.one>",
14+
"homepage": "https://github.com/zestedesavoir/zmarkdown/tree/master/packages/micromark-extension-sub-super",
15+
"license": "MIT",
16+
"main": "lib/index.js",
17+
"module": "lib/index.js",
18+
"directories": {
19+
"lib": "lib",
20+
"test": "__tests__"
21+
},
22+
"files": [
23+
"lib"
24+
],
25+
"repository": {
26+
"type": "git",
27+
"url": "git+https://github.com/zestedesavoir/zmarkdown.git#master"
28+
},
29+
"scripts": {
30+
"pretest": "eslint .",
31+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
32+
"coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
33+
},
34+
"bugs": {
35+
"url": "https://github.com/zestedesavoir/zmarkdown/issues"
36+
},
37+
"dependencies": {
38+
"micromark-util-character": "^2.1.0",
39+
"micromark-util-symbol": "^2.0.0"
40+
},
41+
"devDependencies": {
42+
"micromark": "^4.0.0"
43+
}
44+
}

0 commit comments

Comments
 (0)