diff --git a/go.mod b/go.mod index 7ba453c..b0cbcdf 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 git.jlel.se/jlelse/template-strings v0.0.0-20210617205924-cfa3bd35ae40 github.com/PuerkitoBio/goquery v1.8.0 + github.com/alecthomas/chroma v0.9.4 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2 github.com/cretz/bine v0.2.0 @@ -48,6 +49,7 @@ require ( github.com/yuin/goldmark v1.4.4 // master github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38 + github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c @@ -58,6 +60,9 @@ require ( willnorris.com/go/microformats v1.1.2-0.20210827044458-ff2a6ae41971 ) +// Override some modules with own forks +replace github.com/yuin/goldmark-highlighting => github.com/jlelse/goldmark-highlighting v0.0.0-20211115195757-39f0fea96680 + require ( github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect @@ -66,6 +71,7 @@ require ( github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/coreos/go-iptables v0.6.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dlclark/regexp2 v1.4.0 // indirect github.com/dustin/go-humanize v1.0.0 // indirect github.com/felixge/httpsnoop v1.0.2 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect diff --git a/go.sum b/go.sum index 31b2d0a..4f04a8c 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg= github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= +github.com/alecthomas/chroma v0.9.4 h1:YL7sOAE3p8HS96T9km7RgvmsZIctqbK1qJ0b7hzed44= +github.com/alecthomas/chroma v0.9.4/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA= github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= @@ -111,6 +113,8 @@ github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE= +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/dmulholl/mp3lib v1.0.0 h1:PZq24kJBIk5zIxi/t6Qp8/EOAbAqThyrUCpkUKLBeWQ= github.com/dmulholl/mp3lib v1.0.0/go.mod h1:4RoA+iht/khfwxmH1ieoxZTzYVbb0am/zdvFkyGRr6I= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= @@ -281,6 +285,8 @@ github.com/jlaffaye/ftp v0.0.0-20211029032751-b1140299f4df h1:nsRFf9ZkcalB12ZJZY github.com/jlaffaye/ftp v0.0.0-20211029032751-b1140299f4df/go.mod h1:2lmrmq866uF2tnje75wQHzmPXhmSWUt7Gyx2vgK1RCU= github.com/jlelse/feeds v1.2.1-0.20210704161900-189f94254ad4 h1:d2oKwfgLl3ef0PyYDkzjsVyYlBZzNpOpXitDraOnVXc= github.com/jlelse/feeds v1.2.1-0.20210704161900-189f94254ad4/go.mod h1:vt0iOV52/wq97Ql/jp7mUkqyrlEiGQuhHic4bVoHy0c= +github.com/jlelse/goldmark-highlighting v0.0.0-20211115195757-39f0fea96680 h1:c8Vs/L9WqCXFY0LTJjgbzaRCadEmJZjMn6qY9XwZZNM= +github.com/jlelse/goldmark-highlighting v0.0.0-20211115195757-39f0fea96680/go.mod h1:jxhQhWF6W/ybgbZpMP2/Ui0D7b+x8ubBFipwXJJ1qME= github.com/joeshaw/gengen v0.0.0-20190604015154-c77d87825f5a/go.mod h1:v2qvRL8Xwk4OlARK6gPlf2JreZXzv0dYp/8+kUJ0y7Q= github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ= github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= diff --git a/markdown.go b/markdown.go index f1e9e81..93cd0bd 100644 --- a/markdown.go +++ b/markdown.go @@ -5,8 +5,10 @@ import ( "html/template" marktag "git.jlel.se/jlelse/goldmark-mark" + chromahtml "github.com/alecthomas/chroma/formatters/html" "github.com/yuin/goldmark" emoji "github.com/yuin/goldmark-emoji" + highlighting "github.com/yuin/goldmark-highlighting" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" @@ -35,6 +37,13 @@ func (a *goBlog) initMarkdown() { extension.Linkify, marktag.Mark, emoji.Emoji, + highlighting.NewHighlighting( + highlighting.WithStyle("monokai"), + highlighting.WithFormatOptions( + chromahtml.ClassPrefix("c-"), + chromahtml.WithClasses(true), + ), + ), ), } publicAddress := "" diff --git a/templateAssets.go b/templateAssets.go index 11b70be..2bc1633 100644 --- a/templateAssets.go +++ b/templateAssets.go @@ -10,6 +10,8 @@ import ( "path/filepath" "strings" + chromahtml "github.com/alecthomas/chroma/formatters/html" + "github.com/alecthomas/chroma/styles" "go.goblog.app/app/pkgs/contenttype" ) @@ -38,6 +40,11 @@ func (a *goBlog) initTemplateAssets() (err error) { if err != nil { return err } + // Add syntax highlighting CSS + err = a.initChromaCSS() + if err != nil { + return err + } return nil } @@ -107,3 +114,38 @@ func (a *goBlog) serveAsset(w http.ResponseWriter, r *http.Request) { w.Header().Set(contentType, af.contentType+contenttype.CharsetUtf8Suffix) _, _ = w.Write(af.body) } + +func (a *goBlog) initChromaCSS() error { + // Check if file already exists + if _, ok := a.assetFiles["css/chroma.css"]; ok { + return nil + } + // Initialize the style + chromaStyleBuilder := styles.Get("monokai").Builder() + chromaStyle, err := chromaStyleBuilder.Build() + if err != nil { + return err + } + // Create a temporary file + chromaTempFile, err := os.CreateTemp("", "chroma-*.css") + if err != nil { + return err + } + chromaTempFileName := chromaTempFile.Name() + // Write the CSS to the file + chromahtml.New( + chromahtml.ClassPrefix("c-"), + ).WriteCSS(chromaTempFile, chromaStyle) + // Close the file + _ = chromaTempFile.Close() + // Compile asset + compiled, err := a.compileAsset(chromaTempFileName) + _ = os.Remove(chromaTempFileName) + if err != nil { + return err + } + if compiled != "" { + a.assetFileNames["css/chroma.css"] = compiled + } + return nil +} diff --git a/templates/post.gohtml b/templates/post.gohtml index 268d2e1..775ef38 100644 --- a/templates/post.gohtml +++ b/templates/post.gohtml @@ -1,4 +1,5 @@ {{ define "title" }} + {{ with .Data.RenderedTitle }}{{ . }} - {{end}}{{ mdtitle .Blog.Title }} {{ include "postheadmeta" . }} {{ with shorturl .Data }}{{ end }}