diff --git a/pkg/config/admin_scopes_test.go b/pkg/config/admin_scopes_test.go index c50da0c..bd84511 100644 --- a/pkg/config/admin_scopes_test.go +++ b/pkg/config/admin_scopes_test.go @@ -10,11 +10,11 @@ import ( // returned by the scope list function. func TestAdminScopesCount(t *testing.T) { var scopes = config.ListAdminScopes() - if len(scopes) != config.QuantityAdminScopes || len(scopes) != len(config.AdminScopeDescriptions) { + if len(scopes) != config.QuantityAdminScopes || len(scopes) != len(config.AdminScopeDescriptions)-1 { t.Errorf( "The list of scopes returned by ListAdminScopes doesn't match the expected count. "+ - "Expected %d, got %d", - config.QuantityAdminScopes, + "Expected %d (with %d descriptions), got %d", + config.QuantityAdminScopes, len(config.AdminScopeDescriptions), len(scopes), ) } diff --git a/pkg/markdown/markdown.go b/pkg/markdown/markdown.go index 31dede1..116f18b 100644 --- a/pkg/markdown/markdown.go +++ b/pkg/markdown/markdown.go @@ -2,12 +2,18 @@ package markdown import ( + "regexp" "strings" "github.com/microcosm-cc/bluemonday" "github.com/shurcooL/github_flavored_markdown" ) +var ( + RegexpHTMLTag = regexp.MustCompile(`<(.|\n)+?>`) + RegexpMarkdownLink = regexp.MustCompile(`\[([^\[\]]*)\]\((.*?)\)`) +) + // Render markdown from untrusted sources. func Render(input string) string { // Render Markdown to HTML. @@ -27,3 +33,28 @@ func Quotify(input string) string { } return strings.Join(lines, "\n") } + +/* +DeMarkify strips some Markdown syntax from plain text snippets. + +It is especially useful on Forum views that show plain text contents of posts, and especially +for linked at-mentions of the form `[@username](/go/comment?id=1234)` so that those can be +simplified down to just the `@username` text contents. + +This function will strip and simplify: + +- Markdown hyperlinks as spelled out above +- Literal HTML tags such as +*/ +func DeMarkify(input string) string { + // Strip all standard HTML tags. + input = RegexpHTMLTag.ReplaceAllString(input, "") + + // Replace Markdown hyperlinks. + matches := RegexpMarkdownLink.FindAllStringSubmatch(input, -1) + for _, m := range matches { + input = strings.ReplaceAll(input, m[0], m[1]) + } + + return input +} diff --git a/pkg/markdown/markdown_test.go b/pkg/markdown/markdown_test.go new file mode 100644 index 0000000..0e83fb7 --- /dev/null +++ b/pkg/markdown/markdown_test.go @@ -0,0 +1,38 @@ +package markdown_test + +import ( + "testing" + + "code.nonshy.com/nonshy/website/pkg/markdown" +) + +func TestDeMarkify(t *testing.T) { + var cases = []struct { + Input string + Expect string + }{ + { + Input: "Hello world!", + Expect: "Hello world!", + }, + { + Input: "[@username](/go/comment?id=1234) Very well said!", + Expect: "@username Very well said!", + }, + { + Input: `Wikipedia said **otherwise.**`, + Expect: "Wikipedia said **otherwise.**", + }, + { + Input: "[Here](/here) is one [link](https://example.com), while [Here](/here) is [another](/another).", + Expect: "Here is one link, while Here is another.", + }, + } + + for i, tc := range cases { + actual := markdown.DeMarkify(tc.Input) + if actual != tc.Expect { + t.Errorf("Test #%d: expected '%s' but got '%s'", i, tc.Expect, actual) + } + } +} diff --git a/pkg/templates/template_funcs.go b/pkg/templates/template_funcs.go index 544c14b..9ee1d08 100644 --- a/pkg/templates/template_funcs.go +++ b/pkg/templates/template_funcs.go @@ -36,6 +36,7 @@ func TemplateFuncs(r *http.Request) template.FuncMap { "ComputeAge": utility.Age, "Split": strings.Split, "ToMarkdown": ToMarkdown, + "DeMarkify": markdown.DeMarkify, "ToJSON": ToJSON, "ToHTML": ToHTML, "ToString": func(v interface{}) string { diff --git a/web/templates/forum/board_index.html b/web/templates/forum/board_index.html index aa11309..e7ba2df 100644 --- a/web/templates/forum/board_index.html +++ b/web/templates/forum/board_index.html @@ -132,7 +132,7 @@ {{if .Pinned}}{{end}} {{or .Title "Untitled"}} - {{TrimEllipses .Comment.Message 256}} + {{TrimEllipses (DeMarkify .Comment.Message) 256}}
diff --git a/web/templates/forum/newest.html b/web/templates/forum/newest.html index a29b8aa..5297958 100644 --- a/web/templates/forum/newest.html +++ b/web/templates/forum/newest.html @@ -126,7 +126,7 @@ - {{TrimEllipses .Thread.Comment.Message 256}} + {{TrimEllipses (DeMarkify .Thread.Comment.Message) 256}} {{$Photos := $Root.PhotoMap.Get .Thread.Comment.ID}} @@ -191,7 +191,7 @@ - {{TrimEllipses .Comment.Message 256}} + {{TrimEllipses (DeMarkify .Comment.Message) 256}} {{$Photos := $Root.PhotoMap.Get .Comment.ID}} diff --git a/web/templates/forum/search.html b/web/templates/forum/search.html index 763e25c..f3aa92a 100644 --- a/web/templates/forum/search.html +++ b/web/templates/forum/search.html @@ -165,7 +165,7 @@ - {{TrimEllipses .Message 512}} + {{TrimEllipses (DeMarkify .Message) 512}} {{$Photos := $Root.PhotoMap.Get .ID}}