Skip to content

Commit 27aed31

Browse files
committed
Add improved docs
1 parent 11ee289 commit 27aed31

File tree

3 files changed

+186
-65
lines changed

3 files changed

+186
-65
lines changed

dev/lib/html.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
* @typedef {Record<string, Handle>} HtmlOptions
1414
* Configuration.
1515
*
16+
* > 👉 **Note**: the special field `'*'` can be used to specify a fallback
17+
* > handle to handle all otherwise unhandled directives.
18+
*
1619
* @callback Handle
1720
* Handle a directive.
1821
* @param {CompileContext} this
1922
* Current context.
2023
* @param {Directive} directive
2124
* Directive.
2225
* @returns {boolean | void}
23-
* Signal that the directive could not be handled, in which case the fallback
24-
* is used (when given: a special handle for `'*'`).
26+
* Signal whether the directive was handled.
27+
* Yield `false` to let the fallback (a special handle for `'*'`) handle it.
2528
*
2629
* @typedef Directive
27-
* Structure representing a directive
30+
* Structure representing a directive.
2831
* @property {DirectiveType} type
2932
* Kind.
3033
* @property {string} name

readme.md

Lines changed: 178 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,103 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
**[micromark][]** extension to support the [generic directives proposal][prop]
12-
(`:cite[smith04]`, `::youtube[Video of a cat in a box]{v=01ab2cd3efg}`, and
11+
[micromark][] extensions to support [directives][prop] (`:cite[smith04]` and
1312
such).
1413

15-
Generic directives solve the need for an infinite number of potential extensions
16-
to markdown in a single markdown-esque way.
17-
However, it’s just [a proposal][prop] and may never be specced.
14+
## Contents
15+
16+
* [What is this?](#what-is-this)
17+
* [When to use this](#when-to-use-this)
18+
* [Install](#install)
19+
* [Use](#use)
20+
* [API](#api)
21+
* [`directive()`](#directive)
22+
* [`directiveHtml(options?)`](#directivehtmloptions)
23+
* [`Directive`](#directive-1)
24+
* [`Handle`](#handle)
25+
* [`HtmlOptions`](#htmloptions)
26+
* [Authoring](#authoring)
27+
* [HTML](#html)
28+
* [CSS](#css)
29+
* [Syntax](#syntax)
30+
* [Types](#types)
31+
* [Compatibility](#compatibility)
32+
* [Security](#security)
33+
* [Related](#related)
34+
* [Contribute](#contribute)
35+
* [License](#license)
36+
37+
## What is this?
38+
39+
This package contains two extensions that add support for directive syntax in
40+
markdown to [`micromark`][micromark].
1841

1942
## When to use this
2043

21-
If you’re using [`micromark`][micromark] or
22-
[`mdast-util-from-markdown`][from-markdown], use this package.
23-
Alternatively, if you’re using **[remark][]**, use
24-
[`remark-directive`][remark-directive].
44+
This project is useful when you want to solve the need for an infinite number
45+
of potential extensions to markdown in a single markdown-esque way.
46+
47+
You can use these extensions when you are working with [`micromark`][micromark]
48+
already.
49+
50+
When you need a syntax tree, you can combine this package with
51+
[`mdast-util-directive`][mdast-util-directive].
52+
53+
All these packages are used [`remark-directive`][remark-directive], which
54+
focusses on making it easier to transform content by abstracting these
55+
internals away.
2556

2657
## Install
2758

28-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
29-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
59+
This package is [ESM only][esm].
60+
In Node.js (version 14.14+), install with [npm][]:
3061

3162
[npm][]:
3263

3364
```sh
3465
npm install micromark-extension-directive
3566
```
3667

68+
In Deno with [`esm.sh`][esmsh]:
69+
70+
```js
71+
import {directive, directiveHtml} from 'https://esm.sh/micromark-extension-directive@2'
72+
```
73+
74+
In browsers with [`esm.sh`][esmsh]:
75+
76+
```html
77+
<script type="module">
78+
import {directive, directiveHtml} from 'https://esm.sh/micromark-extension-directive@2?bundle'
79+
</script>
80+
```
81+
3782
## Use
3883

39-
Say we have the following file, `example.md`:
84+
Say our document `example.md` contains:
4085

4186
```markdown
4287
A lovely language know as :abbr[HTML]{title="HyperText Markup Language"}.
4388
```
4489

45-
And our script, `example.js`, looks as follows:
90+
…and our module `example.js` looks as follows:
4691

4792
```js
48-
import fs from 'node:fs'
93+
import fs from 'node:fs/promises'
4994
import {micromark} from 'micromark'
5095
import {directive, directiveHtml} from 'micromark-extension-directive'
5196

52-
const output = micromark(fs.readFileSync('example.md'), {
97+
const output = micromark(await fs.readFile('example.md'), {
5398
extensions: [directive()],
5499
htmlExtensions: [directiveHtml({abbr})]
55100
})
56101

57102
console.log(output)
58103

104+
/**
105+
* @this {import('micromark-util-types').CompileContext}
106+
* @type {import('micromark-extension-directive').Handle}
107+
*/
59108
function abbr(d) {
60109
if (d.type !== 'textDirective') return false
61110

@@ -71,60 +120,100 @@ function abbr(d) {
71120
}
72121
```
73122

74-
Now, running `node example` yields (abbreviated):
123+
…now running `node example.js` yields:
75124

76125
```html
77126
<p>A lovely language know as <abbr title="HyperText Markup Language">HTML</abbr>.</p>
78127
```
79128

80129
## API
81130

82-
This package exports the following identifiers: `directive`, `directiveHtml`.
131+
This package exports the identifiers [`directive`][api-directive] and
132+
[`directiveHtml`][api-directive-html].
83133
There is no default export.
84134

85-
The export map supports the endorsed
86-
[`development` condition](https://nodejs.org/api/packages.html#packages_resolving_user_conditions).
135+
The export map supports the [`development` condition][development].
87136
Run `node --conditions development module.js` to get instrumented dev code.
88137
Without this condition, production code is loaded.
89138

90-
### `directive(syntaxOptions?)`
139+
### `directive()`
91140

92-
### `directiveHtml(htmlOptions?)`
141+
Create an extension for `micromark` to enable directive syntax.
93142

94-
Functions that can be called with options to get an extension for micromark to
95-
parse directives (can be passed in `extensions`) and one to compile them to HTML
96-
(can be passed in `htmlExtensions`).
143+
###### Returns
97144

98-
###### `syntaxOptions`
145+
Extension for `micromark` that can be passed in `extensions`, to enable
146+
directive syntax ([`Extension`][micromark-extension]).
99147

100-
None yet, but might be added in the future.
148+
### `directiveHtml(options?)`
101149

102-
###### `htmlOptions`
150+
Create an extension for `micromark` to support directives when serializing to
151+
HTML.
103152

104-
An object mapping names of directives to handlers
105-
([`Record<string, Handle>`][handle]).
106-
The special name `'*'` is the fallback to handle all unhandled directives.
153+
> 👉 **Note**: this uses KaTeX to render math.
107154
108-
### `function handle(directive)`
155+
###### Parameters
109156

110-
How to handle a `directive` ([`Directive`][directive]).
157+
* `options` ([`HtmlOptions`][api-html-options], optional)
158+
— configuration
111159

112-
##### Returns
160+
###### Returns
113161

114-
`boolean` or `void``false` can be used to signal that the directive could not
115-
be handled, in which case the fallback is used (when given).
162+
Extension for `micromark` that can be passed in `htmlExtensions`, to
163+
support directives when serializing to HTML
164+
([`HtmlExtension`][micromark-html-extension]).
116165

117166
### `Directive`
118167

119-
Structure representing a directive.
168+
Structure representing a directive (TypeScript type).
120169

121170
###### Fields
122171

123-
* `type` (`'textDirective'|'leafDirective'|'containerDirective'`)
124-
* `name` (`string`) — name of directive
125-
* `label` (`string?`) — compiled HTML content that was in `[brackets]`
126-
* `attributes` (`Record<string, string>?`) — object w/ HTML attributes
127-
* `content` (`string?`) — compiled HTML content inside container directive
172+
* `type` (`'textDirective'`, `'leafDirective'`, or `'containerDirective'`)
173+
— kind
174+
* `name` (`string`)
175+
— name of directive
176+
* `label` (`string`, optional)
177+
— compiled HTML content that was in `[brackets]`
178+
* `attributes` (`Record<string, string>`, optional)
179+
— object w/ HTML attributes
180+
* `content` (`string`, optional)
181+
— compiled HTML content inside container directive
182+
183+
### `Handle`
184+
185+
Handle a directive (TypeScript type).
186+
187+
###### Parameters
188+
189+
* `this` ([`CompileContext`][micromark-compile-context])
190+
— current context
191+
* `directive` ([`Directive`][api-directive-type])
192+
— directive
193+
194+
###### Returns
195+
196+
Signal whether the directive was handled (`boolean`, default: `true`).
197+
Yield `false` to let the fallback (a special handle for `'*'`) handle it.
198+
199+
### `HtmlOptions`
200+
201+
Configuration (TypeScript type).
202+
203+
> 👉 **Note**: the special field `'*'` can be used to specify a fallback handle
204+
> to handle all otherwise unhandled directives.
205+
206+
###### Type
207+
208+
```ts
209+
type HtmlOptions = Record<string, Handle>
210+
```
211+
212+
## Authoring
213+
214+
## HTML
215+
216+
## CSS
128217
129218
## Syntax
130219
@@ -200,20 +289,33 @@ this implementation mimics CommonMark as closely as possible:
200289
* The label and attributes in a leaf or container cannot include line endings
201290
(~~`::a[b\nc]`~~) — because it’s not allowed in fenced code either
202291

292+
## Types
293+
294+
This package is fully typed with [TypeScript][].
295+
It exports the additional types [`Handle`][api-handle] and
296+
[`HtmlOptions`][api-html-options].
297+
298+
## Compatibility
299+
300+
Projects maintained by the unified collective are compatible with all maintained
301+
versions of Node.js.
302+
As of now, that is Node.js 14.14+.
303+
Our projects sometimes work with older versions, but this is not guaranteed.
304+
305+
These extensions work with `micromark` version 3+.
306+
307+
## Security
308+
309+
This package is safe assuming that you write safe handlers.
310+
Any vulnerability in your code could open you to a
311+
[cross-site scripting (XSS)][xss] attack.
312+
203313
## Related
204314

205-
* [`remarkjs/remark`][remark]
206-
— markdown processor powered by plugins
207-
* [`remarkjs/remark-directive`][remark-directive]
208-
— remark plugin using this to support directive
209-
* [`micromark/micromark`][micromark]
210-
— the smallest commonmark-compliant markdown parser that exists
211-
* [`syntax-tree/mdast-util-directive`][mdast-util-directive]
212-
— mdast utility to support generic directives
213-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
214-
— mdast parser using `micromark` to create mdast from markdown
215-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
216-
— mdast serializer to create markdown from mdast
315+
* [`remark-directive`][remark-directive]
316+
— remark plugin to support directives
317+
* [`mdast-util-directive`][mdast-util-directive]
318+
— mdast utility to support directives
217319

218320
## Contribute
219321

@@ -259,6 +361,8 @@ abide by its terms.
259361

260362
[npm]: https://docs.npmjs.com/cli/install
261363

364+
[esmsh]: https://esm.sh
365+
262366
[license]: license
263367

264368
[author]: https://wooorm.com
@@ -269,20 +373,34 @@ abide by its terms.
269373

270374
[coc]: https://github.com/micromark/.github/blob/HEAD/code-of-conduct.md
271375

272-
[micromark]: https://github.com/micromark/micromark
376+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
273377

274-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
378+
[typescript]: https://www.typescriptlang.org
275379

276-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
380+
[development]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
277381

278-
[remark]: https://github.com/remarkjs/remark
382+
[micromark]: https://github.com/micromark/micromark
279383

280-
[prop]: https://talk.commonmark.org/t/generic-directives-plugins-syntax/444
384+
[micromark-html-extension]: https://github.com/micromark/micromark#htmlextension
385+
386+
[micromark-extension]: https://github.com/micromark/micromark#syntaxextension
387+
388+
[micromark-compile-context]: https://github.com/micromark/micromark/blob/41e3c4c/packages/micromark-util-types/index.js#L457
281389

282390
[mdast-util-directive]: https://github.com/syntax-tree/mdast-util-directive
283391

284392
[remark-directive]: https://github.com/remarkjs/remark-directive
285393

286-
[handle]: #function-handledirective
394+
[prop]: https://talk.commonmark.org/t/generic-directives-plugins-syntax/444
395+
396+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
397+
398+
[api-directive]: #directive
399+
400+
[api-directive-html]: #directivehtmloptions
401+
402+
[api-directive-type]: #directive-1
403+
404+
[api-handle]: #handle
287405

288-
[directive]: #directive
406+
[api-html-options]: #htmloptions

test/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @typedef {import('micromark-util-types').CompileContext} CompileContext
3-
* @typedef {import('../dev/index.js').HtmlOptions} HtmlOptions
4-
* @typedef {import('../dev/index.js').Handle} Handle
3+
* @typedef {import('micromark-extension-directive').HtmlOptions} HtmlOptions
4+
* @typedef {import('micromark-extension-directive').Handle} Handle
55
*/
66

77
import assert from 'node:assert/strict'

0 commit comments

Comments
 (0)