mdast-util-gfm
Advanced tools
Comparing version 2.0.1 to 2.0.2
/** | ||
* @returns {Array.<FromMarkdownExtension>} | ||
* Create an extension for `mdast-util-from-markdown` to enable GFM (autolink | ||
* literals, footnotes, strikethrough, tables, tasklists). | ||
* | ||
* @returns {Array<FromMarkdownExtension>} | ||
* Extension for `mdast-util-from-markdown` to enable GFM (autolink literals, | ||
* footnotes, strikethrough, tables, tasklists). | ||
*/ | ||
export function gfmFromMarkdown(): Array<FromMarkdownExtension> | ||
/** | ||
* @param {Options} [options] | ||
* Create an extension for `mdast-util-to-markdown` to enable GFM (autolink | ||
* literals, footnotes, strikethrough, tables, tasklists). | ||
* | ||
* @param {Options | null | undefined} [options] | ||
* Configuration. | ||
* @returns {ToMarkdownExtension} | ||
* Extension for `mdast-util-to-markdown` to enable GFM (autolink literals, | ||
* footnotes, strikethrough, tables, tasklists). | ||
*/ | ||
export function gfmToMarkdown( | ||
options?: import('mdast-util-gfm-table').Options | undefined | ||
options?: Options | null | undefined | ||
): ToMarkdownExtension | ||
export type FromMarkdownExtension = import('mdast-util-from-markdown').Extension | ||
export type ToMarkdownExtension = import('mdast-util-to-markdown').Options | ||
/** | ||
* Configuration. | ||
*/ | ||
export type Options = import('mdast-util-gfm-table').Options |
/** | ||
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension | ||
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension | ||
* | ||
*/ | ||
/** | ||
* @typedef {import('mdast-util-gfm-table').Options} Options | ||
* Configuration. | ||
*/ | ||
@@ -27,3 +30,8 @@ | ||
/** | ||
* Create an extension for `mdast-util-from-markdown` to enable GFM (autolink | ||
* literals, footnotes, strikethrough, tables, tasklists). | ||
* | ||
* @returns {Array<FromMarkdownExtension>} | ||
* Extension for `mdast-util-from-markdown` to enable GFM (autolink literals, | ||
* footnotes, strikethrough, tables, tasklists). | ||
*/ | ||
@@ -41,4 +49,10 @@ export function gfmFromMarkdown() { | ||
/** | ||
* @param {Options} [options] | ||
* Create an extension for `mdast-util-to-markdown` to enable GFM (autolink | ||
* literals, footnotes, strikethrough, tables, tasklists). | ||
* | ||
* @param {Options | null | undefined} [options] | ||
* Configuration. | ||
* @returns {ToMarkdownExtension} | ||
* Extension for `mdast-util-to-markdown` to enable GFM (autolink literals, | ||
* footnotes, strikethrough, tables, tasklists). | ||
*/ | ||
@@ -45,0 +59,0 @@ export function gfmToMarkdown(options) { |
{ | ||
"name": "mdast-util-gfm", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "mdast extension to parse and serialize GFM (GitHub Flavored Markdown)", | ||
@@ -51,5 +51,5 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"github-slugger": "^1.0.0", | ||
"github-slugger": "^2.0.0", | ||
"hast-util-to-html": "^8.0.0", | ||
@@ -60,16 +60,15 @@ "mdast-util-to-hast": "^12.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.48.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"lib/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage", | ||
"crawl": "node --conditions development script/crawl-tests.js", | ||
"prepack": "npm run build && npm run format", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"crawl": "node script/crawl-tests", | ||
"test-api": "node --conditions development test/index.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -86,7 +85,15 @@ }, | ||
"xo": { | ||
"prettier": true | ||
"prettier": true, | ||
"overrides": [ | ||
{ | ||
"files": "test/**/*.js", | ||
"rules": { | ||
"no-await-in-loop": "off" | ||
} | ||
} | ||
] | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -93,0 +100,0 @@ }, |
250
readme.md
@@ -11,35 +11,83 @@ # mdast-util-gfm | ||
Extension for [`mdast-util-from-markdown`][from-markdown] and/or | ||
[`mdast-util-to-markdown`][to-markdown] to support GitHub flavored markdown in | ||
**[mdast][]**. | ||
When parsing (`from-markdown`), must be combined with | ||
[`micromark-extension-gfm`][extension]. | ||
[mdast][] extensions to parse and serialize [GFM][] (autolink literals, | ||
footnotes, strikethrough, tables, tasklists). | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When to use this](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`gfmFromMarkdown()`](#gfmfrommarkdown) | ||
* [`gfmToMarkdown(options?)`](#gfmtomarkdownoptions) | ||
* [`Options`](#options) | ||
* [HTML](#html) | ||
* [Syntax](#syntax) | ||
* [Syntax tree](#syntax-tree) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package contains two extensions that add support for GFM syntax in | ||
markdown to [mdast][]: : autolink literals (`www.x.com`), footnotes (`[^1]`), | ||
strikethrough (`~~stuff~~`), tables (`| cell |…`), and tasklists (`* [x]`). | ||
These extensions plug into | ||
[`mdast-util-from-markdown`][mdast-util-from-markdown] (to support parsing | ||
GFM in markdown into a syntax tree) and | ||
[`mdast-util-to-markdown`][mdast-util-to-markdown] (to support serializing | ||
GFM in syntax trees to markdown). | ||
## When to use this | ||
Use this if you’re dealing with the AST manually and need all of GFM. | ||
It’s probably nicer to use [`remark-gfm`][remark-gfm] with | ||
**[remark][]**, which includes this but provides a nicer interface and | ||
makes it easier to combine with hundreds of plugins. | ||
This project is useful when you want to support the same features that GitHub | ||
does in files in a repo, Gists, and several other places. | ||
Users frequently believe that some of these extensions, specifically autolink | ||
literals and tables, are part of normal markdown, so using `mdast-util-gfm` will | ||
help match your implementation to their understanding of markdown. | ||
There are several edge cases where GitHub’s implementation works in unexpected | ||
ways or even different than described in their spec, so *writing* in GFM is not | ||
always the best choice. | ||
Alternatively, the extensions can be used separately: | ||
You can use these extensions when you are working with | ||
`mdast-util-from-markdown` and `mdast-util-to-markdown` already. | ||
* [`syntax-tree/mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal) | ||
When working with `mdast-util-from-markdown`, you must combine this package | ||
with [`micromark-extension-gfm`][extension]. | ||
Instead of this package, you can also use the extensions separately: | ||
* [`mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal) | ||
— support GFM autolink literals | ||
* [`syntax-tree/mdast-util-gfm-footnote`](https://github.com/syntax-tree/mdast-util-gfm-footnote) | ||
* [`mdast-util-gfm-footnote`](https://github.com/syntax-tree/mdast-util-gfm-footnote) | ||
— support GFM footnotes | ||
* [`syntax-tree/mdast-util-gfm-strikethrough`](https://github.com/syntax-tree/mdast-util-gfm-strikethrough) | ||
* [`mdast-util-gfm-strikethrough`](https://github.com/syntax-tree/mdast-util-gfm-strikethrough) | ||
— support GFM strikethrough | ||
* [`syntax-tree/mdast-util-gfm-table`](https://github.com/syntax-tree/mdast-util-gfm-table) | ||
* [`mdast-util-gfm-table`](https://github.com/syntax-tree/mdast-util-gfm-table) | ||
— support GFM tables | ||
* [`syntax-tree/mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item) | ||
* [`mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item) | ||
— support GFM tasklists | ||
A different utility, [`mdast-util-frontmatter`][mdast-util-frontmatter], adds | ||
support for frontmatter. | ||
GitHub supports YAML frontmatter for files in repos and Gists but they don’t | ||
treat it as part of GFM. | ||
All these packages are used in [`remark-gfm`][remark-gfm], which | ||
focusses on making it easier to transform content by abstracting these | ||
internals away. | ||
This utility does not handle how markdown is turned to HTML. | ||
That’s done by [`mdast-util-to-hast`][mdast-util-to-hast]. | ||
If your content is not in English, you should configure that utility. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -49,5 +97,19 @@ npm install mdast-util-gfm | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
Say we have the following file, `example.md`: | ||
Say our document `example.md` contains: | ||
@@ -82,6 +144,6 @@ ```markdown | ||
And our module, `example.js`, looks as follows: | ||
…and our module `example.js` looks as follows: | ||
```js | ||
import fs from 'node:fs' | ||
import fs from 'node:fs/promises' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
@@ -92,3 +154,3 @@ import {toMarkdown} from 'mdast-util-to-markdown' | ||
const doc = fs.readFileSync('example.md') | ||
const doc = await fs.readFile('example.md') | ||
@@ -107,3 +169,3 @@ const tree = fromMarkdown(doc, { | ||
Now, running `node example` yields: | ||
…now running `node example.js` yields (positional info removed for brevity): | ||
@@ -257,4 +319,4 @@ ```js | ||
This package exports the following identifiers: `gfmFromMarkdown`, | ||
`gfmToMarkdown`. | ||
This package exports the identifiers [`gfmFromMarkdown`][api-gfm-from-markdown] | ||
and [`gfmToMarkdown`][api-gfm-to-markdown]. | ||
There is no default export. | ||
@@ -264,33 +326,93 @@ | ||
Create an extension for [`mdast-util-from-markdown`][mdast-util-from-markdown] | ||
to enable GFM (autolink literals, footnotes, strikethrough, tables, tasklists). | ||
###### Returns | ||
Extension for `mdast-util-from-markdown` to enable GFM | ||
([`Array<FromMarkdownExtension>`][from-markdown-extension]). | ||
### `gfmToMarkdown(options?)` | ||
Support GFM. | ||
The export of `fromMarkdown` is a function that can be called and returns an | ||
extension for [`mdast-util-from-markdown`][from-markdown]. | ||
The export of `toMarkdown` is a function that can be called with options and | ||
returns an extension for [`mdast-util-to-markdown`][to-markdown]. | ||
Create an extension for [`mdast-util-to-markdown`][mdast-util-to-markdown] | ||
to enable GFM (autolink literals, footnotes, strikethrough, tables, tasklists). | ||
###### `options` | ||
###### Parameters | ||
Passed as `options` to [`mdast-util-gfm-table`][table]. | ||
* `options` ([`Options`][api-options]) | ||
— configuration | ||
###### Returns | ||
Extension for `mdast-util-to-markdown` to enable GFM | ||
([`Array<ToMarkdownExtension>`][to-markdown-extension]). | ||
### `Options` | ||
Configuration (TypeScript type). | ||
###### Fields | ||
* `tableCellPadding` (`boolean`, default: `true`) | ||
— whether to add a space of padding between delimiters and cells | ||
* `tablePipeAlign` (`boolean`, default: `true`) | ||
— whether to align the delimiters | ||
* `stringLength` (`((value: string) => number)`, default: `s => s.length`) | ||
— function to detect the length of table cell content, used when aligning | ||
the delimiters between cells | ||
## HTML | ||
This utility does not handle how markdown is turned to HTML. | ||
That’s done by [`mdast-util-to-hast`][mdast-util-to-hast]. | ||
## Syntax | ||
See [Syntax in `micromark-extension-gfm`][syntax]. | ||
## Syntax tree | ||
This utility combines several mdast utilities. | ||
See their readmes for the node types supported in the tree: | ||
* [`mdast-util-gfm-autolink-literal`](https://github.com/syntax-tree/mdast-util-gfm-autolink-literal#syntax-tree) | ||
— GFM autolink literals | ||
* [`mdast-util-gfm-footnote`](https://github.com/syntax-tree/mdast-util-gfm-footnote#syntax-tree) | ||
— GFM footnotes | ||
* [`mdast-util-gfm-strikethrough`](https://github.com/syntax-tree/mdast-util-gfm-strikethrough#syntax-tree) | ||
— GFM strikethrough | ||
* [`mdast-util-gfm-table`](https://github.com/syntax-tree/mdast-util-gfm-table#syntax-tree) | ||
— GFM tables | ||
* [`mdast-util-gfm-task-list-item`](https://github.com/syntax-tree/mdast-util-gfm-task-list-item#syntax-tree) | ||
— GFM tasklists | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type [`Options`][api-options]. | ||
The `Delete`, `FootnoteDefinition`, `FootnoteReference`, `Table`, `TableRow`, | ||
and `TableCell` types of the mdast nodes are exposed from `@types/mdast`. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
This plugin works with `mdast-util-from-markdown` version 1+ and | ||
`mdast-util-to-markdown` version 1+. | ||
## Related | ||
* [`remarkjs/remark`][remark] | ||
— markdown processor powered by plugins | ||
* [`remarkjs/remark-gfm`][remark-gfm] | ||
* [`remark-gfm`][remark-gfm] | ||
— remark plugin to support GFM | ||
* [`micromark/micromark`][micromark] | ||
— the smallest commonmark-compliant markdown parser that exists | ||
* [`micromark/micromark-extension-gfm`][extension] | ||
* [`micromark-extension-gfm`][extension] | ||
— micromark extension to parse GFM | ||
* [`syntax-tree/mdast-util-from-markdown`][from-markdown] | ||
— mdast parser using `micromark` to create mdast from markdown | ||
* [`syntax-tree/mdast-util-to-markdown`][to-markdown] | ||
— mdast serializer to create markdown from mdast | ||
## Contribute | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -336,2 +458,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -341,22 +469,36 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[mdast]: https://github.com/syntax-tree/mdast | ||
[remark]: https://github.com/remarkjs/remark | ||
[remark-gfm]: https://github.com/remarkjs/remark-gfm | ||
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[from-markdown-extension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension | ||
[micromark]: https://github.com/micromark/micromark | ||
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[to-markdown-extension]: https://github.com/syntax-tree/mdast-util-to-markdown#options | ||
[mdast-util-frontmatter]: https://github.com/syntax-tree/mdast-util-frontmatter | ||
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast | ||
[extension]: https://github.com/micromark/micromark-extension-gfm | ||
[table]: https://github.com/syntax-tree/mdast-util-gfm-table#options | ||
[syntax]: https://github.com/micromark/micromark-extension-gfm#syntax | ||
[gfm]: https://github.github.com/gfm/ | ||
[api-gfm-from-markdown]: #gfmfrommarkdown | ||
[api-gfm-to-markdown]: #gfmtomarkdownoptions | ||
[api-options]: #options |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21018
13
97
496