Skip to content

Commit 35be550

Browse files
committed
refactor: 🚧 Json to html
Added Attributes to string formating
1 parent 0449900 commit 35be550

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/Models/metadata-model.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,25 @@ export function nodeToMetadata(attribute: Attributes, textNode: TextNode): Metad
5454
contentTypeUid: attribute["content-type-uid"],
5555
item: undefined
5656
}
57+
}
58+
59+
export function attributeToString( attributes: Attributes):string {
60+
let result = ''
61+
for (const key in attributes) {
62+
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
63+
let element = attributes[key];
64+
if (typeof element === 'object') {
65+
let elementString = ''
66+
for (const elementKey in element) {
67+
if (Object.prototype.hasOwnProperty.call(element, elementKey)) {
68+
const value = element[elementKey];
69+
elementString += `${elementKey}:${value}; `
70+
}
71+
}
72+
element = elementString
73+
}
74+
result += ` ${key}="${element}"`
75+
}
76+
}
77+
return result
5778
}

src/options/default-node-options.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Next, RenderOption } from ".";
2+
import { attributeToString } from "../Models/metadata-model";
23
import MarkType from "../nodes/mark-type";
34
import Node from "../nodes/node";
45
import NodeType from "../nodes/node-type";
@@ -8,73 +9,73 @@ export const defaultNodeOption: RenderOption = {
89
return ``
910
},
1011
[NodeType.PARAGRAPH]:(node: Node, next: Next) => {
11-
return `<p>${next(node.children)}</p>`
12+
return `<p${attributeToString(node.attrs)}>${next(node.children)}</p>`
1213
},
1314
[NodeType.LINK]:(node: Node, next: Next) => {
14-
return `<a>${next(node.children)}</a>`
15+
return `<a${attributeToString(node.attrs)}>${next(node.children)}</a>`
1516
},
1617
[NodeType.IMAGE]:(node: Node, next: Next) => {
17-
return `<img src="" />${next(node.children)}`
18+
return `<img${attributeToString(node.attrs)} />${next(node.children)}`
1819
},
1920
[NodeType.EMBED]:(node: Node, next: Next) => {
20-
return `<iframe>${next(node.children)}</iframe>`
21+
return `<iframe${attributeToString(node.attrs)}>${next(node.children)}</iframe>`
2122
},
2223
[NodeType.HEADING_1]:(node: Node, next: Next) => {
23-
return `<h1>${next(node.children)}</h1>`
24+
return `<h1${attributeToString(node.attrs)}>${next(node.children)}</h1>`
2425
},
2526
[NodeType.HEADING_2]:(node: Node, next: Next) => {
26-
return `<h2>${next(node.children)}</h2>`
27+
return `<h2${attributeToString(node.attrs)}>${next(node.children)}</h2>`
2728
},
2829
[NodeType.HEADING_3]:(node: Node, next: Next) => {
29-
return `<h3>${next(node.children)}</h3>`
30+
return `<h3${attributeToString(node.attrs)}>${next(node.children)}</h3>`
3031
},
3132
[NodeType.HEADING_4]:(node: Node, next: Next) => {
32-
return `<h4>${next(node.children)}</h4>`
33+
return `<h4${attributeToString(node.attrs)}>${next(node.children)}</h4>`
3334
},
3435
[NodeType.HEADING_5]:(node: Node, next: Next) => {
35-
return `<h5>${next(node.children)}</h5>`
36+
return `<h5${attributeToString(node.attrs)}>${next(node.children)}</h5>`
3637
},
3738
[NodeType.HEADING_6]:(node: Node, next: Next) => {
38-
return `<h6>${next(node.children)}</h6>`
39+
return `<h6${attributeToString(node.attrs)}>${next(node.children)}</h6>`
3940
},
4041
[NodeType.ORDER_LIST]:(node: Node, next: Next) => {
41-
return `<ol>${next(node.children)}</ol>`
42+
return `<ol${attributeToString(node.attrs)}>${next(node.children)}</ol>`
4243
},
4344
[NodeType.UNORDER_LIST]:(node: Node, next: Next) => {
44-
return `<ul>${next(node.children)}</ul>`
45+
return `<ul${attributeToString(node.attrs)}>${next(node.children)}</ul>`
4546
},
4647
[NodeType.LIST_ITEM]:(node: Node, next: Next) => {
47-
return `<li>${next(node.children)}</li>`
48+
return `<li${attributeToString(node.attrs)}>${next(node.children)}</li>`
4849
},
4950
[NodeType.HR]:(node: Node, next: Next) => {
5051
return `<hr>`
5152
},
5253
[NodeType.TABLE]:(node: Node, next: Next) => {
53-
return `<table>${next(node.children)}</table>`
54+
return `<table${attributeToString(node.attrs)}>${next(node.children)}</table>`
5455
},
5556
[NodeType.TABLE_HEADER]:(node: Node, next: Next) => {
56-
return `<thead>${next(node.children)}</thead>`
57+
return `<thead${attributeToString(node.attrs)}>${next(node.children)}</thead>`
5758
},
5859
[NodeType.TABLE_BODY]:(node: Node, next: Next) => {
59-
return `<tbody>${next(node.children)}</tbody>`
60+
return `<tbody${attributeToString(node.attrs)}>${next(node.children)}</tbody>`
6061
},
6162
[NodeType.TABLE_FOOTER]:(node: Node, next: Next) => {
62-
return `<tfoot>${next(node.children)}</tfoot>`
63+
return `<tfoot${attributeToString(node.attrs)}>${next(node.children)}</tfoot>`
6364
},
6465
[NodeType.TABLE_ROW]:(node: Node, next: Next) => {
65-
return `<tr>${next(node.children)}</tr>`
66+
return `<tr${attributeToString(node.attrs)}>${next(node.children)}</tr>`
6667
},
6768
[NodeType.TABLE_HEAD]:(node: Node, next: Next) => {
68-
return `<th>${next(node.children)}</th>`
69+
return `<th${attributeToString(node.attrs)}>${next(node.children)}</th>`
6970
},
7071
[NodeType.TABLE_DATA]:(node: Node, next: Next) => {
71-
return `<td>${next(node.children)}</td>`
72+
return `<td${attributeToString(node.attrs)}>${next(node.children)}</td>`
7273
},
7374
[NodeType.BLOCK_QUOTE]:(node: Node, next: Next) => {
74-
return `<blockquote>${next(node.children)}</blockquote>`
75+
return `<blockquote${attributeToString(node.attrs)}>${next(node.children)}</blockquote>`
7576
},
7677
[NodeType.CODE]:(node: Node, next: Next) => {
77-
return `<code>${next(node.children)}</code>`
78+
return `<code${attributeToString(node.attrs)}>${next(node.children)}</code>`
7879
},
7980

8081
['reference']:(node: Node, next: Next) => {

0 commit comments

Comments
 (0)