mdast-util-directive
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -6,2 +6,4 @@ import type {Parent} from 'unist' | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
interface DirectiveFields { | ||
@@ -37,1 +39,3 @@ name: string | ||
} | ||
/* eslint-enable @typescript-eslint/consistent-type-definitions */ |
18
index.js
@@ -157,3 +157,8 @@ /** | ||
) | ||
list.push(['id', parseEntities(this.sliceSerialize(token))]) | ||
list.push([ | ||
'id', | ||
parseEntities(this.sliceSerialize(token), { | ||
attribute: true | ||
}) | ||
]) | ||
} | ||
@@ -166,3 +171,8 @@ | ||
) | ||
list.push(['class', parseEntities(this.sliceSerialize(token))]) | ||
list.push([ | ||
'class', | ||
parseEntities(this.sliceSerialize(token), { | ||
attribute: true | ||
}) | ||
]) | ||
} | ||
@@ -175,3 +185,5 @@ | ||
) | ||
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token)) | ||
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token), { | ||
attribute: true | ||
}) | ||
} | ||
@@ -178,0 +190,0 @@ |
{ | ||
"name": "mdast-util-directive", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "mdast extension to parse and serialize generic directives (`:cite[smith04]`)", | ||
@@ -52,3 +52,3 @@ "license": "MIT", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
@@ -60,3 +60,3 @@ "rimraf": "^3.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.47.0" | ||
"xo": "^0.52.0" | ||
}, | ||
@@ -63,0 +63,0 @@ "scripts": { |
191
readme.md
@@ -11,27 +11,66 @@ # mdast-util-directive | ||
Extension for [`mdast-util-from-markdown`][from-markdown] and/or | ||
[`mdast-util-to-markdown`][to-markdown] to support the [generic directives | ||
proposal][prop] (`:cite[smith04]`, `::youtube[Video of a cat in a | ||
box]{v=01ab2cd3efg}`, and such) in **[mdast][]**. | ||
When parsing (`from-markdown`), must be combined with | ||
[`micromark-extension-directive`][extension]. | ||
[mdast][] extensions to parse and serialize [generic directives proposal][prop] | ||
(`:cite[smith04]`, `::youtube[Video of a cat in a box]{v=01ab2cd3efg}`, and | ||
such). | ||
See [`micromark-extension-directive`][extension] for how the syntax works. | ||
This utility handles parsing and serializing. | ||
[Traverse the tree][traversal] to change them to whatever you please. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When to use this](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`directiveFromMarkdown`](#directivefrommarkdown) | ||
* [`directiveToMarkdown`](#directivetomarkdown) | ||
* [Syntax tree](#syntax-tree) | ||
* [Nodes](#nodes) | ||
* [Mixin](#mixin) | ||
* [`Directive`](#directive) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package contains extensions that add support for generic directives to | ||
[`mdast-util-from-markdown`][mdast-util-from-markdown] and | ||
[`mdast-util-to-markdown`][mdast-util-to-markdown]. | ||
This package handles the syntax tree. | ||
You can use this with some more code to match your specific needs, to allow for | ||
anything from callouts, citations, styled blocks, forms, embeds, spoilers, etc. | ||
[Traverse the tree][traversal] to change directives to whatever you please. | ||
## When to use this | ||
Use this if you’re dealing with the AST manually. | ||
It might be better to use [`remark-directive`][plugin] with **[remark][]**, | ||
which includes this but provides a nicer interface and makes it easier to | ||
combine with hundreds of plugins. | ||
These tools are all rather low-level. | ||
In most cases, you’d want to use [`remark-directive`][remark-directive] with | ||
remark instead. | ||
Directives are one of the four ways to extend markdown: an arbitrary extension | ||
syntax (see [Extending markdown][extending-mmarkdown] in micromark’s docs for | ||
the alternatives and more info). | ||
This mechanism works well when you control the content: who authors it, what | ||
tools handle it, and where it’s displayed. | ||
When authors can read a guide on how to embed a tweet but are not expected to | ||
know the ins and outs of HTML or JavaScript. | ||
Directives don’t work well if you don’t know who authors content, what tools | ||
handle it, and where it ends up. | ||
Example use cases are a docs website for a project or product, or blogging tools | ||
and static site generators. | ||
When working with `mdast-util-from-markdown`, you must combine this package with | ||
[`micromark-extension-directive`][extension]. | ||
This utility does not handle how directives are turned to HTML. | ||
You must [traverse the tree][traversal] to change directives to whatever you | ||
please. | ||
## 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 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -41,7 +80,28 @@ npm install mdast-util-directive | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {directiveFromMarkdown, directiveToMarkdown} from 'https://esm.sh/mdast-util-directive@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {directiveFromMarkdown, directiveToMarkdown} from 'https://esm.sh/mdast-util-directive@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
Say our module, `example.js`, looks as follows: | ||
Say our document `example.md` contains: | ||
```markdown | ||
A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}. | ||
``` | ||
…and our module `example.js` looks as follows: | ||
```js | ||
import fs from 'node:fs/promises' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
@@ -52,3 +112,3 @@ import {toMarkdown} from 'mdast-util-to-markdown' | ||
const doc = 'A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.' | ||
const doc = await fs.readFile('example.md') | ||
@@ -67,3 +127,3 @@ const tree = fromMarkdown(doc, { | ||
Now, running `node example` yields (positional info removed for brevity): | ||
…now running `node example.js` yields (positional info removed for brevity): | ||
@@ -97,10 +157,13 @@ ```js | ||
This package exports the identifiers `directiveFromMarkdown` and | ||
`directiveToMarkdown`. | ||
There is no default export. | ||
### `directiveFromMarkdown` | ||
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown]. | ||
### `directiveToMarkdown` | ||
Support the [generic directives proposal][prop]. | ||
The exports are extensions, respectively | ||
for [`mdast-util-from-markdown`][from-markdown] and | ||
[`mdast-util-to-markdown`][to-markdown]. | ||
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown]. | ||
@@ -110,5 +173,2 @@ There are no options, but passing [`options.quote`][quote] to | ||
This utility handles parsing and serializing. | ||
[Traverse the tree][traversal] to change them to whatever you please. | ||
## Syntax tree | ||
@@ -265,21 +325,48 @@ | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types `ContainerDirective`, `LeafDirective`, | ||
`TextDirective`, and `Directive`. | ||
It also registers the node types with `@types/mdast`. | ||
If you’re working with the syntax tree, make sure to import this utility | ||
somewhere in your types, as that registers the new node types in the tree. | ||
```js | ||
/** | ||
* @typedef {import('mdast-util-directive')} | ||
*/ | ||
import {visit} from 'unist-util-visit' | ||
/** @type {import('mdast').Root} */ | ||
const tree = getMdastNodeSomeHow() | ||
visit(tree, (node) => { | ||
// `node` can now be one of the nodes for directives. | ||
}) | ||
``` | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 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-directive`][plugin] | ||
* [`remarkjs/remark-directive`][remark-directive] | ||
— remark plugin to support generic directives | ||
* [`micromark/micromark`][micromark] | ||
— the smallest commonmark-compliant markdown parser that exists | ||
* [`micromark/micromark-extension-directive`][extension] | ||
— micromark extension to parse directives | ||
* [`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. | ||
@@ -325,2 +412,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -330,26 +423,26 @@ | ||
[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 | ||
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[plugin]: https://github.com/remarkjs/remark-directive | ||
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[quote]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote | ||
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[extension]: https://github.com/micromark/micromark-extension-directive | ||
[micromark]: https://github.com/micromark/micromark | ||
[remark-directive]: https://github.com/remarkjs/remark-directive | ||
[extension]: https://github.com/micromark/micromark-extension-directive | ||
[extending-mmarkdown]: https://github.com/micromark/micromark#extending-markdown | ||
[prop]: https://talk.commonmark.org/t/generic-directives-plugins-syntax/444 | ||
[quote]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote | ||
[traversal]: https://unifiedjs.com/learn/recipe/tree-traversal/ | ||
@@ -356,0 +449,0 @@ |
28652
409
448