Skip to content

Commit

Permalink
internal/github, etc.: unify GH markdown parsing
Browse files Browse the repository at this point in the history
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 <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
jba committed Dec 16, 2024
1 parent e238ae9 commit 00be025
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
8 changes: 1 addition & 7 deletions internal/commentfix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/gaby/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions internal/github/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
9 changes: 1 addition & 8 deletions internal/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,7 @@ var htmlCommentRegexp = regexp.MustCompile(`<!--(\n|.)*?-->`)
// 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) {
Expand Down

0 comments on commit 00be025

Please sign in to comment.