Skip to content

Commit

Permalink
feat: Improve README.md rendering and add new flags for customization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chrishrb committed Oct 9, 2024
1 parent 40fa2a7 commit 9bb188e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@
</p>
</div>

## Getting started
## 🚀 Getting started

To install go-grip, simply:

```bash
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:

Expand All @@ -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:
Expand All @@ -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.
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
1 change: 1 addition & 0 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Client struct {
Dark bool
OpenBrowser bool
Port int
OpenReadme bool
}
18 changes: 15 additions & 3 deletions pkg/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9bb188e

Please sign in to comment.