-
Notifications
You must be signed in to change notification settings - Fork 7
/
markdown.js
105 lines (94 loc) · 2.54 KB
/
markdown.js
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
// custom markdown extensions
module.exports = exports = function renderer({
Marked,
_ID,
_parents,
_storeSet,
_store,
_nav,
_globalProp,
_relativeURL,
}) {
Marked.heading = (text, level) => {
let className = `title-h${level}`;
switch (level) {
case 1:
className += " title";
break;
case 2:
className += " sub-title";
break;
default:
break;
}
return `<h${level} id="${text
.toLowerCase()
.replace(/[^\w]+/g, "-")}" class='${className}'
>${text}</h${level}>\n`;
};
Marked.paragraph = (text) => {
switch (text) {
case "...":
return `<p class='text-p dots'>···</p>`;
default:
return `<p class='text-p'>${text}</p>`;
}
};
// example for adding a class
Marked.hr = () => {
return `<hr class="my-custom-class">\n`;
};
Marked.relativeLink = (src) => {
return _relativeURL(src, _ID);
};
// making all links relative
Marked.link = (href, title, text) => {
if (title === "disabled") {
return `<a href="mailto:${href}"${
title ? `title="${title}"` : ""
} class='disabled-link' rel="noopener">${text}</a>`;
}
if (title === "email") {
return `<a href="mailto:${href}"${
title ? `title="${title}"` : ""
} class='email-link' rel="noopener">${text}</a>`;
}
if (title === "blog") {
return `<a href="${_relativeURL(`blog/${href}`, _ID)}"${
title ? ` title="${title}"` : ""
} rel="noopener">${text}</a>`;
}
if (href.startsWith("http://") || href.startsWith("https://")) {
return `<a href="${href}"${
title ? `title="${title}"` : ""
} rel="noopener" target='_blank'>${text}</a>`;
}
return `<a href="${_relativeURL(href, _ID)}"${
title ? ` title="${title}"` : ""
} rel="noopener">${text}</a>`;
};
// making all images relative
Marked.image = (href, title, text) => {
let sourcePath = href;
if (
!sourcePath.startsWith("http://") &&
!sourcePath.startsWith("https://")
) {
sourcePath = _relativeURL(href, _ID);
}
let out = `<figure class='image'><img src="${sourcePath}" alt="${text}"`;
if (title) {
out += ` title="${title}"`;
}
out += "></figure>";
return out;
};
// making all html tags with paths relative
Marked.html = (html) => {
// for (const match of html.matchAll(/=\"(\/[^\"]*)\"/)) {
// html = html.replace(match[1], _relativeURL(match[1], _ID));
// }
return html;
};
return Marked;
};