From 1e974c647590878251bc394eae56447a60d336eb Mon Sep 17 00:00:00 2001 From: legitnull Date: Sun, 2 Apr 2023 13:59:42 -0600 Subject: [PATCH] added better markdown support using goldmark --- assets/_layout.html | 24 --------------------- assets/main.css | 52 --------------------------------------------- go.mod | 4 ++++ go.sum | 9 ++++++++ render.go | 22 ++++++++++++++++--- 5 files changed, 32 insertions(+), 79 deletions(-) delete mode 100644 assets/_layout.html delete mode 100644 assets/main.css diff --git a/assets/_layout.html b/assets/_layout.html deleted file mode 100644 index 18e5fdd..0000000 --- a/assets/_layout.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - TCP.WIKI - - - -
-

TCP.WIKI

- -
-
- {{.Content}} -
- - diff --git a/assets/main.css b/assets/main.css deleted file mode 100644 index 64d7ff4..0000000 --- a/assets/main.css +++ /dev/null @@ -1,52 +0,0 @@ -/* assets/main.css */ - -/* placeholder for better sytle */ -body { - font-family: Arial, sans-serif; - line-height: 1.6; - margin: 20px; - background-color: #131313; - color: #14ee00; -} - -h1, h2, h3, h4, h5, h6 { - font-weight: bold; -} - -a { - color: #059ce2; - text-decoration: none; -} - -a:hover { - color: #f700ff; -} - -header { - padding: 1em; - background-color: #5555; - border-bottom: 1px solid #555; -} - -header h1 { - display: inline-block; - margin-right: 1em; -} - -nav ul { - list-style: none; - padding: 0; -} - -nav ul li { - display: inline; - margin-right: 1em; -} - -/* Add the following rule to improve code block appearance */ -pre { - background-color: #5555; - padding: 1em; - border-radius: 3px; -} - diff --git a/go.mod b/go.mod index 18f9538..8d03577 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,9 @@ require ( github.com/Microsoft/go-winio v0.6.0 // indirect github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec // indirect github.com/acomagu/bufpipe v1.0.4 // indirect + github.com/alecthomas/chroma v0.10.0 // indirect github.com/cloudflare/circl v1.3.2 // indirect + github.com/dlclark/regexp2 v1.4.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.4.1 // indirect @@ -21,6 +23,8 @@ require ( github.com/skeema/knownhosts v1.1.0 // indirect github.com/src-d/gcfg v1.4.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect + github.com/yuin/goldmark v1.5.4 // indirect + github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 // indirect golang.org/x/crypto v0.7.0 // indirect golang.org/x/mod v0.9.0 // indirect golang.org/x/net v0.8.0 // indirect diff --git a/go.sum b/go.sum index c1d74f2..b77249f 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec/go.mod h1:8TI github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= +github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= @@ -18,6 +20,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= @@ -83,7 +87,12 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= +github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU= +github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg= +github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU= golang.org/x/arch v0.1.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/render.go b/render.go index f8ab888..7c97137 100644 --- a/render.go +++ b/render.go @@ -8,7 +8,9 @@ import ( "net/http" "path/filepath" - "github.com/russross/blackfriday/v2" + "github.com/yuin/goldmark" + highlighting "github.com/yuin/goldmark-highlighting" + "github.com/yuin/goldmark/extension" ) type Page struct { @@ -34,7 +36,21 @@ func renderPage(w http.ResponseWriter, localPath, filePath string) error { } func renderMarkdown(w http.ResponseWriter, content []byte) { - md := blackfriday.Run(content) + md := goldmark.New( + goldmark.WithExtensions( + extension.GFM, // GitHub Flavored Markdown + highlighting.NewHighlighting( + highlighting.WithStyle("monokai"), + ), + ), + ) + + var mdBuf bytes.Buffer + err := md.Convert(content, &mdBuf) + if err != nil { + http.Error(w, "Error converting Markdown", http.StatusInternalServerError) + return + } layout, err := ioutil.ReadFile(filepath.Join(localPath, "assets/_layout.html")) if err != nil { @@ -42,7 +58,7 @@ func renderMarkdown(w http.ResponseWriter, content []byte) { return } - page := &Page{Content: template.HTML(md)} + page := &Page{Content: template.HTML(mdBuf.String())} t, err := template.New("layout").Parse(string(layout)) if err != nil { http.Error(w, "Error parsing layout", http.StatusInternalServerError)