Lorem
ipsum.
@@ -121,8 +171,163 @@ The default export is `remarkDirective`. Configures remark so that it can parse and serialize directives. Doesn’t handle the directives: [create your own plugin][create-plugin] to do that. -See the [micromark extension for the syntax][syntax] and the -[mdast utility for the syntax tree][syntax-tree]. + +## Examples + +### Example: YouTube + +This example shows how directives can be used for YouTube embeds. +It’s based on the example in Use above. +If `myRemarkPlugin` was replaced with this function: + +```js +// This plugin is an example to turn `::youtube` into iframes. +/** @type {import('unified').Plugin<[], import('mdast').Root>} */ +function myRemarkPlugin() { + return (tree, file) => { + visit(tree, (node) => { + if ( + node.type === 'textDirective' || + node.type === 'leafDirective' || + node.type === 'containerDirective' + ) { + if (node.name !== 'youtube') return + + const data = node.data || (node.data = {}) + const attributes = node.attributes || {} + const id = attributes.id + + if (node.type === 'textDirective') file.fail('Text directives for `youtube` not supported', node) + if (!id) file.fail('Missing video id', node) + + data.hName = 'iframe' + data.hProperties = { + src: 'https://www.youtube.com/embed/' + id, + width: 200, + height: 200, + frameBorder: 0, + allow: 'picture-in-picture', + allowFullScreen: true + } + } + }) + } +} +``` + +…and `example.md` contains: + +```markdown +# Cat videos + +::youtube[Video of a cat in a box]{#01ab2cd3efg} +``` + +…then running `node example` yields: + +```html +
Cat videos
+ +``` + +### Example: Styled blocks + +Note: This is sometimes called admonitions, callouts, etc. + +This example shows how directives can be used to style blocks. +It’s based on the example in Use above. +If `myRemarkPlugin` was replaced with this function: + +```js +// This plugin is an example to turn `::note` into divs, passing arbitrary +// attributes. +/** @type {import('unified').Plugin<[], import('mdast').Root>} */ +function myRemarkPlugin() { + return (tree) => { + visit(tree, (node) => { + if ( + node.type === 'textDirective' || + node.type === 'leafDirective' || + node.type === 'containerDirective' + ) { + if (node.name !== 'note') return + + const data = node.data || (node.data = {}) + const tagName = node.type === 'textDirective' ? 'span' : 'div' + + data.hName = tagName + data.hProperties = h(tagName, node.attributes).properties + } + }) + } +} +``` + +…and `example.md` contains: + +```markdown +# How to use xxx + +You can use xxx. + +:::note{.warning} +if you chose xxx, you should also use yyy somewhere… +::: +``` + +…then running `node example` yields: + +```html +How to use xxx
+You can use xxx.
+if you chose xxx, you should also use yyy somewhere…
+