Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: additional yfm plugins in contentTransformer #551

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionalPlugins: MarkdownItPluginCb[] => options: {plugins: MarkdownItPluginCb[]}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ready

) => 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
26 changes: 20 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[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionalPlugins -> options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

) {
const blockConfig = blocksConfig[block.type];

if (block) {
Expand All @@ -43,7 +55,9 @@ 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) =>
// eslint-disable-next-line no-useless-call
transformerRaw.call(null, lang, content, additionalPlugins);

if (fields) {
(fields as (keyof typeof block)[]).forEach((field) => {
Expand All @@ -69,12 +83,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