Skip to content

Commit

Permalink
feat: transform yfm in PC (#43)
Browse files Browse the repository at this point in the history
* feat: transform yfm in PC

* lint

* misc
  • Loading branch information
martyanovandrey authored Apr 15, 2024
1 parent 4508c99 commit ec8f0d4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react';
import {renderToString} from 'react-dom/server';

import {App, DocInnerProps, DocLeadingPageData, DocPageData} from './components/App/App';
import {LINK_KEYS} from './constants';
import {LINK_KEYS, LINK_KEYS_LEADING_CONFIG, LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG} from './constants';
import {preprocess} from './preprocess';

export type {DocInnerProps, DocPageData, DocLeadingPageData};
export {LINK_KEYS};
export {LINK_KEYS, LINK_KEYS_LEADING_CONFIG, LINK_KEYS_PAGE_CONSTRUCTOR_CONFIG, preprocess};

export const render = (props: DocInnerProps) => renderToString(<App {...props} />);
63 changes: 63 additions & 0 deletions src/preprocess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {Block, ConstructorBlock, Lang} from '@gravity-ui/page-constructor';
import {config, contentTransformer} from '@gravity-ui/page-constructor/server';

export interface MetaData {
title: string;
description?: string;
}

export interface PageContentBase {
meta?: MetaData;
}

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;
pageName: string;
pageReferer?: string;
}

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

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

if (isPageConfig(content) && content.blocks) {
return {
...content,
blocks: transformBlocks(content.blocks, lang, customYfmTransformer),
};
}

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;
});
return newConfig;
}, {} as BlocksConfig);
}

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

return contentTransformer({
content: {blocks},
options: {
lang,
customConfig,
},
}).blocks as Block[];
}

0 comments on commit ec8f0d4

Please sign in to comment.