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.
This commit is contained in:
Christoph Herb
2024-10-10 00:13:28 +02:00
parent 40fa2a7b86
commit 9bb188e278
4 changed files with 28 additions and 11 deletions

View File

@@ -12,7 +12,7 @@
</p>
</div>
## 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
<img src="./.github/docs/examples.png" alt="examples" width="1000"/>
## 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.

View File

@@ -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")
}

View File

@@ -4,4 +4,5 @@ type Client struct {
Dark bool
OpenBrowser bool
Port int
OpenReadme bool
}

View File

@@ -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)