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 />
|
||||
<div align="center">
|
||||
<a href="#">
|
||||
<img src=".github/assets/nix-dots.png" alt="Logo" height="80">
|
||||
<img src=".github/logo-1.png" alt="Logo" height="80">
|
||||
</a>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -18,10 +18,7 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
client := pkg.Client{Dark: dark, OpenBrowser: browser, Port: port}
|
||||
|
||||
bytes, err := client.MdToHTML(args[0])
|
||||
cobra.CheckErr(err)
|
||||
|
||||
err = client.Serve(bytes)
|
||||
err := client.Serve(args[0])
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package pkg
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"log"
|
||||
|
||||
"github.com/alecthomas/chroma/v2/quick"
|
||||
"github.com/gomarkdown/markdown"
|
||||
@@ -11,12 +11,7 @@ import (
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
)
|
||||
|
||||
func (client *Client) MdToHTML(filename string) ([]byte, error) {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (client *Client) MdToHTML(bytes []byte) []byte {
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
doc := p.Parse(bytes)
|
||||
@@ -25,7 +20,7 @@ func (client *Client) MdToHTML(filename string) ([]byte, error) {
|
||||
opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: client.renderHookCodeBlock}
|
||||
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) {
|
||||
@@ -42,7 +37,10 @@ func (client *Client) renderHookCodeBlock(w io.Writer, node ast.Node, entering b
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
@@ -13,29 +16,62 @@ type htmlStruct struct {
|
||||
Darkmode bool
|
||||
}
|
||||
|
||||
func (client *Client) Serve(htmlContent []byte) error {
|
||||
// Serve static files
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
func (client *Client) Serve(file string) error {
|
||||
dir := http.Dir("./")
|
||||
chttp := http.NewServeMux()
|
||||
chttp.Handle("/", http.FileServer(dir))
|
||||
|
||||
// Regex for markdown
|
||||
regex := regexp.MustCompile(`(?i)\.md$`)
|
||||
|
||||
// Serve website with rendered markdown
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
err := serveTemplate(w, htmlStruct{Content: string(htmlContent), Darkmode: client.Dark})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
if regex.MatchString(r.URL.Path) {
|
||||
// Open file and convert to html
|
||||
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)
|
||||
|
||||
if client.OpenBrowser {
|
||||
Open(addr)
|
||||
if file != "" {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user