Skip to content

Commit

Permalink
fix: error when using uikit in server code (#870)
Browse files Browse the repository at this point in the history
  • Loading branch information
smsochneg authored Mar 20, 2024
1 parent 44ffb79 commit 3ce00f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/text-transform/common.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import defaultPlugins from '@diplodoc/transform/lib/plugins';
import {MarkdownItPluginCb} from '@diplodoc/transform/lib/plugins/typings';
import {Lang} from '@gravity-ui/uikit';

import {Lang} from './types';
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}`,
lang: Lang,
content: string,
options: {plugins: MarkdownItPluginCb[]},
) => string;
Expand Down Expand Up @@ -37,7 +37,7 @@ export const createItemsParser = (fields: string[]) => (transformer: Transformer
});

export function yfmTransformer(
lang: `${Lang}`,
lang: Lang,
content: string,
options: {plugins?: MarkdownItPluginCb[]} = {},
) {
Expand All @@ -50,6 +50,6 @@ export function yfmTransformer(
return html;
}

export function typografTransformer(lang: `${Lang}`, content: string) {
export function typografTransformer(lang: Lang, content: string) {
return typografToHTML(content, lang);
}
8 changes: 4 additions & 4 deletions src/text-transform/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-not-accumulator-reassign/no-not-accumulator-reassign */
import {MarkdownItPluginCb} from '@diplodoc/transform/lib/plugins/typings';
import {Lang} from '@gravity-ui/uikit';
import cloneDeep from 'lodash/cloneDeep';
import shuffle from 'lodash/shuffle';

Expand All @@ -10,14 +9,15 @@ import {ConstructorBlock, PageContent} from '../models/constructor';
import {Transformer} from './common';
import {BlocksConfig, config} from './config';
import {FilterableContent, filterContent} from './filter';
import {Lang} from './types';

export type ContentVariables = Record<string, string>;
export type ContentTransformerProps = {
content: {
blocks?: ConstructorBlock[];
};
options: {
lang: `${Lang}`;
lang: Lang;
customConfig?: {};
vars?: ContentVariables;
plugins?: MarkdownItPluginCb[];
Expand All @@ -26,7 +26,7 @@ export type ContentTransformerProps = {

function transformBlocks(
blocks: ConstructorBlock[],
lang: `${Lang}`,
lang: Lang,
customConfig = {},
options: {plugins?: MarkdownItPluginCb[]} = {},
) {
Expand All @@ -39,7 +39,7 @@ function transformBlocks(
}

function transformBlock(
lang: `${Lang}`,
lang: Lang,
blocksConfig: BlocksConfig,
block: ConstructorBlock,
plugins: MarkdownItPluginCb[],
Expand Down
7 changes: 7 additions & 0 deletions src/text-transform/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// importing Lang as a type so that uikit doesn't get into js code built for server
import type {Lang} from '@gravity-ui/uikit';

// getting values of Lang enum so we can use them later to proper typechecking
type LANG = `${Lang}`; // 'ru' || 'en'

export {LANG as Lang};
19 changes: 10 additions & 9 deletions src/text-transform/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import transformYFM, {Options, Output} from '@diplodoc/transform';
import {Lang} from '@gravity-ui/uikit';
import sanitize from 'sanitize-html';
import Typograf from 'typograf';

import {Lang} from './types';

import AddRuleOptions = typograf.AddRuleOptions;

export enum TransformType {
Expand All @@ -11,7 +12,7 @@ export enum TransformType {
}

interface TransformOptions extends Options {
lang: `${Lang}`;
lang: Lang;
}

export const DEFAULT_ALLOWED_TAGS = [
Expand Down Expand Up @@ -54,10 +55,10 @@ function enableRules(tp: typograf.Typograf) {
disabled.forEach((rule) => tp.disableRule(rule));
}

export function typograf(text: string, lang: `${Lang}` = Lang.Ru) {
export function typograf(text: string, lang: Lang = 'ru') {
const localeByLang = {
[Lang.Ru]: ['ru', 'en-US'],
[Lang.En]: ['en-US', 'ru'],
ru: ['ru', 'en-US'],
en: ['en-US', 'ru'],
};

const tp = new Typograf({
Expand All @@ -74,11 +75,11 @@ export function sanitizeHtml(html: string, options = sanitizeStripOptions) {
return html && sanitize(html, options || sanitizeStripOptions);
}

export function typografToHTML(text: string, lang: `${Lang}`, allowedTags = DEFAULT_ALLOWED_TAGS) {
export function typografToHTML(text: string, lang: Lang, allowedTags = DEFAULT_ALLOWED_TAGS) {
return text && typograf(sanitizeHtml(text, {allowedTags}), lang);
}

export function typografToText(text: string, lang: `${Lang}`) {
export function typografToText(text: string, lang: Lang) {
return text && sanitizeHtml(typograf(text, lang));
}

Expand All @@ -104,14 +105,14 @@ export function fullTransform(
export interface TypografEntityParams {
entity: Record<string, string>;
fields: string[];
lang: `${Lang}`;
lang: Lang;
transformType: TransformType;
}

export function typografEntity({
entity,
fields,
lang = Lang.Ru,
lang = 'ru',
transformType = TransformType.Text,
}: TypografEntityParams) {
const transformTypeMap = {
Expand Down

0 comments on commit 3ce00f7

Please sign in to comment.