Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnoweb): disable html in markdown #2964

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/gno/contribs/gnodev

go 1.22
go 1.22.0
moul marked this conversation as resolved.
Show resolved Hide resolved

replace github.com/gnolang/gno => ../..

Expand Down
1 change: 1 addition & 0 deletions gno.land/cmd/gnoweb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func runMain(args []string) error {
fs.StringVar(&cfg.HelpRemote, "help-remote", cfg.HelpRemote, "help page's remote addr")
fs.BoolVar(&cfg.WithAnalytics, "with-analytics", cfg.WithAnalytics, "enable privacy-first analytics")
fs.StringVar(&bindAddress, "bind", "127.0.0.1:8888", "server listening address")
fs.BoolVar(&cfg.WithHTML, "with-html", cfg.WithHTML, "Enable HTML parsing in markdown rendering")

if err := fs.Parse(args); err != nil {
return err
Expand Down
35 changes: 33 additions & 2 deletions gno.land/pkg/gnoweb/gnoweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -45,6 +46,7 @@
HelpChainID string
HelpRemote string
WithAnalytics bool
WithHTML bool
}

func NewDefaultConfig() Config {
Expand All @@ -56,6 +58,7 @@
HelpChainID: "dev",
HelpRemote: "127.0.0.1:26657",
WithAnalytics: false,
WithHTML: false,
}
}

Expand Down Expand Up @@ -109,6 +112,34 @@
return app
}

var (
inlineCodePattern = regexp.MustCompile("`[^`]*`")
htmlTagPattern = regexp.MustCompile(`<\/?\w+[^>]*?>`)
)

func sanitizeContent(cfg *Config, content string) string {
if cfg.WithHTML {
return content
}

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

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L122-L123

Added lines #L122 - L123 were not covered by tests

placeholders := map[string]string{}
contentWithPlaceholders := inlineCodePattern.ReplaceAllStringFunc(content, func(match string) string {
placeholder := fmt.Sprintf("__GNOMDCODE_%d__", len(placeholders))
placeholders[placeholder] = match
return placeholder
})

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

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L127-L130

Added lines #L127 - L130 were not covered by tests

sanitizedContent := htmlTagPattern.ReplaceAllString(contentWithPlaceholders, "")

if len(placeholders) > 0 {
for placeholder, code := range placeholders {
sanitizedContent = strings.ReplaceAll(sanitizedContent, placeholder, code)
}

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

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoweb/gnoweb.go#L135-L137

Added lines #L135 - L137 were not covered by tests
}

return sanitizedContent
}

// handlerRealmAlias is used to render official pages from realms.
// url is intended to be shorter.
// UX is intended to be more minimalistic.
Expand Down Expand Up @@ -151,7 +182,7 @@
tmpl.Set("RealmPath", rlmpath)
tmpl.Set("Query", querystr)
tmpl.Set("PathLinks", pathLinks)
tmpl.Set("Contents", string(res.Data))
tmpl.Set("Contents", sanitizeContent(cfg, string(res.Data)))
tmpl.Set("Config", cfg)
tmpl.Set("IsAlias", true)
tmpl.Render(w, r, "realm_render.html", "funcs.html")
Expand Down Expand Up @@ -339,7 +370,7 @@
tmpl.Set("RealmPath", rlmpath)
tmpl.Set("Query", querystr)
tmpl.Set("PathLinks", pathLinks)
tmpl.Set("Contents", string(res.Data))
tmpl.Set("Contents", sanitizeContent(cfg, string(res.Data)))
tmpl.Set("Config", cfg)
tmpl.Set("HasReadme", hasReadme)
tmpl.Render(w, r, "realm_render.html", "funcs.html")
Expand Down
Loading