@squiz/dx-common-lib 1.53.1-alpha.10 → 1.53.1-alpha.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,124 +0,0 @@
1
- import {
2
- BaseResolvedNodes,
3
- FORMATTED_TEXT_MODELS,
4
- FullyResolvedNodes,
5
- FullyResolvedNodesMap,
6
- // PAGE_CONTENTS_MODELS,
7
- resolveFormattedText,
8
- } from '@squiz/dx-json-schema-lib';
9
- import { InternalServerError } from '../error';
10
- import { ComponentNode } from 'packages/dx-json-schema-lib/src/formatted-text/v1/formattedText';
11
-
12
- type FormattedTextTag = FullyResolvedNodes & { type: 'tag' };
13
- type FormattedNodeFontProperties = FORMATTED_TEXT_MODELS.v1.FormattedNodeFontProperties;
14
-
15
- export const formattedTextToHtmlStringResolvers: FullyResolvedNodesMap = {
16
- tag: async (node) => buildTagHTML(node),
17
- text: async (node) => node.value,
18
- component: async (node) => {
19
- return buildComponentHtml(node);
20
- },
21
- default: async (node) => {
22
- return `<p class="invalid-node-type"> node of type "${node.type}" cannot be converted to html</p>`;
23
- },
24
- };
25
-
26
- export async function formattedTextToHtmlString(formattedText: BaseResolvedNodes): Promise<string> {
27
- const output = await resolveFormattedText(formattedText, formattedTextToHtmlStringResolvers);
28
- return output;
29
- }
30
-
31
- function buildTagHTML(formattedTextTag: FormattedTextTag): string {
32
- const classes = getClassString(formattedTextTag);
33
-
34
- if (!formattedTextTag.attributes) {
35
- formattedTextTag.attributes = { class: classes };
36
- } else {
37
- formattedTextTag.attributes.class = classes;
38
- }
39
-
40
- const attributesStr = buildAttributesString(formattedTextTag.attributes);
41
-
42
- return `<${formattedTextTag.tag}${attributesStr}>${formattedTextTag.children.join('')}</${formattedTextTag.tag}>`;
43
- }
44
-
45
- function buildComponentHtml(node: ComponentNode): string {
46
- const esiUrl = buildComponentRenderEsiUrl(node);
47
- const esiTag = `<esi:include src="${esiUrl}"/>`;
48
- return esiTag;
49
- }
50
-
51
- function getClassString(formattedTextTag: FormattedTextTag) {
52
- const fontClasses = buildFontClasses(formattedTextTag.font);
53
-
54
- let string = formattedTextTag?.attributes?.class ?? '';
55
-
56
- if (formattedTextTag?.formattingOptions?.alignment) {
57
- fontClasses.push(formattedTextTag.formattingOptions.alignment);
58
- }
59
-
60
- if (fontClasses.length > 0) {
61
- string += ' ' + fontClasses.join(' ');
62
- }
63
-
64
- return string.trim();
65
- }
66
-
67
- export function buildAttributesString(attributes: { [k: string]: string } | undefined): string {
68
- if (!attributes) {
69
- return '';
70
- }
71
-
72
- let attributesString = '';
73
- for (const attribute in attributes) {
74
- if (attributes[attribute] !== '') {
75
- attributesString += ` ${attribute}="${attributes[attribute]}"`;
76
- }
77
- }
78
-
79
- return attributesString;
80
- }
81
-
82
- export function buildFontClasses(font: FormattedNodeFontProperties | undefined): string[] {
83
- if (!font) {
84
- return [];
85
- }
86
-
87
- const classes: string[] = [];
88
- if (font.bold) {
89
- classes.push('bold');
90
- }
91
- if (font.underline) {
92
- classes.push('underline');
93
- }
94
- if (font.italics) {
95
- classes.push('italics');
96
- }
97
- if (font.color) {
98
- classes.push(`color-${font.color}`);
99
- }
100
- if (font.size) {
101
- classes.push(`size-${font.size}`);
102
- }
103
- if (font.fontFamily) {
104
- classes.push(`font-${font.fontFamily}`);
105
- }
106
-
107
- return classes;
108
- }
109
-
110
- function buildComponentRenderEsiUrl(node: ComponentNode): string {
111
- const BASE_DXP_PATH = '/__dxp/au/components-render';
112
-
113
- if (process.env.TENANT_ID === undefined) {
114
- throw new InternalServerError('component node resolution failed due to unknown tenant');
115
- }
116
- const tenantId = process.env.TENANT_ID;
117
-
118
- let query = `?_componentSet=${node.componentSet}`;
119
- if (node.contentItem?.id) {
120
- query = query.concat(`&_contentItemId=${node.contentItem.id}`);
121
- }
122
-
123
- return `${BASE_DXP_PATH}/${tenantId}/r/${node.componentId}${query}`;
124
- }