From 438bdde8a225d372b14f470729181b6618936cf0 Mon Sep 17 00:00:00 2001 From: Breno A Date: Sat, 24 Feb 2024 21:49:02 -0300 Subject: [PATCH] feat(md): custom containers support --- src/node/markdown/plugins/containers.ts | 50 +++++++++++-------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/node/markdown/plugins/containers.ts b/src/node/markdown/plugins/containers.ts index 5008850b..6fa86b73 100644 --- a/src/node/markdown/plugins/containers.ts +++ b/src/node/markdown/plugins/containers.ts @@ -14,34 +14,27 @@ export const containerPlugin = ( options: Options, containerOptions?: ContainerOptions ) => { - md.use(...createContainer('tip', containerOptions?.tipLabel || 'TIP', md)) - .use(...createContainer('info', containerOptions?.infoLabel || 'INFO', md)) - .use( - ...createContainer( - 'warning', - containerOptions?.warningLabel || 'WARNING', - md - ) - ) - .use( - ...createContainer( - 'danger', - containerOptions?.dangerLabel || 'DANGER', - md - ) - ) - .use( - ...createContainer( - 'details', - containerOptions?.detailsLabel || 'Details', - md - ) - ) - // explicitly escape Vue syntax - .use(container, 'v-pre', { - render: (tokens: Token[], idx: number) => - tokens[idx].nesting === 1 ? `
\n` : `
\n` - }) + const defaultContainers = { + tip: containerOptions?.tipLabel ?? 'TIP', + info: containerOptions?.infoLabel ?? 'INFO', + warning: containerOptions?.warningLabel ?? 'WARNING', + danger: containerOptions?.dangerLabel ?? 'DANGER', + details: containerOptions?.detailsLabel ?? 'Details' + } + const containers: Record = { + ...defaultContainers, + ...(containerOptions?.customContainers ?? {}) + } + + Object.entries(containers).forEach(([key, value]) => { + md.use(...createContainer(key, value, md)) + }) + + // explicitly escape Vue syntax + md.use(container, 'v-pre', { + render: (tokens: Token[], idx: number) => + tokens[idx].nesting === 1 ? `
\n` : `
\n` + }) .use(container, 'raw', { render: (tokens: Token[], idx: number) => tokens[idx].nesting === 1 ? `
\n` : `
\n` @@ -136,4 +129,5 @@ export interface ContainerOptions { detailsLabel?: string importantLabel?: string cautionLabel?: string + customContainers?: Record }