-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlibrary.js
190 lines (167 loc) · 8.49 KB
/
library.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
const slugify = require.main.require('./src/slugify');
const textHeaderRegex = /<p dir="auto">#([a-zA-Z0-9-]*)\((.*)\)<\/p>/g;
const tooltipRegex = /(<code.*>*?[^]<\/code>)|°(.*)°\((.*)\)/g;
const codeTabRegex = /(?:<p dir="auto">={3}group<\/p>\n)((?:<pre><code class=".+">[^]*?<\/code><\/pre>\n){2,})(?:<p dir="auto">={3}<\/p>)/g;
const langCodeRegex = /<code class="(.+)">/;
const colorRegex = /(<code.*>*?[^]<\/code>)|%\((#[\dA-Fa-f]{6}|rgb\(\d{1,3}, ?\d{1,3}, ?\d{1,3}\)|[a-z]+)\)\[(.+?)]/g;
const paragraphAndHeadingRegex = /<(h[1-6]|p dir="auto")>([^]*?)<\/(h[1-6]|p)>/g;
const noteRegex = /<p dir="auto">!!! (info|warning|important) \[([a-zA-Z0-9]*)\]: ((.|<br \/>\n)*)<\/p>/g;
const spoilerRegex = /(?:<p dir="auto">)(?:\|\|)([^]*?)(?:\|\|)(?:<\/p>)/g;
const noteIcons = {
info: 'fa-info-circle',
warning: 'fa-exclamation-triangle',
important: 'fa-exclamation-circle'
};
const ExtendedMarkdown = {
// post
async parsePost(data) {
if (data && data.postData && data.postData.content) {
data.postData.content = applyExtendedMarkdown(data.postData.content);
data.postData.content = applyGroupCode(data.postData.content, data.postData.pid);
data.postData.content = applySpoiler(data.postData.content, data.postData.pid);
}
return data;
},
// user signature
async parseSignature(data) {
if (data && data.userData && data.userData.signature) {
data.userData.signature = applyExtendedMarkdown(data.userData.signature);
}
return data;
},
// user description
async parseAboutMe(data) {
if (data) {
data = applyExtendedMarkdown(data);
}
return data;
},
// direct preview in editor
async parseRaw(data) {
if (data) {
data = applyExtendedMarkdown(data);
data = applyGroupCode(data, "");
data = applySpoiler(data, "");
}
return data;
},
async registerFormatting(payload) {
const formatting = [
{ name: "color", className: "fa fa-eyedropper", title: "[[extendedmarkdown:composer.formatting.color]]" },
{ name: "left", className: "fa fa-align-left", title: "[[extendedmarkdown:composer.formatting.left]]" },
{ name: "center", className: "fa fa-align-center", title: "[[extendedmarkdown:composer.formatting.center]]" },
{ name: "right", className: "fa fa-align-right", title: "[[extendedmarkdown:composer.formatting.right]]" },
{ name: "justify", className: "fa fa-align-justify", title: "[[extendedmarkdown:composer.formatting.justify]]" },
{ name: "textheader", className: "fa fa-header", title: "[[extendedmarkdown:composer.formatting.textheader]]" },
{ name: "groupedcode", className: "fa fa-file-code-o", title: "[[extendedmarkdown:composer.formatting.groupedcode]]" },
{ name: "bubbleinfo", className: "fa fa-info-circle", title: "[[extendedmarkdown:composer.formatting.bubbleinfo]]" },
{ name: "spoiler", className: "fa fa-eye-slash", title: "[[extendedmarkdown:composer.formatting.spoiler]]" }
];
payload.options = payload.options.concat(formatting);
return payload;
},
async sanitizerConfig(config) {
config.allowedAttributes['a'].push('name');
return config;
}
};
function applyExtendedMarkdown(textContent) {
if (textContent.match(noteRegex)) {
textContent = textContent.replace(noteRegex, function (match, type, title, text) {
return `<div class="admonition ${type.toLowerCase()}"><p class="admonition-title"><i class="fa ${noteIcons[type.toLowerCase()]}"></i>${title}</p><p>${text}</p></div>`;
});
}
if (textContent.match(textHeaderRegex)) {
textContent = textContent.replace(textHeaderRegex, function (match, anchorId, text) {
return `<h2 class="text-header"><a class="anchor-offset" name="${anchorId}"></a>${text}</h2>`;
});
}
if (textContent.match(tooltipRegex)) {
textContent = textContent.replace(tooltipRegex, function (match, code, text, tooltipText) {
if (typeof (code) !== "undefined") {
return code;
} else if ("fa-info" === text) {
return `<i class="fa fa-info-circle extended-markdown-tooltip" data-bs-toggle="tooltip" data-bs-placement="top" title="${tooltipText}"></i>`;
} else {
return `<span class="extended-markdown-tooltip" data-bs-toggle="tooltip" data-bs-placement="top" title="${tooltipText}">${text}</span>`;
}
});
}
if (textContent.match(colorRegex)) {
textContent = textContent.replace(colorRegex, function (match, code, color, text) {
if (typeof (code) !== "undefined") {
return code;
}
return `<span style="color: ${color};">${text}</span>`;
});
}
if (textContent.match(paragraphAndHeadingRegex)) {
textContent = textContent.replace(paragraphAndHeadingRegex, function (match, tag, text, closeTag) {
let hasStartPattern = text.startsWith("|-");
let hasEndPattern = text.endsWith("-|");
let anchor = tag.charAt(0) == "h" ? generateAnchorFromHeading(text) : "";
if (text.startsWith("|=") && text.endsWith("=|")) {
return `<${tag} style="text-align:justify;">${anchor}${text.slice(2).slice(0, -2)}</${closeTag}>`;
} else if (hasStartPattern && hasEndPattern) {
return `<${tag} style="text-align:center;">${anchor}${text.slice(2).slice(0, -2)}</${closeTag}>`;
} else if (hasEndPattern) {
return `<${tag} style="text-align:right;">${anchor}${text.slice(0, -2)}</${closeTag}>`;
} else if (hasStartPattern) {
return `<${tag} style="text-align:left;">${anchor}${text.slice(2)}</${closeTag}>`;
}
return `<${tag}>${anchor}${text}</${closeTag}>`;
});
}
return textContent;
}
function applyGroupCode(textContent, id) {
if (textContent.match(codeTabRegex)) {
let count = 0;
textContent = textContent.replace(codeTabRegex, (match, codes) => {
// code is the first match, the full grouped code without the start ===group and the end ===
let codeArray = codes.substring(5, codes.length - 6).split(/<\/pre>\n<pre>/g); // remove first and last <pre> then split between all lang
let lang = [];
for (let i in codeArray) {
lang[i] = langCodeRegex.exec(codeArray[i])[1]; // extract lang for code
codeArray[i] = "<pre>" + codeArray[i] + "</pre>\n"; // add pre at the start and at the end of all code
}
let menuTab = "<ul class='nav nav-tabs' role='tablist'>";
let contentTab = "<div class='tab-content'>";
for (let i = 0; i < lang.length; i++) {
menuTab += `<li role='presentation' ${i === 0 ? "class='active'" : ""}><a href='#${lang[i] + count + id}' aria-controls='${lang[i]}' role='tab' data-toggle='tab'>${capitalizeFirstLetter(lang[i])}</a></li>`;
contentTab += `<div role="tabpanel" class="tab-pane ${i === 0 ? "active" : ""}" id="${lang[i] + count + id}">${codeArray[i]}</div>`;
}
menuTab += "</ul>";
contentTab += "</div>";
count++;
return menuTab + contentTab;
});
}
return textContent;
}
function capitalizeFirstLetter(name) {
return name.charAt(0).toUpperCase() + name.slice(1);
}
function generateAnchorFromHeading(heading) {
return `<a class="anchor-offset" name="${slugify(heading)}"></a>`;
}
function applySpoiler(textContent, id) {
if (textContent.match(spoilerRegex)) {
let count = 0;
textContent = textContent.replace(spoilerRegex, (match, text) => {
const spoilerButton = `
<button class="btn btn-primary extended-markdown-spoiler" type="button" name="spoiler" data-bs-toggle="collapse" data-bs-target="#spoiler${count + id}" aria-expanded="false" aria-controls="spoiler${count + id}">
Spoiler <i class="fa fa-eye"></i>
</button>`;
const spoilerContent = `
<div class="collapse" id="spoiler${count + id}">
<div class="card card-body spoiler">${text}</div>
</div>`;
count++;
return `<p>${spoilerButton}${spoilerContent}</p>`;
});
}
return textContent;
}
module.exports = ExtendedMarkdown;