Skip to content

Commit

Permalink
Merge pull request #17 from rneatherway/codefence-fix
Browse files Browse the repository at this point in the history
Fix rendering of blocks in code fences
  • Loading branch information
rneatherway authored Sep 28, 2022
2 parents 78926b5 + 4c3a72f commit 73d749a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

var userRE = regexp.MustCompile("<@[A-Z0-9]+>")
var linkRE = regexp.MustCompile(`<(https?://[^|>]+)\|([^>]+)>`)
var openCodefence = regexp.MustCompile("(?m)^```")
var closeCodefence = regexp.MustCompile("(?m)(.)```$")

type UserProvider interface {
UsernameForID(string) (string, error)
Expand Down Expand Up @@ -68,6 +70,8 @@ func convert(client UserProvider, b *strings.Builder, s string) error {
}

text = linkRE.ReplaceAllString(text, "[$2]($1)")
text = openCodefence.ReplaceAllString(text, "```\n")
text = closeCodefence.ReplaceAllString(text, "$1\n```")

for _, line := range strings.Split(text, "\n") {
// TODO: Might be a good idea to escape 'line'
Expand Down
17 changes: 17 additions & 0 deletions internal/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ func TestLinkify(t *testing.T) {
}
}
}

func TestFixCodefence(t *testing.T) {
table := [][]string{
{"```{\n x: y,\n a: b\n}```", "```\n{\n x: y,\n a: b\n}\n```"},
}

for _, test := range table {
input := test[0]
expected := test[1]
actual := openCodefence.ReplaceAllLiteralString(input, "```\n")
actual = closeCodefence.ReplaceAllString(actual, "$1\n```")

if actual != expected {
t.Errorf("expected %q, actual %q", expected, actual)
}
}
}

0 comments on commit 73d749a

Please sign in to comment.