From 9bb188e2785943cac57ec2cf1f447cada962ecdb Mon Sep 17 00:00:00 2001 From: Christoph Herb <52382992+chrishrb@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:13:28 +0200 Subject: [PATCH] feat: Improve README.md rendering and add new flags for customization Added support for rendering the `README.md` file with a markdown parser, allowing users to customize the behavior with new flags. The `-r=false` flag can be used to disable rendering of the README.md file if no file is provided. Updated the `cmd/root.go` file to include the new flags and updated the `pkg/client.go` file to reflect the changes in the client struct. --- README.md | 16 +++++++++------- cmd/root.go | 4 +++- pkg/client.go | 1 + pkg/webserver.go | 18 +++++++++++++++--- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2276b7c..b1208fe 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@

-## Getting started +## 🚀 Getting started To install go-grip, simply: @@ -20,15 +20,17 @@ To install go-grip, simply: go install github.com/chrishrb/go-grip@latest ``` -## Usage +## 🔨 Usage -To render a markdown file simply execute: +To render the `README.md` file simply execute: ```bash go-grip README.md +# or +go-grip ``` -The browser will automatically be opened on http://localhost:6419. You can disable this behaviour with the `-b=false` option. +The browser will automatically open on http://localhost:6419. You can disable this behaviour with the `-b=false` option. You can also specify a port: @@ -39,7 +41,7 @@ go-grip -p 80 README.md or just open a file-tree with all available files in the current directory: ```bash -go-grip +go-grip -r=false ``` It's also possible to activate the darkmode: @@ -50,10 +52,10 @@ go-grip -d . To terminate the current server simply press `CTRL-C`. -## Examples +## 📝 Examples examples -## Similar tools +## 📌 Similar tools This tool is, like the name already says, a reimplementation of [grip](https://github.com/joeyespo/grip) in go and without using the web API of GitHub. diff --git a/cmd/root.go b/cmd/root.go index 2b346cc..56bcb0b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,9 +14,10 @@ var rootCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { dark, _ := cmd.Flags().GetBool("dark") browser, _ := cmd.Flags().GetBool("browser") + openReadme, _ := cmd.Flags().GetBool("readme") port, _ := cmd.Flags().GetInt("port") - client := pkg.Client{Dark: dark, OpenBrowser: browser, Port: port} + client := pkg.Client{Dark: dark, OpenBrowser: browser, Port: port, OpenReadme: openReadme} var file string if len(args) == 1 { @@ -37,5 +38,6 @@ func Execute() { func init() { rootCmd.Flags().BoolP("dark", "d", false, "Activate darkmode") rootCmd.Flags().BoolP("browser", "b", true, "Open new browser tab") + rootCmd.Flags().BoolP("readme", "r", true, "Open readme if no file provided") rootCmd.Flags().IntP("port", "p", 6419, "Port to use") } diff --git a/pkg/client.go b/pkg/client.go index 9277d68..c336197 100644 --- a/pkg/client.go +++ b/pkg/client.go @@ -4,4 +4,5 @@ type Client struct { Dark bool OpenBrowser bool Port int + OpenReadme bool } diff --git a/pkg/webserver.go b/pkg/webserver.go index cfc12e4..13f316e 100644 --- a/pkg/webserver.go +++ b/pkg/webserver.go @@ -32,7 +32,10 @@ func (client *Client) Serve(file string) error { // Serve website with rendered markdown http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if regex.MatchString(r.URL.Path) { + f, err := dir.Open(r.URL.Path) + defer f.Close() + + if err == nil && regex.MatchString(r.URL.Path) { // Open file and convert to html bytes, err := readToString(dir, r.URL.Path) htmlContent := client.MdToHTML(bytes) @@ -48,10 +51,19 @@ func (client *Client) Serve(file string) error { }) addr := fmt.Sprintf("http://localhost:%d", client.Port) - if file != "" { + if file == "" { + // If README.md exists then open README.md at beginning + readme := "README.md" + f, err := dir.Open(readme) + defer f.Close() + if err == nil && client.OpenReadme { + addr = path.Join(addr, readme) + } + } else { addr = path.Join(addr, file) } - fmt.Printf("Starting server: %s\n", addr) + + fmt.Printf("🚀 Starting server: %s\n", addr) if client.OpenBrowser { err := Open(addr)