From 9b7d9650c1c9a2c6496ab31a18fcd4be6ccc41da Mon Sep 17 00:00:00 2001 From: kkmch Date: Mon, 11 Sep 2023 14:08:13 +0300 Subject: [PATCH] feat: add ability to add external plugins for contentTransformer function --- src/text-transform/common.ts | 20 +++++++++++++++++--- src/text-transform/transformers.ts | 25 +++++++++++++++++++------ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/text-transform/common.ts b/src/text-transform/common.ts index bad491d6b..2e9fb6ee5 100644 --- a/src/text-transform/common.ts +++ b/src/text-transform/common.ts @@ -1,3 +1,6 @@ +import defaultPlugins from '@doc-tools/transform/lib/plugins'; +import {MarkdownItPluginCb} from '@doc-tools/transform/lib/plugins/typings'; + import {Lang} from '../utils/configure'; import {fullTransform, typografToHTML} from './utils'; @@ -5,7 +8,11 @@ import {fullTransform, typografToHTML} from './utils'; export type ComplexItem = {[key: string]: string}; export type Item = string | null | ComplexItem; export type Transformer = (text: string) => string; -export type TransformerRaw = (lang: Lang, content: string) => string; +export type TransformerRaw = ( + lang: Lang, + content: string, + additionalPlugins: MarkdownItPluginCb[], +) => string; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type Parser = (transformer: Transformer, block: T) => T; @@ -30,8 +37,15 @@ export const createItemsParser = (fields: string[]) => (transformer: Transformer } }); -export function yfmTransformer(lang: Lang, content: string) { - const {html} = fullTransform(content, {lang}); +export function yfmTransformer( + lang: Lang, + content: string, + additionalPlugins: MarkdownItPluginCb[] = [], +) { + const {html} = fullTransform(content, { + lang, + plugins: [...defaultPlugins, ...additionalPlugins], + }); return html; } diff --git a/src/text-transform/transformers.ts b/src/text-transform/transformers.ts index 42657ee7a..8591fabac 100644 --- a/src/text-transform/transformers.ts +++ b/src/text-transform/transformers.ts @@ -1,5 +1,6 @@ /* eslint-disable no-param-reassign */ /* eslint-disable no-not-accumulator-reassign/no-not-accumulator-reassign */ +import {MarkdownItPluginCb} from '@doc-tools/transform/lib/plugins/typings'; import _ from 'lodash'; import {ConstructorBlock, PageContent} from '../models/constructor'; @@ -18,18 +19,29 @@ export type ContentTransformerProps = { lang: Lang; customConfig?: {}; vars?: ContentVariables; + additionalPlugins?: MarkdownItPluginCb[]; }; }; -function transformBlocks(blocks: ConstructorBlock[], lang: Lang, customConfig = {}) { +function transformBlocks( + blocks: ConstructorBlock[], + lang: Lang, + customConfig = {}, + additionalPlugins: MarkdownItPluginCb[] = [], +) { const fullConfig = {...config, ...customConfig}; const clonedBlocks = _.cloneDeep(blocks); - return clonedBlocks.map((block) => transformBlock(lang, fullConfig, block)); + return clonedBlocks.map((block) => transformBlock(lang, fullConfig, block, additionalPlugins)); } -function transformBlock(lang: Lang, blocksConfig: BlocksConfig, block: ConstructorBlock) { +function transformBlock( + lang: Lang, + blocksConfig: BlocksConfig, + block: ConstructorBlock, + additionalPlugins: MarkdownItPluginCb[], +) { const blockConfig = blocksConfig[block.type]; if (block) { @@ -43,7 +55,8 @@ function transformBlock(lang: Lang, blocksConfig: BlocksConfig, block: Construct configs.forEach((transformConfig) => { const {fields, transformer: transformerRaw, parser} = transformConfig; - const transformer: Transformer = transformerRaw.bind(null, lang); + const transformer: Transformer = (content) => + transformerRaw(lang, content, additionalPlugins); if (fields) { (fields as (keyof typeof block)[]).forEach((field) => { @@ -69,12 +82,12 @@ function transformBlock(lang: Lang, blocksConfig: BlocksConfig, block: Construct } export const contentTransformer = ({content, options}: ContentTransformerProps) => { - const {lang, customConfig = {}, vars} = options; + const {lang, customConfig = {}, vars, additionalPlugins = []} = options; const {blocks = []} = ( vars ? filterContent(content as FilterableContent, vars) : content ) as PageContent; - const transformedBlocks = transformBlocks(blocks, lang, customConfig); + const transformedBlocks = transformBlocks(blocks, lang, customConfig, additionalPlugins); return { blocks: transformedBlocks,