Skip to content

Commit

Permalink
fix: preprocess types (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
martyanovandrey authored Apr 15, 2024
1 parent c75a06a commit 6b21901
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions src/preprocess.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {Block, ConstructorBlock, Lang} from '@gravity-ui/page-constructor';
import {config, contentTransformer} from '@gravity-ui/page-constructor/server';
import {
Block,
ConstructorBlock,
NavigationData as ConstructorNavigaitonData,
PageContent as ConstructorPageContentBase,
} from '@gravity-ui/page-constructor';
import {
BlocksConfig,
Parser,
TransformerRaw,
config,
contentTransformer,
} from '@gravity-ui/page-constructor/server';

export interface MetaData {
title: string;
Expand All @@ -10,22 +21,37 @@ export interface PageContentBase {
meta?: MetaData;
}

export enum Lang {
RU = 'ru',
EN = 'en',
}

export type PageContent<T> = T & PageContentBase;
export type ConstructorPageContent = PageContent<ConstructorPageContentBase>;
export type NavigationData = PageContent<ConstructorNavigaitonData>;
export type ConfigData = ConstructorPageContent | NavigationData;

export interface PreloadParams {
locale: string;
lang: Lang;
pageName: string;
pageReferer?: string;
}

interface BlockConfig {
transformer: TransformerRaw;
fields?: string[];
parser?: Parser;
}

export function isPageConfig(config: ConfigData): config is ConstructorPageContent {
return 'blocks' in config;
}

export function preprocess(content: ConfigData, params: PreloadParams, customYfmTransformer) {
export function preprocess(
content: ConfigData,
params: PreloadParams,
customYfmTransformer: TransformerRaw,
) {
const {lang} = params;

if (isPageConfig(content) && content.blocks) {
Expand All @@ -38,19 +64,32 @@ export function preprocess(content: ConfigData, params: PreloadParams, customYfm
return content;
}

function replaceTransformer(config: BlocksConfig, newTransformer: Function): BlocksConfig {
return Object.keys(config).reduce((newConfig, key) => {
const subBlockType = key as SubBlockType;
newConfig[subBlockType] = config[subBlockType].map((block) => {
return block.transformer.name === 'yfmTransformer'
? {...block, transformer: newTransformer}
: block;
});
function replaceTransformer(config: BlocksConfig, newTransformer: TransformerRaw): BlocksConfig {
return Object.keys(config).reduce((newConfig, subBlockType) => {
const subBlock = config[subBlockType];

if (Array.isArray(subBlock)) {
newConfig[subBlockType] = subBlock.map((block: {transformer: {name: string}}) => {
return block.transformer.name === 'yfmTransformer'
? {...block, transformer: newTransformer}
: block;
}) as BlockConfig[];
} else {
newConfig[subBlockType] =
subBlock.transformer.name === 'yfmTransformer'
? {...subBlock, transformer: newTransformer}
: subBlock;
}

return newConfig;
}, {} as BlocksConfig);
}

function transformBlocks(blocks: ConstructorBlock[], lang: Lang, customYfmTransformer) {
function transformBlocks(
blocks: ConstructorBlock[],
lang: Lang,
customYfmTransformer: TransformerRaw,
) {
const customConfig = replaceTransformer(config, customYfmTransformer);

return contentTransformer({
Expand Down

0 comments on commit 6b21901

Please sign in to comment.