Skip to content

Commit f5070ad

Browse files
fix: adding classname in the span tag
1 parent 8c3ca17 commit f5070ad

6 files changed

Lines changed: 81 additions & 3 deletions

File tree

__test__/json-to-html.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,66 @@ describe('Node parse text Content', () => {
167167
const paths = ['supercharged_rte']
168168

169169
jsonToHTML({ entry, paths})
170-
171170
expect(entry.supercharged_rte).toEqual([plainTextHtml])
172171
done()
173172
})
173+
it('Should return bold text', done => {
174+
const entry = {
175+
"uid": "",
176+
"_version": 3,
177+
"locale": "en-us",
178+
"json_rte": {
179+
"type": "doc",
180+
"attrs": {},
181+
"uid": "",
182+
"children": [
183+
{
184+
"type": "p",
185+
"attrs": {},
186+
"uid": "",
187+
"children": [
188+
{
189+
"text": "abc",
190+
"classname": "yellow",
191+
},
192+
{
193+
"text": " "
194+
},
195+
{
196+
"text": "def",
197+
"italic": true
198+
},
199+
{
200+
"text": " "
201+
},
202+
{
203+
"text": "ghi",
204+
"underline": true,
205+
"classname": "blue",
206+
},
207+
{
208+
"text": " "
209+
},
210+
{
211+
"text": "Basic",
212+
"classname": "red",
213+
"id": "blue",
214+
bold: true
215+
},
216+
{
217+
"text": " InformationEntry Type IDabc"
218+
}
219+
]
220+
}
221+
],
222+
"_version": 3
223+
}
224+
}
225+
const paths = ['json_rte', 'children']
226+
const response = jsonToHTML({ entry: entry, paths })
227+
console.log("🚀 ~ file: json-to-html.test.ts:193 ~ describe ~ entry.json_rte:", entry)
228+
done()
229+
})
174230
})
175231

176232
describe('Node parse headers content', () => {

src/helper/enumerate-entries.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ export function textNodeToHTML(node: TextNode, renderOption: RenderOption): stri
6565
text = (renderOption[MarkType.ITALIC] as RenderMark)(text)
6666
}
6767
if (node.bold) {
68+
// if(node.classname) {
69+
// text = (renderOption[MarkType.BOLD] as RenderMark)(text, node.classname)
70+
// }
6871
text = (renderOption[MarkType.BOLD] as RenderMark)(text)
6972
}
73+
if (node.classname) {
74+
text = (renderOption[MarkType.CLASSNAME] as RenderMark)(text, node.classname)
75+
}
7076
return text
7177
}
7278
export function referenceToHTML(node: Node,

src/nodes/mark-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ enum MarkType {
22
BOLD = 'bold',
33
ITALIC = 'italic',
44
UNDERLINE = 'underline',
5+
CLASSNAME = 'classname',
56

67
STRIKE_THROUGH = 'strikethrough',
78
INLINE_CODE = 'inlineCode',

src/nodes/text-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class TextNode extends Node {
99
superscript?: boolean
1010
subscript?: boolean
1111
break?: boolean
12+
classname?: string
1213

1314
text: string
1415

src/options/default-node-options.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export const defaultNodeOption: RenderOption = {
1010
[NodeType.PARAGRAPH]:(node: Node, next: Next) => {
1111
return `<p${addStyleAttrs(node.attrs.style)}>${next(node.children)}</p>`
1212
},
13+
// [NodeType.PARAGRAPH]:(node: Node, next: Next) => {
14+
// return `<p${addStyleAttrs(node.attrs.style)}>${next(node.children)}</p>`
15+
// },
16+
// [NodeType.PARAGRAPH]: (node: Node, next: Next) => {
17+
// const child = node.children.find((child) => child.classname)
18+
// return `<p${addStyleAttrs(node.attrs.style)} class ="${child.classname}">${next([child])}</p>`;
19+
// },
1320
[NodeType.LINK]:(node: Node, next: Next) => {
1421
if (node.attrs.target) {
1522
return `<a${addStyleAttrs(node.attrs.style)} href="${node.attrs.href || node.attrs.url}" target="${node.attrs.target}">${next(node.children)}</a>`
@@ -90,7 +97,11 @@ export const defaultNodeOption: RenderOption = {
9097
return next(node.children)
9198
},
9299

93-
[MarkType.BOLD]:(text: string) => {
100+
[MarkType.BOLD]:(text: string, classname?: string) => {
101+
// if (classname) {
102+
// return `<strong class="${classname}">${text}</strong>`
103+
// }
104+
// else
94105
return `<strong>${text}</strong>`
95106
},
96107
[MarkType.ITALIC]:(text: string) => {
@@ -114,6 +125,9 @@ export const defaultNodeOption: RenderOption = {
114125
[MarkType.BREAK]:(text: string) => {
115126
return `<br />${text}`
116127
},
128+
[MarkType.CLASSNAME]:(text: string, classname: string) => {
129+
return `<span class="${classname}">${text}</span>`
130+
}
117131

118132
}
119133

src/options/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Node from '../nodes/node';
66

77
export type Next = (nodes: AnyNode[]) => string
88
export type RenderNode = (node: Node, next: Next) => string;
9-
export type RenderMark = (text: string) => string;
9+
export type RenderMark = (text: string, classname?: string) => string;
1010
export type RenderItem = (item: EmbeddedItem | EntryNode, metadata: Metadata) => string;
1111
export interface RenderOption {
1212
[embedType: string]: RenderNode | RenderMark | RenderItem | RenderContentType;

0 commit comments

Comments
 (0)