Skip to content

Commit

Permalink
feat: add ability to add external plugins for contentTransformer func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
kkmch committed Sep 11, 2023
1 parent ad5e125 commit 9b7d965
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
20 changes: 17 additions & 3 deletions src/text-transform/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
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';

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<T = any> = (transformer: Transformer, block: T) => T;

Expand All @@ -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;
}
Expand Down
25 changes: 19 additions & 6 deletions src/text-transform/transformers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down

0 comments on commit 9b7d965

Please sign in to comment.