From ad6b6a65c7bd4d5dc2fb43658be80c54c2756a3b Mon Sep 17 00:00:00 2001 From: d3m1d0v Date: Tue, 12 Nov 2024 16:46:41 +0300 Subject: [PATCH] docs: added example of register directive using config-object --- README.md | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 907000f..b9722a9 100644 --- a/README.md +++ b/README.md @@ -10,17 +10,18 @@ Add new MarkdownIt-plugin or transformer extension, that plug-in a directive par ```ts import type MarkdownIt from 'markdown-it'; -import {directiveParser, registerBlockDirective} from '@diplodoc/directive'; +import {directiveParser, registerContainerDirective} from '@diplodoc/directive'; export function simpleBlockPlugin(): MarkdownIt.PluginSimple { return (md) => { md.use(directiveParser()); - registerBlockDirective(md, 'block', (state, params) => { + // register container directive using handler + registerContainerDirective(md, 'block', (state, params) => { if (!params.content) return false; let token = state.push('simple_block_open', 'div', 1); - token.attrSet('class', 'simple_block'); + token.attrSet('class', 'simple-block'); tokenizeBlockContent(state, params.content); @@ -28,6 +29,22 @@ export function simpleBlockPlugin(): MarkdownIt.PluginSimple { return true; }); + + // or using config-object + registerContainerDirective(md, { + name: 'block', + match(_params, state) { + // here you can add something to state.env + return true; + }, + container: { + tag: 'div', + token: 'simple_block', + attrs: { + class: 'simple-block', + }, + }, + }); }; } ``` @@ -59,7 +76,7 @@ const html = md.render(markup); `html` variable will have the value: ```html -
+

Heading 3 inside a simple block

``` @@ -119,7 +136,7 @@ All of parameters groups – `[]`, `()`, `{}` – are optional, but their order ): void; ``` -- `registerContainerDirective()` – register handler for new container block. +- `registerContainerDirective()` – register handler for new container block or configure it using config-object. ```ts function registerContainerDirective(md: MarkdownIt, config: ContainerDirectiveConfig): void; function registerContainerDirective(