-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update remark-abbr to work with remark@next #516
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__tests__/ | ||
specs/ | ||
.npmignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# `micromark-extension-abbr` | ||
|
||
**[micromark][]** extension that parses custom Markdown syntax to handle | ||
abbreviations. | ||
|
||
This package provides the low-level modules for integrating with the micromark | ||
tokenizer. | ||
|
||
## Install | ||
|
||
[npm][]: | ||
|
||
```sh | ||
npm install micromark-extension-abbr | ||
``` | ||
|
||
## API | ||
|
||
### `abbr` | ||
|
||
Support custom syntax to handle abbreviations. | ||
|
||
The export of `abbr` is an extension for the micromark parser (can be passed | ||
in `extensions`). | ||
|
||
## License | ||
|
||
[MIT][license] © [Zeste de Savoir][zds] | ||
|
||
<!-- Definitions --> | ||
|
||
[license]: LICENCE | ||
|
||
[micromark]: https://github.com/micromark/micromark | ||
|
||
[npm]: https://docs.npmjs.com/cli/install | ||
|
||
[zds]: https://zestedesavoir.com |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import {preprocess, parse, postprocess} from 'micromark' | ||
import {abbr, abbrTypes} from '../lib/syntax' | ||
|
||
describe('micromark-extension-abbr', () => { | ||
it('parses definitions', () => { | ||
const input = `*[HTML]: Hyper Text Markup Language` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const eventTypes = events.map((event) => [event[0], event[1].type]) | ||
expect(eventTypes).toEqual( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't aware of these kind of tests, am quite worried that they might be too micromark-dependant, and breaking on newer versions. Did you saw them used in another project? |
||
// prettier-ignore | ||
[ | ||
[ 'enter', 'content' ], | ||
[ 'enter', 'abbrDefinition' ], | ||
[ 'enter', 'abbrDefinitionLabel' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'enter', 'abbrDefinitionString' ], | ||
[ 'enter', 'data' ], | ||
[ 'exit', 'data' ], | ||
[ 'exit', 'abbrDefinitionString' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionLabel' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'enter', 'lineSuffix' ], | ||
[ 'exit', 'lineSuffix' ], | ||
[ 'enter', 'abbrDefinitionValueString' ], | ||
[ 'enter', 'data' ], | ||
[ 'exit', 'data' ], | ||
[ 'exit', 'abbrDefinitionValueString' ], | ||
[ 'exit', 'abbrDefinition' ], | ||
[ 'exit', 'content' ], | ||
], | ||
) | ||
}) | ||
|
||
it('parses definitions without whitespace', () => { | ||
const input = `*[HTML]:Hyper Text Markup Language` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const eventTypes = events.map((event) => [event[0], event[1].type]) | ||
expect(eventTypes).toEqual( | ||
// prettier-ignore | ||
[ | ||
[ 'enter', 'content' ], | ||
[ 'enter', 'abbrDefinition' ], | ||
[ 'enter', 'abbrDefinitionLabel' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'enter', 'abbrDefinitionString' ], | ||
[ 'enter', 'data' ], | ||
[ 'exit', 'data' ], | ||
[ 'exit', 'abbrDefinitionString' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionLabel' ], | ||
[ 'enter', 'abbrDefinitionMarker' ], | ||
[ 'exit', 'abbrDefinitionMarker' ], | ||
[ 'enter', 'abbrDefinitionValueString' ], | ||
[ 'enter', 'data' ], | ||
[ 'exit', 'data' ], | ||
[ 'exit', 'abbrDefinitionValueString' ], | ||
[ 'exit', 'abbrDefinition' ], | ||
[ 'exit', 'content' ], | ||
], | ||
) | ||
}) | ||
|
||
it('does not parse definitions with empty labels', () => { | ||
const input = `*[]: Empty` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const abbrDefinitions = events.filter( | ||
(event) => event[1].type === abbrTypes.abbrDefinition, | ||
) | ||
expect(abbrDefinitions).toEqual([]) | ||
}) | ||
|
||
it( | ||
'does not parse definitions with parens instead of square brackets', | ||
() => { | ||
const input = `*(HTML): Hyper Text Markup Language` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const abbrDefinitions = events.filter( | ||
(event) => event[1].type === abbrTypes.abbrDefinition, | ||
) | ||
expect(abbrDefinitions).toEqual([]) | ||
}, | ||
) | ||
|
||
it('does not parse definitions without colons', () => { | ||
const input = `*[HTML]; Hyper Text Markup Language` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const abbrDefinitions = events.filter( | ||
(event) => event[1].type === abbrTypes.abbrDefinition, | ||
) | ||
expect(abbrDefinitions).toEqual([]) | ||
}) | ||
|
||
it('parses definitions with labels containing spaces and punctuation', () => { | ||
const input = `*[MV(VSL) (E&W)]: Motor Vehicles (Variation of Speed Limits) (England & Wales) Regulations` | ||
const events = postprocess( | ||
parse({extensions: [abbr]}) | ||
.document() | ||
.write(preprocess()(input, null, true)), | ||
) | ||
const abbrDefinitionString = events.find( | ||
(event) => event[1].type === abbrTypes.abbrDefinitionString, | ||
) | ||
if (abbrDefinitionString === undefined) { | ||
throw new Error('could not find an abbrDefinitionString') | ||
} else { | ||
const [_, token, context] = abbrDefinitionString | ||
expect(context.sliceSerialize(token)).toEqual('MV(VSL) (E&W)') | ||
} | ||
}, | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the tests. Would like to see a few (you do not need to reproduce all of them) to see full Markdown to HTML conversion. This means also writing a small (usually ridiculously small) file like this one. Maybe in your case it is actually not so simple since you do not directly compile to HTML?