Skip to content

Commit

Permalink
feat: highlight md
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiscolin committed Dec 12, 2024
1 parent cbc4f9b commit f93398f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
24 changes: 17 additions & 7 deletions gno.land/pkg/gnoweb/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/alecthomas/chroma/v2/styles"
"github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
mdhtml "github.com/yuin/goldmark/renderer/html"
)

Expand Down Expand Up @@ -58,10 +59,24 @@ func mustGetStyle(name string) *chroma.Style {

// NewRouter initializes the gnoweb router, with the given logger and config.
func NewRouter(logger *slog.Logger, cfg *AppConfig) (http.Handler, error) {
mdopts := []goldmark.Option{}
chromaOptions := []chromahtml.Option{
chromahtml.WithLineNumbers(true),
chromahtml.WithLinkableLineNumbers(true, "L"),
chromahtml.WithClasses(true),
chromahtml.ClassPrefix("chroma-"),
}

mdopts := []goldmark.Option{
goldmark.WithExtensions(
highlighting.NewHighlighting(
highlighting.WithFormatOptions(chromaOptions...),
),
),
}
if cfg.UnsafeHTML {
mdopts = append(mdopts, goldmark.WithRendererOptions(mdhtml.WithXHTML(), mdhtml.WithUnsafe()))
}

Check warning on line 78 in gno.land/pkg/gnoweb/app.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/app.go#L77-L78

Added lines #L77 - L78 were not covered by tests

md := goldmark.New(mdopts...)

client, err := client.NewHTTPClient(cfg.NodeRemote)
Expand All @@ -70,12 +85,7 @@ func NewRouter(logger *slog.Logger, cfg *AppConfig) (http.Handler, error) {
}

Check warning on line 85 in gno.land/pkg/gnoweb/app.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/app.go#L84-L85

Added lines #L84 - L85 were not covered by tests
webcli := NewWebClient(logger, client, md)

formatter := chromahtml.New(
chromahtml.WithLineNumbers(true),
chromahtml.WithLinkableLineNumbers(true, "L"),
chromahtml.WithClasses(true),
chromahtml.ClassPrefix("chroma-"),
)
formatter := chromahtml.New(chromaOptions...)
chromaStylePath := path.Join(cfg.AssetsPath, "_chroma", "style.css")

var webConfig WebHandlerConfig
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f93398f

Please sign in to comment.