Skip to content

Commit d5ecf16

Browse files
committed
WIP
1 parent 967cdde commit d5ecf16

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

coderd/parameter/plaintext.go renamed to coderd/parameter/renderer.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package parameter
22

33
import (
4+
"regexp"
45
"strings"
56

67
"github.com/charmbracelet/glamour"
@@ -95,3 +96,33 @@ func Plaintext(markdown string) (string, error) {
9596

9697
return strings.TrimSpace(output), nil
9798
}
99+
100+
var (
101+
reBold = regexp.MustCompile(`\*\*([^*]+)\*\*`)
102+
reItalic = regexp.MustCompile(`\*([^*]+)\*`)
103+
reStrikethrough = regexp.MustCompile(`~~([^~]+)~~`)
104+
reLink = regexp.MustCompile(`\[([^]]+)\]\(([^)]+)\)`)
105+
)
106+
107+
func HTML(markdown string) string {
108+
draft := replaceBold(markdown) // bold regexp must go before italic - ** vs. *
109+
draft = replaceItalic(draft)
110+
draft = replaceStrikethrough(draft)
111+
return replaceLinks(draft)
112+
}
113+
114+
func replaceItalic(markdown string) string {
115+
return reItalic.ReplaceAllString(markdown, "<i>$1</i>")
116+
}
117+
118+
func replaceBold(markdown string) string {
119+
return reBold.ReplaceAllString(markdown, "<strong>$1</strong>")
120+
}
121+
122+
func replaceStrikethrough(markdown string) string {
123+
return reStrikethrough.ReplaceAllString(markdown, "<del>$1</del>")
124+
}
125+
126+
func replaceLinks(markdown string) string {
127+
return reLink.ReplaceAllString(markdown, `<a href="$2">$1</a>`)
128+
}

coderd/parameter/plaintext_test.go renamed to coderd/parameter/renderer_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ __This is bold text.__
4747
require.Equal(t, nothingChanges, stripped)
4848
})
4949
}
50+
51+
func TestHTML(t *testing.T) {
52+
t.Parallel()
53+
t.Run("Simple", func(t *testing.T) {
54+
t.Parallel()
55+
56+
mdDescription := `**Coder** is in *early access* mode. To ~~register~~ request access, fill out [this form](https://internal.example.com). ***Thank you!***`
57+
expected := `<strong>Coder</strong> is in <i>early access</i> mode. To <del>register</del> request access, fill out <a href="https://internal.example.com">this form</a>. <i><strong>Thank you!</strong></i>`
58+
59+
rendered := parameter.HTML(mdDescription)
60+
require.Equal(t, expected, rendered)
61+
})
62+
63+
t.Run("Nothing changes", func(t *testing.T) {
64+
t.Parallel()
65+
66+
nothingChanges := "This is a simple description, so nothing changes."
67+
68+
rendered := parameter.HTML(nothingChanges)
69+
require.Equal(t, nothingChanges, rendered)
70+
})
71+
}

0 commit comments

Comments
 (0)