Skip to content

Commit 23be324

Browse files
authored
Merge pull request #142 from julwrites/staging
Escaping markdown better
2 parents 5538170 + 35315ad commit 23be324

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

pkg/app/passage.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ func escapeMarkdownV2(s string) string {
4343
// Note: '^' is not in this list. Let's assume it doesn't need escaping.
4444
// The logic should be to escape these characters *only* when they are not part of a formatting tag.
4545
// However, since we are processing raw text nodes, any special character should be escaped.
46-
r := strings.NewReplacer(
47-
"_", `\_`, "*", `\*`, "[", `\[`, "]", `\]`, "(", `\(`, ")", `\)`,
48-
"~", `\~`, "`", "\\`", ">", `\>`, "#", `\#`, "+", `\+`, "-", `\-`,
49-
"=", `\=`, "|", `\|`, "{", `\{`, "}", `\}`, ".", `\.`, "!", `\!`,
50-
`\`, `\\`,
51-
)
52-
return r.Replace(s)
46+
var sb strings.Builder
47+
for _, r := range s {
48+
switch r {
49+
case '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\\':
50+
sb.WriteRune('\\')
51+
}
52+
sb.WriteRune(r)
53+
}
54+
return sb.String()
5355
}
5456

5557
// Helper functions for parsing

pkg/app/passage_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ func TestParsePassageFromHtml(t *testing.T) {
139139
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
140140
}
141141
})
142+
143+
t.Run("Dot escaping", func(t *testing.T) {
144+
html := `heaven.`
145+
expected := `heaven\.`
146+
if got := ParsePassageFromHtml(html); got != expected {
147+
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
148+
}
149+
})
142150
}
143151

144152
func TestCheckBibleReference(t *testing.T) {

0 commit comments

Comments
 (0)