Skip to content

Commit

Permalink
feat: Use gfm mode (#54)
Browse files Browse the repository at this point in the history
* Use `gfm` mode by default

* Add tests
  • Loading branch information
anurag-roy authored Jul 30, 2024
1 parent 4864d65 commit dd62f13
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func findReadme(dir string) (string, error) {
}

func toHTML(markdown string) (string, error) {
sout, _, err := gh("api", "-X", "POST", "/markdown", "-f", fmt.Sprintf("text=%s", markdown))
sout, _, err := gh("api", "-X", "POST", "/markdown", "-f", fmt.Sprintf("text=%s", markdown), "-f", "mode=gfm")
if err != nil {
return "", err
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,54 @@ func TestToHTML(t *testing.T) {
t.Errorf("got %v\n want %v", actual, expected)
}
}

func TestGfmCheckboxes(t *testing.T) {
string, err := slurp("../testdata/gfm-checkboxes.md")
if err != nil {
t.Errorf(err.Error())
}
html, err := toHTML(string)
if err != nil {
t.Errorf(err.Error())
}
actual := strings.TrimSpace(html)

checkBoxes := 0
checkedCheckBoxes := 0
uncheckedCheckBoxes := 0
for _, line := range strings.Split(actual, "\n") {
if strings.Contains(line, "<input type=\"checkbox\"") {
checkBoxes++
if strings.Contains(line, "checked") {
checkedCheckBoxes++
} else {
uncheckedCheckBoxes++
}
}
}
if checkBoxes != 2 {
t.Errorf("got %v checkboxes, want 2", checkBoxes)
}
if checkedCheckBoxes != 1 {
t.Errorf("got %v checked checkboxes, want 1", checkedCheckBoxes)
}
if uncheckedCheckBoxes != 1 {
t.Errorf("got %v unchecked checkboxes, want 1", uncheckedCheckBoxes)
}
}

func TestGfmAlerts(t *testing.T) {
string, err := slurp("../testdata/gfm-alerts.md")
if err != nil {
t.Errorf(err.Error())
}
html, err := toHTML(string)
if err != nil {
t.Errorf(err.Error())
}
actual := strings.TrimSpace(html)

if strings.Contains(actual, "<blockquote") {
t.Error("got blockquote tag instead of alerts")
}
}
6 changes: 3 additions & 3 deletions cmd/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>{{ .Title }}</title>
<link rel="icon" type="image/svg+xml" href="https://github.githubassets.com/favicons/favicon.svg">
{{ $cssURL :=
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.0.0/github-markdown.min.css" }} {{
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.6.1/github-markdown.min.css" }} {{
if eq .Mode "dark" }} {{ $cssURL =
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.0.0/github-markdown-dark.min.css"
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.6.1/github-markdown-dark.min.css"
}} {{ else if eq .Mode "light" }} {{ $cssURL =
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.0.0/github-markdown-light.min.css"
"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.6.1/github-markdown-light.min.css"
}}{{ end }}
<link rel="stylesheet" href="{{ $cssURL }}" />
<style>
Expand Down
14 changes: 14 additions & 0 deletions testdata/gfm-alerts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
> [!NOTE]
> Useful information that users should know, even when skimming content.
> [!TIP]
> Helpful advice for doing things better or more easily.
> [!IMPORTANT]
> Key information users need to know to achieve their goal.
> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.
> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.
2 changes: 2 additions & 0 deletions testdata/gfm-checkboxes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ ] Not done
- [x] Done

0 comments on commit dd62f13

Please sign in to comment.