-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightmarkdown.js
330 lines (299 loc) · 11.8 KB
/
lightmarkdown.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const debug_latex=true
const debug_pre=true
const QUOT/*ationMark*/='ºq '
class P /*(Paragraph with metadata) */ {
static ROOT=new P(false,"")
constructor ( _isPre, _strContent ) {
this.isPre = _isPre;
this.strContent = _strContent;
this.parent = P.ROOT // TODO:(future) allow parents. ej: pre inside li
}
}
// REF: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/writing-mathematical-expressions#about-writing-mathematical-expressions
let sRETexInline="[$]" // RegEx starts with a '$'
sRETexInline=sRETexInline+"([^$]+)" // It continues until finding another '$'
// (matching anything that is not $$)
sRETexInline=sRETexInline+"[$]" // and ends including the $
if (debug_latex) { console.log("RegeExg_TeX Inline: "+sRETexInline); }
const RE_TeX_INLINE=new RegExp(sRETexInline,"g")
let sRETexBlock="[$][$]" // RegEx starts with a '$$'
sRETexBlock=sRETexBlock+"([^$][^$]+)" // It continues until finding another '$$'
// (matching anything that is not $$)
sRETexBlock=sRETexBlock+"[$][$]" // and ends including the $$
if (debug_latex) { console.log("RegeExg_TeX Block: "+sRETexBlock); }
const RE_TeX_BLOCK=new RegExp(sRETexBlock,"g")
// A single empty line delimits paragraphs
const PARAGRAPH_MARK_REGEX=/^\n/gm
function _00_escapeHTML(md, relative_path){
// NEXT) Replace external link <http...> link
// NEXT) Clean/replace conflictive chars.
md = md
.replaceAll('<!--' ,'º[!--' ) // TODO:(document) No native HTML allowed
.replaceAll('-->' ,'--]º' ) // except comments, since '<'... '>' is
.replaceAll('<br/>' ,'º[br/]º' )
.replaceAll('<br>' ,'º[br/]º' )
.replaceAll('<hr/>' ,'º[hr/]º' )
.replaceAll('<hr xxl/>' ,'º[hr xxl/]º' )
.replace(/\<((https?|\.\/).*[^>\n]+)\>/g," º[a target='_blank' href='$1']º$1º[/a]º ")
.replaceAll(/^[>] /mg,QUOT )
.replaceAll('<' ,'<' ) // always replaced.
.replaceAll('>' ,'>' )
.replaceAll(/^[-] /mg,'* ' )
// .replaceAll(/ $/mg,'<br/>' ) <·· Commented.
return md
}
const funReplaceHeader = function(match, m1){
const CLEAN_BUILD_ID_REGEX_0=new RegExp('\\[\\[([^\\[])*\\]\\]' , 'g');
const CLEAN_BUILD_ID_REGEX_1=new RegExp('[^",a-z\,A-Z,0-9,_,\']', 'g');
const tag = match.startsWith("# " ) ? "h1"
: match.startsWith("## " ) ? "h2"
: match.startsWith("### " ) ? "h3"
: match.startsWith("#### " ) ? "h4"
: match.startsWith("##### " ) ? "h5"
: match.startsWith("###### ") ? "h6"
: ""
if (tag === "") return match
const id=match
.replace(CLEAN_BUILD_ID_REGEX_0, '')
.replace(CLEAN_BUILD_ID_REGEX_1, '')
.replaceAll('"', '') // for some weird reason '"' is not replaced by regex.
const result = `<${tag} class='h_anchor' id='${id}'>${m1.trim()}</${tag}>\n`;
return result
}
const REGEX_MAYBE_IS_TABLE=/^[|][^|]+[|].*\n[|][^|]+[|]/gs
function handleTables(p/*aragraph*/) {
if (! (p.match(REGEX_MAYBE_IS_TABLE) /*table*/)) { return p; }
p = p.trim()
const tableStart = '<table cellspacing="0"><tbody>', tableEnd = '</tbody></table>',
rowStart = '<tr>' , rowEnd = '\n</tr>',
headStart = '<th>' , headEnd = '\n</th>',
colStart = '<td>' , colEnd = '\n</td>';
/* ^^
* Adding a "next line" help to parse properly other elements inside
* the <tr>,<th>,<td> (TeX expressions for example).
*/
const row_l = p.split('\n')
let content = '';
for (let i=0; i < row_l.length; i += 1) {
let i_res = row_l[i]
let column_l = i_res.split('|')
let k = 0
let inner = ''
for (k; k < column_l.length; k += 1) {
if (k == 0) {continue;}
let k_res = column_l[k].trim()
inner += i==0 ? `${headStart}${k_res}${headEnd}\n`
: `${colStart}${k_res}${colEnd}\n`
}
content += `${rowStart}${inner}${rowEnd}`
i_res = row_l[i + 1]
}
let result = (content) ? `${tableStart}${content}${tableEnd}` : '';
return result
}
function handlePre(p_m/*aragraph*/) {
if (debug_pre) { console.log(p_m.strContent) }
let result = "<pre>"
+ ( p_m.strContent.trimEnd()
.replaceAll(/^\s*[|]/gm,"")
/* .replaceAll(/[|]\s*$/gm,'') commented. Not standard, not documented */
/* .replaceAll(/\s\s*$/mg,"") // avoid problems */
)
+ "</pre>"
;
if (debug_pre) { console.log(result) }
return new P(true, result);
}
const ulistRegex_l=[/^[*-] /gm ,
/^ {2,3}[*-] /gm ,
/^ {4,6}[*-] /gm ,
/^ {7,8}[*-] /gm ,
]
function handleUnorderedLists(nLevel, p/*aragraph*/) {
if (nLevel>ulistRegex_l.length-1) return p;
const li_list = p.split(ulistRegex_l[nLevel])
if (li_list.length == 1) return p;
return li_list[0]+"<ul>"+li_list.slice(1).map(li=>"<li>"+handleOrderedLists(nLevel+1,handleUnorderedLists(nLevel+1,li))+"</li>").join("\n")+"</ul>"
}
const olistRegex_l=[ /^([0-9,.]+\. )/gm,
/^ {2,3}([0-9,.]+\. )/gm,
/^ {4,6}([0-9,.]+\. )/gm,
/^ {7,8}([0-9,.]+\. )/gm,
]
function handleOrderedLists(nLevel, p/*aragraph*/) {
if (nLevel>olistRegex_l.length-1) return p;
const li_list = p.split(olistRegex_l[nLevel])
if (li_list.length == 1) return p;
let even=true;
const l = li_list
.slice(1)
.map(it => {
even = !even
if (even) {
const liContent=
handleUnorderedLists(nLevel+1,
handleOrderedLists(nLevel+1,it))
return `${liContent}</li>`
} else {
if (it == "1. ") return "<li>"
it = it.replace(". ","")
return `<li value="${it}">`
}
}).join("\n")
return `${li_list[0]}<ol>${l}</ol>`
}
function handleHeaders(p/*aragraph*/) {
return p.replace(/^[\#]{1,6}[ ](.+)/mg, funReplaceHeader);
}
const imageRegex=/\!\[([^\]]+)\]\(([^\)]+)\){([^}]+)}?/g
//
function handleImages(p/*aragraph*/) {
return p.replace(imageRegex,
'<img src="$2" alt="$1" style="$3" />');
}
function handleLinks(p/*aragraph*/) {
return p.replace(
/[\[]{1}([^\]]+)[\]]{1}[\(]{1}([^\)\"]+)(\"(.+)\")?[\)]{1}/g ,
'<a href="$2" title="$4">$1</a>');
}
const stylesParseHelp={
"bold" : ["<b>" ,"</b>" , /\b[\*\_]{2}|[\*\_]{2}\b/, " "," "],
"italic" : ["<i>" ,"</i>" , /\b[\*\_]{1}|[\*\_]{1}\b/, " " ," " ],
"strikethrough" : ["<del>","</del>", /\b[\~]{2}|[\~]{2}\b/ , " "," "],
}
const styleList = Object.keys(stylesParseHelp)
function handleFontStyles(p/*aragraph*/) {
if (p.match(PARAGRAPH_MARK_REGEX /* Markdown collides with LaTex. LaTex takes precedence.*/)) { return p; }
for (let styleIdx=0; styleIdx < styleList.length; styleIdx++){
let even=true; // due to next slice(1)
const styleKey=styleList[styleIdx]
const oTag=stylesParseHelp[styleKey][0], // openTag
cTag=stylesParseHelp[styleKey][1], // closeTag
regex=stylesParseHelp[styleKey][2], // split regex
oW=stylesParseHelp[styleKey][3], // open white-space
cW=stylesParseHelp[styleKey][4]; // close white-space
const list = p.split(regex)
if (list.length==1) continue;
p = list.slice(0,-1).map(it => {
even = !even
if (even) { return `${it}${cTag}${cW}` }
else { return `${it}${oW}${oTag}` }
}).join("")+list.slice(-1)
if (list.length%2==0) {
p=p+cTag+cW // Finally close any open tag.
}
}
return p
}
function handleBlockQuotes(p/*aragraph*/) {
if (! (p.startsWith(QUOT) /* quotations */ )) { return p; }
return "<blockquote>"+p.replaceAll(QUOT, "")+"</blockquote>"
}
/**
* Apply markdown to each paragraph.
*/
function _01_standardMarkdownParsing(p_m/*paragraph */, relative_path){
if (p_m.isPre) {
return handlePre(p_m);
}
let p = p_m.strContent;
p = handleUnorderedLists(0, p);
p = handleOrderedLists (0, p);
p = handleTables(p);
p = handleHeaders(p);
p = handleImages(p);
p = handleFontStyles(p);
p = handleBlockQuotes(p);
p = p.replaceAll(/ $/mg,"º[br/]º" ) // space + space + end-of-line == new html line in markdown
p = handleLinks(p)
return new P(p_m.isPre, p.length>0 ? "\n<p>"+p+"</p>" : "");
}
/* step 2: markdown txt extensions for pre-like */
const _02_markdown_extension = (p_m, relative_path) => {
let p = p_m.strContent;
// NEXT) TXTWD extension. replace anchors link #[target_id]
p = p.replace( /[#]\[([^\]]*)\]/g, "◆<span id='$1'>#$1</span>◆")
// NEXT) TXTWD extension. replace relative links @[#internal_link]
// TODO: Improve relative handling. There can be:
// 1. links to external but relative to viewer content (normal case)
// 2. links indicating viewer to reload current content
// (non-standard) links that just the TXTWD parser/viewer will understand.
p = p.replace(
/@\[(#[^\]\n]*)\]/g,
" ◐<a onClick='window.scrollInto(\"$1\")'>$1</a>◑")
// TODO:(0) move to standard markdown (paragraph) parsing
// Note that relative images are relative to txt document
// (vs html viewer)
p = p.replace(
/i\[((\.\/)?[^|\]\n]*)[|]?([^\]\n]*)\]/g,
"<img src='"+relative_path+"/$1' style='$2' />")
// NEXT) TXTWD extension. Add style to topic blocks [[topic1,topic2.subtopicA,...]]
p = p.replace(/(\[\[[^\]\n]*\]\])/g, "<div class='txtblock'>$1</div>")
return new P(p_m.isPre, p)
}
const HAS_REGEX_LATEX=/[$]([^$]+)[$]/gs
const _03_latex_extension = (p_m) => {
if (p_m.isPre) return p_m;
if (! (p_m.strContent.match(HAS_REGEX_LATEX) )) { return p_m; }
if (debug_latex) { console.log(`paragraph with Tex found: ${p_m.strContent}`) }
// NEXT) Replace LaTex expression with SVG
const funReplaceTeXInLine = function(match, m1) {
if (debug_latex) {
console.log(match);
console.log(m1);
}
return MathJax.tex2mml(m1); // TODO:(0) Recheck .tex2SVG not found ¿?
}
const funReplaceTeXBlock = function(match, m1) {
return `<div>${funReplaceTeXInLine(match, m1)}</div>`
}
let p = p_m.strContent
// Blocks must be replaced first.
/* 1) */ p = p.replace( RE_TeX_BLOCK , funReplaceTeXBlock)
/* 2) */ p = p.replace( RE_TeX_INLINE, funReplaceTeXInLine)
return new P(p_m.isPre, p)
}
function _04_unescapeHTML(p_m/*paragraph */){
return new P(
p_m.isPre,
p_m.strContent
.replaceAll("º[","<")
.replaceAll("]º",">")
)
}
function parseMD2HTML(md_payload, relative_path){
md_payload = _00_escapeHTML(md_payload, relative_path)
let even=true
//const paragraph_l = md_payload.split(PARAGRAPH_MARK_REGEX).filter(p => p.length>0)
const paragraph_l = md_payload.split("```").map(it => {
const p_list = (even)
? it.split(PARAGRAPH_MARK_REGEX).filter(it => it!="")
: [it]
const result = p_list.map(
it2 => { return new P( !even /* isPre*/, it2 /* strContent*/ ) }
)
even = !even
// console.log(result)
return result
}).flat()
const result = paragraph_l
.map(p_m/*paragraph class instance*/ => {
return _01_standardMarkdownParsing(p_m, relative_path)
})
.map(p_m/*paragraph class instance*/ => {
return _02_markdown_extension (p_m, relative_path)
})
.map(p_m/*paragraph class instance*/ => {
return _03_latex_extension (p_m)
})
.map(p_m/*paragraph class instance*/ => {
return _04_unescapeHTML(p_m)
})
.map(p_m/*paragraph class instance*/ => {
return p_m.strContent
})
return result;
}
export {
parseMD2HTML
}