improve usability
This commit is contained in:
BIN
.github/logo-1.png
vendored
Normal file
BIN
.github/logo-1.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 41 KiB |
@@ -1,7 +1,7 @@
|
|||||||
<br />
|
<br />
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<a href="#">
|
<a href="#">
|
||||||
<img src=".github/assets/nix-dots.png" alt="Logo" height="80">
|
<img src=".github/logo-1.png" alt="Logo" height="80">
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,7 @@ var rootCmd = &cobra.Command{
|
|||||||
|
|
||||||
client := pkg.Client{Dark: dark, OpenBrowser: browser, Port: port}
|
client := pkg.Client{Dark: dark, OpenBrowser: browser, Port: port}
|
||||||
|
|
||||||
bytes, err := client.MdToHTML(args[0])
|
err := client.Serve(args[0])
|
||||||
cobra.CheckErr(err)
|
|
||||||
|
|
||||||
err = client.Serve(bytes)
|
|
||||||
cobra.CheckErr(err)
|
cobra.CheckErr(err)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package pkg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"log"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma/v2/quick"
|
"github.com/alecthomas/chroma/v2/quick"
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
@@ -11,12 +11,7 @@ import (
|
|||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (client *Client) MdToHTML(filename string) ([]byte, error) {
|
func (client *Client) MdToHTML(bytes []byte) []byte {
|
||||||
bytes, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
||||||
p := parser.NewWithExtensions(extensions)
|
p := parser.NewWithExtensions(extensions)
|
||||||
doc := p.Parse(bytes)
|
doc := p.Parse(bytes)
|
||||||
@@ -25,7 +20,7 @@ func (client *Client) MdToHTML(filename string) ([]byte, error) {
|
|||||||
opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: client.renderHookCodeBlock}
|
opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: client.renderHookCodeBlock}
|
||||||
renderer := html.NewRenderer(opts)
|
renderer := html.NewRenderer(opts)
|
||||||
|
|
||||||
return markdown.Render(doc, renderer), err
|
return markdown.Render(doc, renderer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) renderHookCodeBlock(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
func (client *Client) renderHookCodeBlock(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
@@ -42,7 +37,10 @@ func (client *Client) renderHookCodeBlock(w io.Writer, node ast.Node, entering b
|
|||||||
style = "github"
|
style = "github"
|
||||||
}
|
}
|
||||||
|
|
||||||
quick.Highlight(w, string(block.Literal), string(block.Info), "html", style)
|
err := quick.Highlight(w, string(block.Literal), string(block.Info), "html", style)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error:", err)
|
||||||
|
}
|
||||||
|
|
||||||
return ast.GoToNext, true
|
return ast.GoToNext, true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,29 +16,62 @@ type htmlStruct struct {
|
|||||||
Darkmode bool
|
Darkmode bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Serve(htmlContent []byte) error {
|
func (client *Client) Serve(file string) error {
|
||||||
// Serve static files
|
dir := http.Dir("./")
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
chttp := http.NewServeMux()
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
chttp.Handle("/", http.FileServer(dir))
|
||||||
|
|
||||||
|
// Regex for markdown
|
||||||
|
regex := regexp.MustCompile(`(?i)\.md$`)
|
||||||
|
|
||||||
// Serve website with rendered markdown
|
// Serve website with rendered markdown
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := serveTemplate(w, htmlStruct{Content: string(htmlContent), Darkmode: client.Dark})
|
if regex.MatchString(r.URL.Path) {
|
||||||
if err != nil {
|
// Open file and convert to html
|
||||||
log.Fatal(err)
|
bytes, err := readToString(dir, r.URL.Path)
|
||||||
|
htmlContent := client.MdToHTML(bytes)
|
||||||
|
|
||||||
|
// Serve
|
||||||
|
err = serveTemplate(w, htmlStruct{Content: string(htmlContent), Darkmode: client.Dark})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chttp.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
addr := fmt.Sprintf("http://localhost:%d", client.Port)
|
addr := fmt.Sprintf("http://localhost:%d/", client.Port)
|
||||||
fmt.Printf("Starting server: %s\n", addr)
|
fmt.Printf("Starting server: %s\n", addr)
|
||||||
|
|
||||||
if client.OpenBrowser {
|
if file != "" {
|
||||||
Open(addr)
|
addr = path.Join(addr, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.ListenAndServe(fmt.Sprintf(":%d", client.Port), nil)
|
if client.OpenBrowser {
|
||||||
|
err := Open(addr)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
err := http.ListenAndServe(fmt.Sprintf(":%d", client.Port), nil)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func readToString(dir http.Dir, filename string) ([]byte, error) {
|
||||||
|
f, err := dir.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, err = buf.ReadFrom(f)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveTemplate(w http.ResponseWriter, html htmlStruct) error {
|
func serveTemplate(w http.ResponseWriter, html htmlStruct) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user