summaryrefslogtreecommitdiff
path: root/html/highlight.go
diff options
context:
space:
mode:
authora73x <[email protected]>2024-12-29 19:13:27 +0000
committera73x <[email protected]>2024-12-30 11:00:49 +0000
commita783270b09af3d873c08a01d13f802018b69fb02 (patch)
treebdac4e38357afa535515dd8dda790d7193f371d0 /html/highlight.go
parent71513b80ebc21240b5b68d8bbbf8b7ee2f54893e (diff)
new markdown renderer
since TOC has a title now and it can compact toc headers, we can use header 2 for everything use the built in goldmark extension for syntax highlighting
Diffstat (limited to 'html/highlight.go')
-rw-r--r--html/highlight.go79
1 files changed, 0 insertions, 79 deletions
diff --git a/html/highlight.go b/html/highlight.go
index d198ad0..621abdb 100644
--- a/html/highlight.go
+++ b/html/highlight.go
@@ -1,80 +1 @@
package html
-
-import (
- "io"
-
- "github.com/gomarkdown/markdown/ast"
- mdhtml "github.com/gomarkdown/markdown/html"
-
- "github.com/alecthomas/chroma"
- "github.com/alecthomas/chroma/formatters/html"
- "github.com/alecthomas/chroma/lexers"
- "github.com/alecthomas/chroma/styles"
-)
-
-type Highlighter struct {
- htmlFormatter *html.Formatter
- highlightStyle *chroma.Style
-}
-
-func (h Highlighter) htmlHighlight(w io.Writer, source, lang, defaultLang string) error {
- if lang == "" {
- lang = defaultLang
- }
-
- l := lexers.Get(lang)
- if l == nil {
- l = lexers.Analyse(source)
- }
- if l == nil {
- l = lexers.Fallback
- }
- l = chroma.Coalesce(l)
-
- it, err := l.Tokenise(nil, source)
- if err != nil {
- return err
- }
- return h.htmlFormatter.Format(w, h.highlightStyle, it)
-}
-
-func (h Highlighter) renderCode(w io.Writer, codeBlock *ast.CodeBlock, entering bool) {
- defaultLang := ""
- lang := string(codeBlock.Info)
- h.htmlHighlight(w, string(codeBlock.Literal), lang, defaultLang)
-}
-
-func (h Highlighter) myRenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
- if code, ok := node.(*ast.CodeBlock); ok {
- h.renderCode(w, code, entering)
- return ast.GoToNext, true
- }
- return ast.GoToNext, false
-}
-
-type opts struct {
-}
-
-func newRenderer(hasToc bool) *mdhtml.Renderer {
-
- htmlFormatter := html.New(html.WithClasses(true), html.TabWidth(2))
-
- styleName := "monokailight"
- highlightStyle := styles.Get(styleName)
-
- h := Highlighter{
- htmlFormatter: htmlFormatter,
- highlightStyle: highlightStyle,
- }
-
- flags := mdhtml.CommonFlags | mdhtml.FootnoteReturnLinks
- if hasToc {
- flags = flags | mdhtml.TOC
- }
-
- opts := mdhtml.RendererOptions{
- Flags: flags,
- RenderNodeHook: h.myRenderHook,
- }
- return mdhtml.NewRenderer(opts)
-}