Skip to content
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

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions packages/micromark-extension-abbr/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__tests__/
specs/
.npmignore
38 changes: 38 additions & 0 deletions packages/micromark-extension-abbr/README.md
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
137 changes: 137 additions & 0 deletions packages/micromark-extension-abbr/__tests__/plugin.test.js
Copy link
Member

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?

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(
Copy link
Member

Choose a reason for hiding this comment

The 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)')
}
},
)
})
Loading
Loading