-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[remark-sub-super] Start migrating to micromark
- Loading branch information
1 parent
f6b6fe4
commit 435116a
Showing
8 changed files
with
483 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__tests__/ | ||
specs/ | ||
.npmignore |
46 changes: 46 additions & 0 deletions
46
packages/micromark-extension-sub-super/__tests__/spec.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
||
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) { | ||
effects.enter('subSuperSequence') | ||
effects.consume(code) | ||
effects.exit('subSuperSequence') | ||
effects.exit('subSuperCall') | ||
|
||
return ok | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.