Skip to content

Commit

Permalink
Merge pull request #33 from chrishrb/32-syntax-highlighting-is-wrong-…
Browse files Browse the repository at this point in the history
…for-sql

32 Fix syntax highlighting
  • Loading branch information
chrishrb authored Jan 6, 2025
2 parents b5dd18a + 646a7ce commit d344b34
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
4 changes: 4 additions & 0 deletions defaults/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<link rel="icon" type="image/x-icon" href="/static/images/favicon.ico" />
{{if eq .Theme "dark" }}
<link rel="stylesheet" href="/static/css/github-markdown-dark.css" />
<style>{{ .CssCodeDark }}</style>
{{else if eq .Theme "light" }}
<link rel="stylesheet" href="/static/css/github-markdown-light.css" />
<style>{{ .CssCodeLight }}</style>
{{else}}
<link
rel="stylesheet"
Expand All @@ -19,6 +21,8 @@
href="/static/css/github-markdown-dark.css"
media="(prefers-color-scheme: dark)"
/>
<style media="(prefers-color-scheme: light)">{{ .CssCodeLight }}</style>
<style media="(prefers-color-scheme: dark)">{{ .CssCodeDark }}</style>
{{end}}
<link rel="stylesheet" href="/static/css/github-print.css" media="print" />
</head>
Expand Down
16 changes: 7 additions & 9 deletions pkg/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"regexp"
"strings"

"github.com/alecthomas/chroma/v2/quick"
chroma_html "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/chrishrb/go-grip/defaults"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
Expand Down Expand Up @@ -52,13 +54,6 @@ func (client *Client) renderHook(w io.Writer, node ast.Node, entering bool) (ast
func renderHookCodeBlock(w io.Writer, node ast.Node, theme string) (ast.WalkStatus, bool) {
block := node.(*ast.CodeBlock)

var style string
if theme == "dark" {
style = "github-dark"
} else {
style = "github"
}

if string(block.Info) == "mermaid" {
m, err := renderMermaid(string(block.Literal), theme)
if err != nil {
Expand All @@ -68,7 +63,10 @@ func renderHookCodeBlock(w io.Writer, node ast.Node, theme string) (ast.WalkStat
return ast.GoToNext, true
}

err := quick.Highlight(w, string(block.Literal), string(block.Info), "html", style)
lexer := lexers.Get(string(block.Info))
iterator, _ := lexer.Tokenise(nil, string(block.Literal))
formatter := chroma_html.New(chroma_html.WithClasses(true))
err := formatter.Format(w, styles.Fallback, iterator)
if err != nil {
log.Println("Error:", err)
}
Expand Down
27 changes: 23 additions & 4 deletions pkg/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ import (
"net/url"
"path"
"regexp"
"strings"
"text/template"

"github.com/aarol/reload"
chroma_html "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/styles"
"github.com/chrishrb/go-grip/defaults"
)

type htmlStruct struct {
Content string
Theme string
BoundingBox bool
Content string
Theme string
BoundingBox bool
CssCodeLight string
CssCodeDark string
}

func (client *Client) Serve(file string) error {
Expand Down Expand Up @@ -60,7 +65,13 @@ func (client *Client) Serve(file string) error {
htmlContent := client.MdToHTML(bytes)

// Serve
err = serveTemplate(w, htmlStruct{Content: string(htmlContent), Theme: client.Theme, BoundingBox: client.BoundingBox})
err = serveTemplate(w, htmlStruct{
Content: string(htmlContent),
Theme: client.Theme,
BoundingBox: client.BoundingBox,
CssCodeLight: getCssCode("github"),
CssCodeDark: getCssCode("github-dark"),
})
if err != nil {
log.Fatal(err)
return
Expand Down Expand Up @@ -123,3 +134,11 @@ func serveTemplate(w http.ResponseWriter, html htmlStruct) error {
err = tmpl.Execute(w, html)
return err
}

func getCssCode(style string) string {
buf := new(strings.Builder)
formatter := chroma_html.New(chroma_html.WithClasses(true))
s := styles.Get(style)
_ = formatter.WriteCSS(buf, s)
return buf.String()
}

0 comments on commit d344b34

Please sign in to comment.