Repository navigation
Expand file tree
/
Copy pathmarkdown.ts
More file actions
63 lines (47 loc) · 2.03 KB
/
Copy pathmarkdown.ts
File metadata and controls
63 lines (47 loc) · 2.03 KB
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
export function parseMarkdown(input: string) {
// Undertitle: # Text #
input = input.replace(/#\s*(.*?)\s*#/g, "<h4>$1</h4>")
// Cross Reference & Notes: *{text}* | *{[reference](id) text}*
input = parseCrossReferences()
// Jesus Red Words: !{text}!
input = input.replace(/!\{(.*?)\}!/g, '<span style="color:red;">$1</span>')
// Uncertain: [text]
input = input.replace(/\[(.*?)\]/g, '<span class="uncertain">[$1]</span>')
///// https://www.markdownguide.org/basic-syntax/
// Bold: **text** or __text__
input = input.replace(/(\*\*|__)(.*?)\1/g, "<strong>$2</strong>")
// Italic: *text* or _text_
input = input.replace(/(\*|_)(.*?)\1/g, "<em>$2</em>")
// Underline: ++text++
input = input.replace(/\+\+(.*?)\+\+/g, "<u>$1</u>")
// Strikethrough: ~~text~~
input = input.replace(/~~(.*?)~~/g, "<del>$1</del>")
/////
// Quote: "text"
input = input.replace(/(?<!<[^>]*?)"([^"]*?)"(?![^<]*?>)/g, "<q>$1</q>")
// Line Break: \n
input = input.replace(/\n/g, "<br>")
// Paragraph: ¶
input = input.replace(/¶/g, "<br><br>")
return input
function parseCrossReferences() {
return input.replace(/\*\{(.*?)\}\*/g, (_, content) => {
const transformedContent = content.replace(/\[(.*?)\]\((.*?)\)/g, '<span id="$2" class="cross-ref-id">$1</span>')
return `<span class="cross-ref"><span class="content">${transformedContent}</span></span>`
})
}
}
export function stripMarkdown(input: string) {
input = input.replace(/#\s*(.*?)\s*#/g, "")
input = input.replace(/\*\{(.*?)\}\*/g, "$1")
input = input.replace(/!\{(.*?)\}!/g, "$1")
// input = input.replace(/\[(.*?)\]/g, "[$1]")
input = input.replace(/(\*\*|__)(.*?)\1/g, "$2")
input = input.replace(/(\*|_)(.*?)\1/g, "$2")
input = input.replace(/\+\+(.*?)\+\+/g, "$1")
input = input.replace(/~~(.*?)~~/g, "$1")
input = input.replace(/"([^"]*?)"/g, "$1")
input = input.replace(/\n/g, "")
input = input.replace(/¶/g, "")
return input
}