### Container Directive Examples Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Illustrates various examples of container directives, including simple notes and complex cards with attributes. ```markdown :::note Important information ::: :::card[Card Title]{class="highlight" id="my-card"} Content of the card ::: :::::container Outer container :::inner Inner container ::: ::::: ``` -------------------------------- ### Markdown for YouTube Directive Example Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md Example Markdown content demonstrating the usage of a `::youtube` directive with an ID. ```markdown # Cat videos ::youtube[Video of a cat in a box]{#01ab2cd3efg} ``` -------------------------------- ### Basic Setup for remark-directive Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/usage-patterns.md Add remark-directive to your unified processor to enable directive parsing. ```javascript import {remark} from 'remark' import remarkDirective from 'remark-directive' const processor = remark().use(remarkDirective) ``` -------------------------------- ### Text Directive Examples Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Shows examples of text directives for abbreviations, links, and citations within sentences. ```markdown This is a :abbr[HTML]{title="HyperText Markup Language"} acronym. See :link[the documentation]{href="#intro"} for more. A :cite[Smith04] reference. ``` -------------------------------- ### Shortcut Attributes Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Demonstrates the CSS selector shortcut syntax for `id` and `class` attributes. ```markdown ::directive{#myid.class1.class2} ``` -------------------------------- ### Directive Markdown Syntax Examples Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/README.md Demonstrates the syntax for container, leaf, and text directives in markdown. ```markdown Container directive: :::name[label]{attributes}...::: Leaf directive: ::name[label]{attributes} Text directive: :name[label]{attributes} ``` -------------------------------- ### Directive Label Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Illustrates the optional label component in square brackets for directives. ```markdown ::directive[This is the label] ^^^^^^^^^^^^^^^^^^^ This is the label ``` -------------------------------- ### Markdown for Styled Block Directive Example Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md Example Markdown content using a `:::note` directive with a class for styling. ```markdown # How to use xxx You can use xxx. :::note{.warning} if you chose xxx, you should also use yyy somewhere… ::: ``` -------------------------------- ### Mixed Attributes Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Combines shortcut attribute syntax with explicit key-value pairs. ```markdown ::directive{#myid.myclass data="value" required} ``` -------------------------------- ### Directive Components Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/README.md Illustrates the components of a leaf directive, including name, label, and attributes. ```markdown ::youtube[Cool video]{id="abc123" width="640"} ^^^^^^ ^^^^^^^^^^ ^^^^^^^^ name label attributes ``` -------------------------------- ### Leaf Directive Examples Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Provides examples of leaf directives such as horizontal rules, images, and videos with attributes. ```markdown ::hr ::image[Alt text]{src="image.png"} ::video{src="video.mp4" width="640" height="480"} ``` -------------------------------- ### Install remark-directive with npm Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md Install the remark-directive package using npm. This package is ESM only and requires Node.js version 16 or later. ```sh npm install remark-directive ``` -------------------------------- ### Container Directive Syntax Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/api-reference.md Illustrates the Markdown syntax for container directives, which wrap content and have opening and closing markers. ```markdown :::directiveName[label]{attributes} Content inside the container. ::: ``` -------------------------------- ### Directive Name Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Highlights the required directive name component within the syntax. ```markdown ::myDirective[label]{attrs} ^^^^^^^^^^^ This is the name ``` -------------------------------- ### Example MDAST Node for a Leaf Directive Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/api-reference.md Provides an example of the Abstract Syntax Tree (AST) node structure for a leaf directive, including its type, name, and attributes. ```javascript { type: 'leafDirective', name: 'youtube', attributes: { id: '01ab2cd3efg' } } ``` -------------------------------- ### Configure remark-directive Serialization Options Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/configuration.md Set serialization options when initializing remark-directive. This example shows how to configure all available options. ```javascript import {remark} from 'remark' import remarkDirective from 'remark-directive' // Configure serialization options const processor = remark() .use(remarkDirective, { collapseEmptyAttributes: true, preferShortcut: true, preferUnquoted: false, quoteSmart: false, quote: '"' }) ``` -------------------------------- ### HTML Output for YouTube Directive Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md The resulting HTML structure after processing the YouTube directive example. ```html

Cat videos

``` -------------------------------- ### Directive Attributes Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Shows the syntax for key-value pair attributes enclosed in curly braces. ```markdown ::directive{key="value" another="test"} ^^^^^^^^^^^^^^^^^^ These are attributes ``` -------------------------------- ### Text Directive Syntax Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/api-reference.md Demonstrates the Markdown syntax for text directives, which are inline directives within text. ```markdown This is a :directiveName[label]{attributes} in text. ``` -------------------------------- ### Quick Start: Processing Directives with remark-directive Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/README.md Sets up a remark processor with remark-directive and a custom handler to transform 'note' container directives into HTML divs with a specific class. Parses and transforms markdown content. ```javascript import {remark} from 'remark' import remarkDirective from 'remark-directive' import {visit} from 'unist-util-visit' // Plugin to transform directives function myDirectiveHandler() { return (tree) => { visit(tree, (node) => { if (node.type === 'containerDirective' && node.name === 'note') { node.data = node.data || {} node.data.hName = 'div' node.data.hProperties = {className: 'note'} } }) } } // Set up processor const processor = remark() .use(remarkDirective) .use(myDirectiveHandler) // Parse and transform const ast = processor.parse(':::note\nImportant!\n:::') const html = await processor.process(':::note\nImportant!\n:::') ``` -------------------------------- ### HTML Output for Styled Block Directive Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md The resulting HTML structure after processing the styled block directive example. ```html

How to use xxx

You can use xxx.

if you chose xxx, you should also use yyy somewhere…

``` -------------------------------- ### Leaf Directive Syntax Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/api-reference.md Shows the Markdown syntax for leaf directives, which are block-level directives without content. ```markdown ::directiveName[label]{attributes} ``` -------------------------------- ### Process Markdown with Directives using remark-directive Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md A JavaScript example using unified, remark-parse, remark-directive, remark-rehype, and rehype-stringify to process markdown with directives. It includes a custom remark plugin to transform directives into HAST nodes. ```js /** * @import {} from 'mdast-util-directive' * @import {} from 'mdast-util-to-hast' * @import {Root} from 'mdast' */ import {h} from 'hastscript' import rehypeFormat from 'rehype-format' import rehypeStringify from 'rehype-stringify' import remarkDirective from 'remark-directive' import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' import {read} from 'to-vfile' import {unified} from 'unified' import {visit} from 'unist-util-visit' const file = await unified() .use(remarkParse) .use(remarkDirective) .use(myRemarkPlugin) .use(remarkRehype) .use(rehypeFormat) .use(rehypeStringify) .process(await read('example.md')) console.log(String(file)) // This plugin is an example to let users write HTML with directives. // It’s informative but rather useless. // See below for others examples. function myRemarkPlugin() { /** * @param {Root} tree * Tree. * @returns {undefined} * Nothing. */ return function (tree) { visit(tree, function (node) { if ( node.type === 'containerDirective' || node.type === 'leafDirective' || node.type === 'textDirective' ) { const data = node.data || (node.data = {}) const hast = h(node.name, node.attributes || {}) data.hName = hast.tagName data.hProperties = hast.properties } }) } } ``` -------------------------------- ### Chain Multiple Directive Handlers Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/advanced-topics.md Organize directive handling by splitting different directive types into separate plugins. This example shows how to import and use multiple directive plugins in a remark processor. ```javascript // directives/youtube.js function youtube() { return (tree) => { visit(tree, 'leafDirective', (node) => { if (node.name !== 'youtube') return // Handle youtube directives }) } } // directives/image.js function image() { return (tree) => { visit(tree, 'leafDirective', (node) => { if (node.name !== 'image') return // Handle image directives }) } } // main.js import {remark} from 'remark' import remarkDirective from 'remark-directive' import youtube from './directives/youtube.js' import image from './directives/image.js' const processor = remark() .use(remarkDirective) .use(youtube) .use(image) // ... other plugins ``` -------------------------------- ### Attribute Escaping Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Shows how special characters in attributes are HTML-encoded during serialization. Ampersands are encoded as `&`. ```markdown ::directive{attr="Tom & Jerry"} ``` ```markdown ::directive{attr="Tom & Jerry"} ``` -------------------------------- ### Integrate remark-directive with remark-stringify Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/configuration.md Pass remark-directive serialization options through remark-stringify configuration. This example shows how to set `quote` and `preferShortcut`. ```javascript import {remark} from 'remark' import remarkDirective from 'remark-directive' import {remarkStringify} from 'remark-stringify' const processor = remark() .use(remarkDirective) .use(remarkStringify, { // These options are passed to directive serialization quote: '"', preferShortcut: true }) ``` -------------------------------- ### Markdown with Directives Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md Example markdown content demonstrating container, leaf, and text directives. These directives can be used to embed custom elements or content. ```markdown :::main{#readme} Lorem:br ipsum. ::hr{.red} A :i[lovely] language know as :abbr[HTML]{title="HyperText Markup Language"}. ::: ``` -------------------------------- ### JSDoc Type Import for JavaScript Projects Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/integration-and-dependencies.md Provides an example of how to import and use type definitions for remark-directive options in JavaScript projects using JSDoc comments. ```javascript /** * @import {Options} from 'remark-directive' * @import {Root} from 'mdast' */ /** * @type {Options} */ const options = { preferShortcut: true } ``` -------------------------------- ### Markdown to HTML Pipeline with Directive Handling Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/usage-patterns.md Integrate remark-directive into a full markdown-to-HTML processing pipeline. This example customizes how directives are transformed into HTML elements. ```javascript import {remark} from 'remark' import remarkDirective from 'remark-directive' import remarkRehype from 'remark-rehype' import rehypeFormat from 'rehype-format' import rehypeStringify from 'rehype-stringify' import {visit} from 'unist-util-visit' function handleDirectives() { return (tree) => { visit(tree, (node) => { if (node.type === 'containerDirective') { node.data = node.data || {} node.data.hName = 'section' node.data.hProperties = { className: node.name } } if (node.type === 'leafDirective') { if (node.name === 'linebreak') { node.data = node.data || {} node.data.hName = 'hr' } } if (node.type === 'textDirective') { if (node.name === 'highlight') { node.data = node.data || {} node.data.hName = 'mark' } } }) } } const processor = remark() .use(remarkDirective) .use(handleDirectives) .use(remarkRehype) .use(rehypeFormat) .use(rehypeStringify) const html = await processor.process(` # Title :::note This is important. ::: A :highlight[marked] word. ::hr `) console.log(String(html)) ``` -------------------------------- ### Text Directive Node Example Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/directive-syntax.md Represents a text directive node in the AST. It includes type, name, optional label, and attributes. ```javascript { type: 'textDirective', name: 'cite', label: 'Smith04', attributes: { year: '2004' } } ``` -------------------------------- ### Processed HTML Output Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md The resulting HTML after processing the markdown with directives using the provided JavaScript example. This shows how directives are transformed into HTML elements. ```html

Lorem
ipsum.


A lovely language know as HTML.

``` -------------------------------- ### TypeScript Configuration for Remark Directive Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/source-code-reference.md Configures TypeScript for the project, enabling JSDoc type checking, declaration file generation, strict type checking, and modern JavaScript/ESM targets. This setup is used for generating TypeScript definitions from JSDoc annotations. ```json { "compilerOptions": { "checkJs": true, // Type-check .js files with JSDoc "declaration": true, // Generate .d.ts files "emitDeclarationOnly": true, // Only emit types, not JS "strict": true, // Strict type checking "target": "es2022", // Modern JavaScript "module": "node16" // ESM with Node 16 support }, "include": ["**/*.js", "index.d.ts"] } ``` -------------------------------- ### Content Validation for Dangerous Directives Source: https://github.com/remarkjs/remark-directive/blob/main/_autodocs/integration-and-dependencies.md Example of a directive handler that rejects dangerous directives like 'script' and validates attributes to prevent security risks. ```javascript function safeDirectiveHandler() { return (tree, file) => { visit(tree, (node) => { if (node.type === 'leafDirective' && node.name === 'script') { // Reject dangerous directives file.fail('Script directives are not allowed', node) } // Validate attributes if (node.attributes?.onclick) { file.fail('JavaScript event handlers are not allowed', node) } }) } } ``` -------------------------------- ### Transform YouTube Directives to Iframes Source: https://github.com/remarkjs/remark-directive/blob/main/readme.md This JavaScript plugin transforms `::youtube` directives into `