Skip to content

Commit 336b8aa

Browse files
committed
h1...6 markdown
1 parent 12bddde commit 336b8aa

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

bundle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ const render = require('./src/Render.js');
88

99
loader(data => {
1010
render(data);
11-
});
11+
});
12+

examples/simple/css/main.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.section-1 h2 {
2+
color: darkviolet;
3+
}
4+
.content code {
5+
color: red;
6+
}

examples/simple/index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"img": "this is the ![logo](img/ok.png) and this is the [link](#)",
77
"menu": ["[Home](#section-1) [Section 1](#section-1) [Section 2](#section-2) [Page 1](/about)"],
8-
"title": "Title h1",
8+
"title": "# Title h1",
99
"subtitle": [
1010
"_It is a long **established fact** that a reader will be distracted by the readable content of a page when looking at its layout._"
1111
],
@@ -19,15 +19,15 @@
1919
"hello": "world",
2020
"coding": "this is the code ```{\n \"key\": \"If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.\",\n \"key2\": \"value\"\n}``` oia o code.",
2121
"section-1": {
22-
"title": "Subtitle h2 👋",
22+
"title": "## Subtitle h2 👋",
2323
"subtitle": "Subtitle",
2424
"description": "Paragraph ~~delete~~...",
2525
"more": "[Link →](#)",
2626
"code": "{\n \"key\": \"If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.\",\n \"key2\": \"value\"\n}",
2727
"num": 23,
2828
"bool": true,
2929
"subsection": {
30-
"title": "Subtitle h3",
30+
"title": "### Subtitle h3",
3131
"description": "Subsection [description](#) passages of Lorem Ipsum available, but the majority.",
3232
"img": "img/ok.png",
3333
"moreimg": [
@@ -43,7 +43,7 @@
4343
]
4444
},
4545
"section-2": {
46-
"title": "Subtitle h2",
46+
"title": "## Subtitle h2",
4747
"description": "This is the description...",
4848
"items": [
4949
{
@@ -68,5 +68,8 @@
6868
"thank you 🙏"
6969
]
7070
},
71-
"#render": "<html hidden><script src=/dist/render.js></script></html><noscript><style>html{display:block !important; white-space:pre}</style></noscript>"
71+
"#render": {
72+
"_": "<html hidden><script src=/dist/render.js></script></html><noscript><style>html{display:block !important; white-space:pre}</style></noscript>",
73+
"css": "css/main.css"
74+
}
7275
}

src/Render.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ function render(data) {
1111
el.create('title', {}, (data['#info'] && data['#info'].title) || (data['title']))
1212
]);
1313

14+
if (data['#render'] && data['#render'].css) {
15+
el.append(document.head, el.create('link', {rel: "stylesheet", href: data['#render'].css}) );
16+
}
17+
1418
const main = el.create('main', {});
1519
el.append(document.body, main);
20+
1621
renderItem(main, '', data, 0);
1722
}
1823

1924
function renderItem(parent, key, value, level) {
2025

2126
if (Array.isArray(value)) {
2227

23-
const section = el.create('div', {});
28+
const section = el.create('div', {class: key});
2429
el.append(parent, section);
2530

2631
value.forEach(v => {
@@ -29,18 +34,18 @@ function renderItem(parent, key, value, level) {
2934
el.append(section, sectionItem);
3035
renderItem(sectionItem, key, v, level);
3136
} else {
32-
renderItem(parent, key, v, level);
37+
renderItem(section, null, v, level);
3338
}
3439
})
3540

3641
} else if (typeof value == 'object') {
3742

3843
let p = parent;
3944
if (level == 1) {
40-
p = el.create('section', {});
45+
p = el.create('section', {class: key});
4146
el.append(parent, [ el.create('a', {name: key}, ""), p]);
4247
} else if (level > 1) {
43-
p = el.create('div', {});
48+
p = el.create('div', {class: key});
4449
el.append(parent, p);
4550
}
4651

@@ -53,11 +58,7 @@ function renderItem(parent, key, value, level) {
5358

5459
} else {
5560

56-
if (key == 'title') {
57-
el.append(parent, el.create(`h${level < 6 ? level : '6'}`, {innerHTML: textFormatting(value + "")}));
58-
} else {
59-
el.append(parent, el.create('p', {innerHTML: textFormatting(value + "")}));
60-
}
61+
el.append(parent, el.create('p', {class: key, innerHTML: textFormatting(value + "")}));
6162

6263
}
6364
}

src/TextFormatting.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function textFormatting(str) {
55
if (str === undefined) return "";
66
if (typeof str != 'string') return "";
77

8-
const regTop = /(\*\*?|__?|~~|[<>\r\n])|\[([^[]*)\]\(([^(]*)\)|!\[([^[]*)\]\(([^(]*)\)|`([^`]+)`|```([^`]+)```/g;
8+
const regTop = /(\*\*?|__?|~~|[<>\r\n])|\[([^[]*)\]\(([^(]*)\)|!\[([^[]*)\]\(([^(]*)\)|`([^`]+)`|```([^`]+)```|(#{1,6})\ (.*)/g;
99

1010
const closeTag = {};
1111
const renderTag = tag => {
@@ -18,7 +18,7 @@ function textFormatting(str) {
1818
}
1919
};
2020

21-
return str.replace(regTop, (m, cmd, linkName, linkUrl, imgAlt, imgPath, code, pre) => {
21+
return str.replace(regTop, (m, cmd, linkName, linkUrl, imgAlt, imgPath, code, pre, titleNum, title) => {
2222

2323
if (cmd) {
2424
if (cmd == '**' || cmd == '__') {
@@ -44,6 +44,8 @@ function textFormatting(str) {
4444
return `<code>${code}</code>`
4545
} else if (pre) {
4646
return `<pre>${pre}</pre>`
47+
} else if (title) {
48+
return `<h${titleNum.length}>${title}</h1>`
4749
} else {
4850
return "";
4951
}

0 commit comments

Comments
 (0)