From 00be0256f616cd6a78bf3d26269b129a768880e2 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 13 Dec 2024 17:19:25 -0500 Subject: [PATCH] internal/github, etc.: unify GH markdown parsing Add a function to internal/github that parses GitHub-flavored markdown (approximately), and use it. Change-Id: I32c96599ce28dc4dfe2ee40d885dacaca1acdcf4 Reviewed-on: https://go-review.googlesource.com/c/oscar/+/635679 Reviewed-by: Hyang-Ah Hana Kim LUCI-TryBot-Result: Go LUCI --- internal/commentfix/fix.go | 8 +------- internal/gaby/labels.go | 1 + internal/github/data.go | 12 ++++++++++++ internal/labels/labels.go | 9 +-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/internal/commentfix/fix.go b/internal/commentfix/fix.go index fc7408d..6c378f0 100644 --- a/internal/commentfix/fix.go +++ b/internal/commentfix/fix.go @@ -560,13 +560,7 @@ func (ic *issueOrComment) editBody(ctx context.Context, gh *github.Client, body // If no fixes apply, it returns "", false. // If any fixes apply, it returns the updated text and true. func (f *Fixer) Fix(text string) (newText string, fixed bool) { - p := &markdown.Parser{ - AutoLinkText: true, - Strikethrough: true, - HeadingIDs: true, - Emoji: true, - } - doc := p.Parse(text) + doc := github.ParseMarkdown(text) for _, fixer := range f.fixes { if f.fixOne(fixer, doc) { fixed = true diff --git a/internal/gaby/labels.go b/internal/gaby/labels.go index 10f254b..c5a4654 100644 --- a/internal/gaby/labels.go +++ b/internal/gaby/labels.go @@ -107,6 +107,7 @@ func (g *Gaby) populateLabelsPage(r *http.Request) *labelsPage { } lr.Category = cat lr.Explanation = exp + // Should probably use GH-flavored markdown here, but it's only for display. lr.BodyHTML = htmlutil.MarkdownToSafeHTML(i.Body) } p.Results = append(p.Results, lr) diff --git a/internal/github/data.go b/internal/github/data.go index 38893a5..3f61806 100644 --- a/internal/github/data.go +++ b/internal/github/data.go @@ -17,6 +17,7 @@ import ( "golang.org/x/oscar/internal/model" "golang.org/x/oscar/internal/storage" "golang.org/x/oscar/internal/storage/timed" + "rsc.io/markdown" "rsc.io/ordered" ) @@ -384,3 +385,14 @@ func mustParseTime(s string) time.Time { } return t } + +// ParseMarkdown parses text that is in GitHub-flavored markdown format. +func ParseMarkdown(text string) *markdown.Document { + p := &markdown.Parser{ + AutoLinkText: true, + Strikethrough: true, + HeadingIDs: true, + Emoji: true, + } + return p.Parse(text) +} diff --git a/internal/labels/labels.go b/internal/labels/labels.go index dd9382e..ef21b38 100644 --- a/internal/labels/labels.go +++ b/internal/labels/labels.go @@ -84,14 +84,7 @@ var htmlCommentRegexp = regexp.MustCompile(``) // cleanIssueBody adjusts the issue body to improve the odds that it will be properly // labeled. func cleanIssueBody(text string) string { - // TODO(jba): These settings are also used in fix.go to parse bodies. Factor out. - p := &markdown.Parser{ - AutoLinkText: true, - Strikethrough: true, - HeadingIDs: true, - Emoji: true, - } - doc := p.Parse(text) + doc := github.ParseMarkdown(text) var cleanBlock func(markdown.Block) cleanBlock = func(x markdown.Block) {