Skip to content

Commit 6a59cbc

Browse files
fix: add id to the span tag as per json rte changes
1 parent e1df0b9 commit 6a59cbc

8 files changed

Lines changed: 108 additions & 88 deletions

File tree

__test__/json-to-html.test.ts

Lines changed: 17 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import {
2020
plainTextJson,
2121
styleinPJson,
2222
tableJson,
23-
unorderListJson} from './mock/json-element-mock'
23+
unorderListJson,
24+
entryJsonRteWithClass,
25+
entryJsonRteWithId} from './mock/json-element-mock'
2426
import {
2527
blockquoteHtml,
2628
codeHtml,
@@ -38,7 +40,9 @@ import {
3840
plainTextHtml,
3941
styleinPHtml,
4042
tableHtml,
41-
unorderListHtml} from './mock/json-element-mock-result'
43+
unorderListHtml,
44+
plainTextHtmlWithClass,
45+
plainTextHtmlWithId } from './mock/json-element-mock-result'
4246
describe('Node parser paragraph content', () => {
4347
it('Should accept proper values', done => {
4448
const entry = { uid: 'uid'}
@@ -171,85 +175,17 @@ describe('Node parse text Content', () => {
171175
done()
172176
})
173177
it('Should return html text with classname', 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": {class: "class_p"},
186-
"uid": "",
187-
"children": [
188-
{
189-
"text": "abc",
190-
"classname": "yellow",
191-
},
192-
{
193-
"text": "def",
194-
"italic": false,
195-
"underline": true,
196-
"classname": "red",
197-
198-
},
199-
{
200-
"text": "fed",
201-
"italic": true,
202-
"underline": true,
203-
"classname": "green",
204-
205-
},
206-
{
207-
"text": "ghi",
208-
"underline": true,
209-
"classname": "blue",
210-
},
211-
{
212-
"text": "Basic",
213-
"classname": "orange",
214-
"id": "blue",
215-
"bold": true,
216-
"underline": true,
217-
"italic": true
218-
},
219-
{
220-
"text": "data",
221-
"classname": "brown",
222-
"id": "blue",
223-
"bold": false,
224-
"underline": true,
225-
"italic": true
226-
},
227-
{
228-
"type": "h1",
229-
"attrs": {},
230-
"uid": "",
231-
"children": [
232-
{
233-
"text": "abc",
234-
"classname": "purple",
235-
},
236-
{
237-
"text": "def",
238-
"italic": false,
239-
"underline": true,
240-
"classname": "white",
241-
}
242-
]
243-
}
244-
]
245-
}
246-
],
247-
"_version": 3
248-
}
249-
}
250-
const paths = ['json_rte', 'children']
251-
const response = jsonToHTML({ entry: entry, paths })
252-
console.log("🚀 ~ file: json-to-html.test.ts:193 ~ describe ~ entry.json_rte:", entry)
178+
const entry = entryJsonRteWithClass
179+
const paths = ['json_rte']
180+
jsonToHTML({ entry: entry, paths })
181+
expect(entry.json_rte).toEqual(plainTextHtmlWithClass)
182+
done()
183+
})
184+
it('Should return html text with id', done => {
185+
const entry = entryJsonRteWithId
186+
const paths = ['json_rte']
187+
jsonToHTML({ entry: entry, paths })
188+
expect(entry.json_rte).toEqual(plainTextHtmlWithId)
253189
done()
254190
})
255191
})

__test__/mock/json-element-mock-result.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const codeHtml = "<code>Code template.</code>"
1515
const linkInPHtml = "<strong><em><u><sub></sub></u></em></strong><a href=\"LINK.com\">LINK</a>"
1616
const linkInPURLHtml = "<strong><em><u><sub></sub></u></em></strong><a href=\"LINK.com\" target=\"_blank\">LINK</a>"
1717
const styleinPHtml = "<p style=\"text-align:right;\">This is <span>second</span> <u>JSON</u> entry</p><p style=\"text-align:left;\"><a href=\"example.com\" target=\"_blank\">Example</a></p>"
18+
const plainTextHtmlWithClass = "<p class=\"class_p\"><span class=\"yellow\">abc</span><span class=\"blue\"><u>ghi</u></span><span class=\"orange\" id=\"blue\"><strong><em><u>Basic</u></em></strong></span><h1><span class=\"purple\">abc</span></h1></p>"
19+
const plainTextHtmlWithId = "<p class=\"class_p\"><span id=\"blue\"><em><u>data</u></em></span></p>"
1820

1921
export {
2022
h1Html,
@@ -34,4 +36,6 @@ export {
3436
blockquoteHtml,
3537
unorderListHtml,
3638
styleinPHtml,
39+
plainTextHtmlWithClass,
40+
plainTextHtmlWithId
3741
}

__test__/mock/json-element-mock.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,83 @@ const embeddedEntryJsonEntry = {
10641064
}
10651065
}
10661066

1067+
const entryJsonRteWithClass = {
1068+
"uid": "",
1069+
"_version": 3,
1070+
"locale": "en-us",
1071+
"json_rte": {
1072+
"type": "doc",
1073+
"attrs": {},
1074+
"uid": "",
1075+
"children": [
1076+
{
1077+
"type": "p",
1078+
"attrs": {class: "class_p"},
1079+
"uid": "",
1080+
"children": [
1081+
{
1082+
"text": "abc",
1083+
"classname": "yellow",
1084+
},
1085+
{
1086+
"text": "ghi",
1087+
"underline": true,
1088+
"classname": "blue",
1089+
},
1090+
{
1091+
"text": "Basic",
1092+
"classname": "orange",
1093+
"id": "blue",
1094+
"bold": true,
1095+
"underline": true,
1096+
"italic": true
1097+
},
1098+
{
1099+
"type": "h1",
1100+
"attrs": {},
1101+
"uid": "",
1102+
"children": [
1103+
{
1104+
"text": "abc",
1105+
"classname": "purple",
1106+
}
1107+
]
1108+
}
1109+
]
1110+
}
1111+
],
1112+
"_version": 3
1113+
}
1114+
}
1115+
1116+
const entryJsonRteWithId = {
1117+
"uid": "",
1118+
"_version": 3,
1119+
"locale": "en-us",
1120+
"json_rte": {
1121+
"type": "doc",
1122+
"attrs": {},
1123+
"uid": "",
1124+
"children": [
1125+
{
1126+
"type": "p",
1127+
"attrs": {class: "class_p"},
1128+
"uid": "",
1129+
"children": [
1130+
{
1131+
"text": "data",
1132+
"id": "blue",
1133+
"bold": false,
1134+
"underline": true,
1135+
"italic": true
1136+
}
1137+
]
1138+
}
1139+
],
1140+
"_version": 3
1141+
}
1142+
}
1143+
10671144
export {
10681145
h1Json,
10691146
h2Json,
@@ -1090,5 +1167,7 @@ export {
10901167
embeddedEntryJsonEntry,
10911168
entryReferenceLinkJson,
10921169
entryReferenceBlockJson,
1093-
entryReferenceInlineJson
1170+
entryReferenceInlineJson,
1171+
entryJsonRteWithClass,
1172+
entryJsonRteWithId
10941173
}

src/helper/enumerate-entries.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export function textNodeToHTML(node: TextNode, renderOption: RenderOption): stri
6767
if (node.bold) {
6868
text = (renderOption[MarkType.BOLD] as RenderMark)(text)
6969
}
70-
if (node.classname) {
71-
text = (renderOption[MarkType.CLASSNAME] as RenderMark)(text, node.classname)
70+
if (node.classname || node.id) {
71+
text = (renderOption[MarkType.CLASSNAME_OR_ID] as RenderMark)(text, node.classname, node.id)
7272
}
7373
return text
7474
}

src/nodes/mark-type.ts

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

77
STRIKE_THROUGH = 'strikethrough',
88
INLINE_CODE = 'inlineCode',

src/nodes/text-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class TextNode extends Node {
1010
subscript?: boolean
1111
break?: boolean
1212
classname?: string
13+
id?: string
1314

1415
text: string
1516

src/options/default-node-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export const defaultNodeOption: RenderOption = {
114114
[MarkType.BREAK]:(text: string) => {
115115
return `<br />${text}`
116116
},
117-
[MarkType.CLASSNAME]:(text: string, classname: string) => {
118-
return `<span class="${classname}">${text}</span>`
117+
[MarkType.CLASSNAME_OR_ID]:(text: string, classname: string, id:string) => {
118+
return `<span${classname ? ` class="${classname}"` : ``}${id ? ` id="${id}"` : ``}>${text}</span>`
119119
}
120120

121121
}

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, classname?: string) => string;
9+
export type RenderMark = (text: string, classname?: string, id?: 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)