Skip to content

Commit 3247481

Browse files
feat: Add --markdown-mode option (#61)
This change adds the new `--markdown-mode` boolean option, which allows users to force output to be rendered in `markdown` mode[1], rather than in the default `gfm` mode. [1] https://docs.github.com/en/rest/markdown/markdown#render-a-markdown-document--parameters
1 parent 5030c99 commit 3247481

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Available options:
5757

5858
```text
5959
--dark-mode Force dark mode
60+
--markdown-mode Force "markdown" mode (rather than default "gfm")
6061
--disable-auto-open Disable auto opening your browser
6162
--disable-reload Disable live reloading
6263
-h, --help help for gh-markdown-preview

cmd/app.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ func findReadme(dir string) (string, error) {
4343
return "", err
4444
}
4545

46-
func toHTML(markdown string) (string, error) {
47-
sout, _, err := gh("api", "-X", "POST", "/markdown", "-f", fmt.Sprintf("text=%s", markdown), "-f", "mode=gfm")
46+
func toHTML(markdown string, param *Param) (string, error) {
47+
mode := "gfm"
48+
if param.markdownMode {
49+
mode = "markdown"
50+
}
51+
sout, _, err := gh("api", "-X", "POST", "/markdown", "-f", fmt.Sprintf("text=%s", markdown), "-f", fmt.Sprintf("mode=%s", mode))
4852
if err != nil {
4953
return "", err
5054
}

cmd/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var verbose = false
1414

1515
type Param struct {
1616
filename string
17+
markdownMode bool
1718
reload bool
1819
forceLightMode bool
1920
forceDarkMode bool
@@ -52,6 +53,8 @@ var rootCmd = &cobra.Command{
5253
forceLightMode, _ := cmd.Flags().GetBool("light-mode")
5354
forceDarkMode, _ := cmd.Flags().GetBool("dark-mode")
5455

56+
markdownMode, _ := cmd.Flags().GetBool("markdown-mode")
57+
5558
disableAutoOpen, _ := cmd.Flags().GetBool("disable-auto-open")
5659
autoOpen := true
5760
if disableAutoOpen {
@@ -60,6 +63,7 @@ var rootCmd = &cobra.Command{
6063

6164
param := &Param{
6265
filename: filename,
66+
markdownMode: markdownMode,
6367
reload: reload,
6468
forceLightMode: forceLightMode,
6569
forceDarkMode: forceDarkMode,
@@ -85,6 +89,7 @@ func init() {
8589
rootCmd.Flags().StringP("host", "", "localhost", "Hostname this server will bind")
8690
rootCmd.Flags().BoolP("version", "", false, "Show the version")
8791
rootCmd.Flags().BoolP("disable-reload", "", false, "Disable live reloading")
92+
rootCmd.Flags().BoolP("markdown-mode", "", false, "Force \"markdown\" mode (rather than default \"gfm\")")
8893
rootCmd.Flags().BoolP("disable-auto-open", "", false, "Disable auto opening your browser")
8994
rootCmd.Flags().BoolP("verbose", "", false, "Show verbose output")
9095
rootCmd.Flags().BoolP("light-mode", "", false, "Force light mode")

cmd/server.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (server *Server) Serve(param *Param) error {
4949

5050
r := http.NewServeMux()
5151
r.Handle("/", wrapHandler(handler(filename, param, http.FileServer(http.Dir(dir)))))
52-
r.Handle("/__/md", wrapHandler(mdHandler(filename)))
52+
r.Handle("/__/md", wrapHandler(mdHandler(filename, param)))
5353

5454
watcher, err := createWatcher(dir)
5555
if err != nil {
@@ -102,7 +102,7 @@ func handler(filename string, param *Param, h http.Handler) http.Handler {
102102
return
103103
}
104104

105-
html, err := toHTML(markdown)
105+
html, err := toHTML(markdown, param)
106106
if err != nil {
107107
http.Error(w, err.Error(), http.StatusInternalServerError)
108108
return
@@ -116,7 +116,7 @@ func handler(filename string, param *Param, h http.Handler) http.Handler {
116116
})
117117
}
118118

119-
func mdResponse(w http.ResponseWriter, filename string) {
119+
func mdResponse(w http.ResponseWriter, filename string, param *Param) {
120120
w.Header().Set("Content-Type", "text/html; charset=utf-8")
121121

122122
markdown, err := slurp(filename)
@@ -125,7 +125,7 @@ func mdResponse(w http.ResponseWriter, filename string) {
125125
return
126126
}
127127

128-
html, err := toHTML(markdown)
128+
html, err := toHTML(markdown, param)
129129
if err != nil {
130130
http.Error(w, err.Error(), 500)
131131
return
@@ -134,13 +134,13 @@ func mdResponse(w http.ResponseWriter, filename string) {
134134

135135
}
136136

137-
func mdHandler(filename string) http.Handler {
137+
func mdHandler(filename string, param *Param) http.Handler {
138138
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
139139
pathParam := r.URL.Query().Get("path")
140140
if pathParam != "" {
141-
mdResponse(w, pathParam)
141+
mdResponse(w, pathParam, param)
142142
} else {
143-
mdResponse(w, filename)
143+
mdResponse(w, filename, param)
144144
}
145145
})
146146
}

0 commit comments

Comments
 (0)