Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit a37f6c2

Browse files
committed
Added support for inline code & code blocks
input bar
1 parent 892088a commit a37f6c2

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

components/mdToHTML.go

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package components
33
import (
44
"fmt"
55
"regexp"
6+
"strings"
67
)
78

89
var rReg = regexp.MustCompile(`\r`)
@@ -101,6 +102,7 @@ func MDToHTML(content string) (string, bool) {
101102
icI := 0
102103
esI := 0
103104

105+
// escaped characters
104106
for syntax := range mdSyntax {
105107
reg := regexp.MustCompile(`\\` + syntax)
106108
for reg.MatchString(content) {
@@ -110,11 +112,33 @@ func MDToHTML(content string) (string, bool) {
110112
esI++
111113
}
112114
}
115+
116+
skipIC := 0
113117

114118
// inline code index
115-
for mdSpreadInline.Reg.MatchString(content) {
116-
curLoc := mdSpreadInline.Reg.FindStringIndex(content)
117-
inlineCodeMap[icI] = content[curLoc[0]:curLoc[1]]
119+
for skipIC != len(mdSpreadInline.Reg.FindAllStringIndex(content, skipIC + 1)) {
120+
curLocs := mdSpreadInline.Reg.FindAllStringIndex(content, skipIC + 1)
121+
curLoc := curLocs[skipIC]
122+
123+
testContent := content[curLoc[0]:curLoc[1]]
124+
codeBlock := false
125+
126+
if len(testContent) >= 7 {
127+
codeBlock = testContent[:3] == "```" && testContent[len(testContent)-3:] == "```"
128+
}
129+
130+
// don't parse multi-line `inline code`
131+
if len(testContent) >= 3 && !codeBlock {
132+
if testContent[:1] == "`" && testContent[len(testContent)-1:] == "`" {
133+
if len(strings.Split(testContent, "<br />")) != 1 {
134+
skipIC++
135+
continue
136+
}
137+
}
138+
}
139+
140+
inlineCodeMap[icI] = testContent
141+
118142
content = fmt.Sprintf("%v--\tic%v--%v", content[:curLoc[0]], icI, content[curLoc[1]:])
119143
icI++
120144
}
@@ -134,7 +158,45 @@ func MDToHTML(content string) (string, bool) {
134158

135159
for i, str := range inlineCodeMap {
136160
reg := regexp.MustCompile(fmt.Sprintf(`--\tic%v--`, i))
137-
content = reg.ReplaceAllString(content, str)
161+
prefix := ""
162+
suffix := ""
163+
164+
for {
165+
changed := false
166+
if len(str) >= 7 {
167+
if str[:3] == "```" && str[len(str)-3:] == "```" {
168+
str = str[3 : len(str)-3]
169+
prefix += `<span class="code-block-wrapper"><span class="code-block">`
170+
suffix = "</span></span>" + suffix
171+
spl := strings.SplitN(str, "<br />", 2)
172+
173+
if len(spl) == 2 {
174+
str = spl[1]
175+
}
176+
177+
changed = true
178+
}
179+
}
180+
if len(str) >= 3 {
181+
if str[:1] == "`" && str[len(str)-1:] == "`" {
182+
str = str[1 : len(str)-1]
183+
prefix += `<span class="inline-code">`
184+
suffix = "</span>" + suffix
185+
changed = true
186+
}
187+
}
188+
if !changed {
189+
break
190+
}
191+
}
192+
193+
if len(str) >= 6 {
194+
if str[len(str)-6:] == "<br />" {
195+
str = str[:len(str)-6]
196+
}
197+
}
198+
199+
content = reg.ReplaceAllString(content, prefix + str + suffix)
138200
}
139201

140202
for i, str := range escapedSyntaxMap {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div class="row" style="height: 7vh; padding: 12.5px;"><div class="row full-width" style="height: 100%; background: var(--background-lighter); border-radius: 6.25px"><h1 style="color: var(--text-muted);"><i>Sorry can't send messages right now, this is a log!</i></h1></div></div>
1+
<div class="row" style="padding: 15px;"><div class="row full-width" style="height: 100%; padding: 6px; background: var(--background-lighter); border-radius: 6.25px"><h1 style="color: var(--text-muted);"><i>Sorry can't send messages right now, this is a log!</i></h1></div></div>

themes/base/css/style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,17 @@ p, h1, h2, h3, h4, h5, h6 {
324324
width: 5px;
325325
width: 100%;
326326
}
327+
328+
.inline-code {
329+
background-color: var(--super-dark);
330+
padding: 0.2vh 0.4vw;
331+
border-radius: 6.25px;
332+
}
333+
334+
.code-block-wrapper {
335+
background-color: var(--super-dark);
336+
padding: 1vh 1vw;
337+
border-radius: 6.25px;
338+
width: 80vw;
339+
display: block;
340+
}

0 commit comments

Comments
 (0)