forked from mirror/oddmu
Compare commits
1 Commits
svg-saniti
...
gemtext
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
643b33f4a3 |
26
wiki.go
26
wiki.go
@@ -4,7 +4,9 @@ import (
|
||||
"os"
|
||||
"fmt"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -28,10 +30,17 @@ func (p *Page) save() error {
|
||||
func loadPage(title string) (*Page, error) {
|
||||
filename := title + ".md"
|
||||
body, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if err == nil {
|
||||
return &Page{Title: title, Body: body}, nil
|
||||
}
|
||||
return &Page{Title: title, Body: body}, nil
|
||||
filename = title + ".gmi"
|
||||
body, err = os.ReadFile(filename)
|
||||
if err == nil {
|
||||
re := regexp.MustCompile(`(?m)^=>\s*(\S+)\s+(.+)`)
|
||||
body = []byte(re.ReplaceAllString(string(body), `* [$2]($1)`))
|
||||
return &Page{Title: title, Body: body}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
|
||||
@@ -51,7 +60,14 @@ func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||
return
|
||||
}
|
||||
maybeUnsafeHTML := markdown.ToHTML(p.Body, nil, nil)
|
||||
extensions := parser.CommonExtensions | parser.NoEmptyLineBeforeBlock
|
||||
markdownParser := parser.NewWithExtensions(extensions)
|
||||
flags := html.CommonFlags
|
||||
opts := html.RendererOptions{
|
||||
Flags: flags,
|
||||
}
|
||||
htmlRenderer := html.NewRenderer(opts)
|
||||
maybeUnsafeHTML := markdown.ToHTML(p.Body, markdownParser, htmlRenderer)
|
||||
html := bluemonday.UGCPolicy().SanitizeBytes(maybeUnsafeHTML)
|
||||
p.Html = template.HTML(html);
|
||||
renderTemplate(w, "view", p)
|
||||
|
||||
Reference in New Issue
Block a user