-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdefault-node-options.ts
More file actions
177 lines (168 loc) · 11.3 KB
/
default-node-options.ts
File metadata and controls
177 lines (168 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Next, RenderOption } from ".";
import MarkType from "../nodes/mark-type";
import Node from "../nodes/node";
import NodeType from "../nodes/node-type";
import { sanitizeHTML } from "../helper/sanitize";
export const defaultNodeOption: RenderOption = {
[NodeType.DOCUMENT]:(node: Node) => {
return ``
},
[NodeType.PARAGRAPH]:(node: Node, next: Next) => {
return `<p${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</p>`
},
[NodeType.LINK]:(node: Node, next: Next) => {
const sanitizedHref = sanitizeHTML(node.attrs.href || node.attrs.url);
if (node.attrs.target) {
return `<a${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} href="${sanitizedHref}" target="${node.attrs.target}">${sanitizeHTML(next(node.children))}</a>`
}
return `<a${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} href="${sanitizedHref}">${sanitizeHTML(next(node.children))}</a>`
},
[NodeType.IMAGE]:(node: Node, next: Next) => {
const sanitizedSrc = sanitizeHTML(node.attrs.src || node.attrs.url);
return `<img${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} src="${sanitizedSrc}" />${sanitizeHTML(next(node.children))}`
},
[NodeType.EMBED]:(node: Node, next: Next) => {
const sanitizedSrc = sanitizeHTML(node.attrs.src || node.attrs.url);
return `<iframe${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``} src="${sanitizedSrc}">${sanitizeHTML(next(node.children))}</iframe>`
},
[NodeType.HEADING_1]:(node: Node, next: Next) => {
return `<h1${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h1>`
},
[NodeType.HEADING_2]:(node: Node, next: Next) => {
return `<h2${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h2>`
},
[NodeType.HEADING_3]:(node: Node, next: Next) => {
return `<h3${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h3>`
},
[NodeType.HEADING_4]:(node: Node, next: Next) => {
return `<h4${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h4>`
},
[NodeType.HEADING_5]:(node: Node, next: Next) => {
return `<h5${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h5>`
},
[NodeType.HEADING_6]:(node: Node, next: Next) => {
return `<h6${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</h6>`
},
[NodeType.ORDER_LIST]:(node: Node, next: Next) => {
return `<ol${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</ol>`
},
[NodeType.FRAGMENT]:(node: Node, next: Next) => {
return `<fragment>${sanitizeHTML(next(node.children))}</fragment>`
},
[NodeType.UNORDER_LIST]:(node: Node, next: Next) => {
return `<ul${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</ul>`
},
[NodeType.LIST_ITEM]:(node: Node, next: Next) => {
return `<li${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</li>`
},
[NodeType.HR]:(node: Node, next: Next) => {
return `<hr>`
},
[NodeType.TABLE]: (node: Node, next: Next) => {
// Generate colgroup if colWidths attribute is present
let colgroupHTML = '';
if (node.attrs.colWidths && Array.isArray(node.attrs.colWidths)) {
const totalWidth = node.attrs.colWidths.reduce((sum, width) => sum + width, 0);
colgroupHTML = `<${NodeType.COL_GROUP} data-width="${totalWidth}">`;
node.attrs.colWidths.forEach(colWidth => {
const widthPercentage = (colWidth / totalWidth) * 100;
colgroupHTML += `<${NodeType.COL} style="width:${widthPercentage.toFixed(2)}%"/>`;
});
colgroupHTML += `</${NodeType.COL_GROUP}>`;
}
// Generate table with colgroup and other attributes
return `<table${node.attrs.style ? ` style="${node.attrs.style}"` : ``}` +
`${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}` +
`${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>` +
`${colgroupHTML}` +
`${sanitizeHTML(next(node.children))}` +
`</table>`;
},
[NodeType.TABLE_HEADER]:(node: Node, next: Next) => {
return `<thead${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</thead>`
},
[NodeType.TABLE_BODY]:(node: Node, next: Next) => {
return `<tbody${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</tbody>`
},
[NodeType.TABLE_FOOTER]:(node: Node, next: Next) => {
return `<tfoot${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</tfoot>`
},
[NodeType.TABLE_ROW]:(node: Node, next: Next) => {
return `<tr${node.attrs.style ? ` style="${node.attrs.style}"` : ``}${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</tr>`
},
[NodeType.TABLE_HEAD]:(node: Node, next: Next) => {
if (node.attrs.void) return '';
return `<th` +
`${node.attrs.rowSpan ? ` rowspan="${node.attrs.rowSpan}"` : ``}` +
`${node.attrs.colSpan ? ` colspan="${node.attrs.colSpan}"` : ``}` +
`${node.attrs.style ? ` style="${node.attrs.style}"` : ``}`+
`${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}`+
`${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}` +
`</th>`
},
[NodeType.TABLE_DATA]:(node: Node, next: Next) => {
if (node.attrs.void) return '';
return `<td` +
`${node.attrs.rowSpan ? ` rowspan="${node.attrs.rowSpan}"` : ``}` +
`${node.attrs.colSpan ? ` colspan="${node.attrs.colSpan}"` : ``}` +
`${node.attrs.style ? ` style="${node.attrs.style}"` : ``}`+
`${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}`+
`${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}` +
`</td>`
},
[NodeType.BLOCK_QUOTE]:(node: Node, next: Next) => {
return `<blockquote${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</blockquote>`
},
[NodeType.CODE]:(node: Node, next: Next) => {
return `<code${node.attrs['class-name'] ? ` class="${node.attrs['class-name']}"` : ``}${node.attrs.id ? ` id="${node.attrs.id}"` : ``}>${sanitizeHTML(next(node.children))}</code>`
},
['reference']:(node: Node, next: Next) => {
if (node.attrs.type === 'asset') {
const src = node.attrs['asset-link'];
const alt = node.attrs?.['redactor-attributes']?.['alt'];
const link = node.attrs.link;
const target = node.attrs.target || "";
const caption = node.attrs?.['redactor-attributes']?.['asset-caption'] || node.attrs?.['asset-caption'] || "";
const style = node.attrs.style;
const asset_uid= node.attrs['asset-uid'];
let imageTag = `<img${asset_uid ? ` asset_uid="${asset_uid}"` : `` }${node.attrs['class-name'] ? ` class="${sanitizeHTML(node.attrs['class-name'])}"`: ``}${src ? ` src="${sanitizeHTML(src)}"` : ``}${alt ? ` alt="${alt}"` : `` }${target === "_blank" ? ` target="_blank"` : `` }${style ? ` style="${style}"` : `` } />`;
return `<figure${style ? ` style="${style}"` : ''}>` +
(link ? `<a href="${link}" target="${target || ""}">` : "") +
imageTag +
(link ? `</a>` : "") +
(caption ? `<figcaption>${caption}</figcaption>` : "") +
`</figure>`;
}
return ``
},
['default']:(node: Node, next: Next) => {
return sanitizeHTML(next(node.children))
},
[MarkType.BOLD]:(text: string) => {
return `<strong>${sanitizeHTML(text)}</strong>`
},
[MarkType.ITALIC]:(text: string) => {
return `<em>${sanitizeHTML(text)}</em>`
},
[MarkType.UNDERLINE]:(text: string) => {
return `<u>${sanitizeHTML(text)}</u>`
},
[MarkType.STRIKE_THROUGH]:(text: string) => {
return `<strike>${sanitizeHTML(text)}</strike>`
},
[MarkType.INLINE_CODE]:(text: string) => {
return `<span data-type='inlineCode'>${sanitizeHTML(text)}</span>`
},
[MarkType.SUBSCRIPT]:(text: string) => {
return `<sub>${sanitizeHTML(text)}</sub>`
},
[MarkType.SUPERSCRIPT]:(text: string) => {
return `<sup>${sanitizeHTML(text)}</sup>`
},
[MarkType.BREAK]:(text: string) => {
return `<br />${sanitizeHTML(text)}`
},
[MarkType.CLASSNAME_OR_ID]:(text: string, classname: string, id:string) => {
return `<span${classname ? ` class="${classname}"` : ``}${id ? ` id="${id}"` : ``}>${sanitizeHTML(text)}</span>`
}
}