Skip to content
Open
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
50 changes: 50 additions & 0 deletions gno.land/pkg/gnoweb/markdown/ext_forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
formInputTag = "gno-input"
formTextareaTag = "gno-textarea"
formSelectTag = "gno-select"
formMarkdownTag = "gno-markdown"

formDefaultInputType = "text"
formDefaultPlaceholder = "Enter value"
Expand Down Expand Up @@ -54,6 +55,20 @@
}
)

// FormMarkdown represents a block of custom markdown content embedded
type FormMarkdown struct {
Raw string // Raw markdown content inside <gno-markdown>

Check failure on line 60 in gno.land/pkg/gnoweb/markdown/ext_forms.go

View workflow job for this annotation

GitHub Actions / Run gno.land suite / Go Lint / lint

File is not properly formatted (goimports)

Check failure on line 60 in gno.land/pkg/gnoweb/markdown/ext_forms.go

View workflow job for this annotation

GitHub Actions / Run gno.land suite / Go Lint / lint

File is not properly formatted (gofmt)
Error error // Parsing/Randering error (if any)
}
func (e FormMarkdown) GetName() string { return "" }
func (e FormMarkdown) GetError() error { return e.Error }
func (e FormMarkdown) String() string {
if e.Error != nil {
return fmt.Sprintf("(err=%s)", e.Error)
}
return "(markdown)"
}

// FormElement represents any form element
type FormElement interface {
GetName() string
Expand Down Expand Up @@ -265,6 +280,8 @@
p.parseTextarea(formNode, tok)
case formSelectTag:
p.parseSelect(formNode, tok)
case formMarkdownTag:
p.parseMarkdown(formNode, reader)
default:
formNode.addElement(FormInput{Error: ErrFormInvalidTag})
}
Expand Down Expand Up @@ -486,6 +503,8 @@
r.renderSelect(w, n.Elements, e, i, &lastDescID, isExec)
renderedSelects[e.Name] = true
}
case FormMarkdown:
r.renderMarkdown(w, e)
}
}

Expand Down Expand Up @@ -744,3 +763,34 @@
}
return toks[0], true
}

func (p *FormParser) parseMarkdown(node *FormNode, reader text.Reader) {
var buf bytes.Buffer
reader.AdvanceLine()
for {
line, _ := reader.PeekLine()
if bytes.Contains(line, []byte("</gno-markdown>")) {
reader.AdvanceLine()
break
}
buf.Write(line)
reader.AdvanceLine()
}
node.addElement(FormMarkdown{
Raw: strings.TrimSpace(buf.String()),
})
}

func (r *FormRenderer) renderMarkdown(w util.BufWriter, e FormMarkdown) {
if e.Error != nil {
fmt.Fprintf(w, "Error: %s\n", HTMLEscapeString(e.Error.Error()))
return
}
var out bytes.Buffer
md := goldmark.New()
if err := md.Convert([]byte(e.Raw), &out); err != nil {
fmt.Fprintf(w, "Markdown error: %s\n", HTMLEscapeString(err.Error()))
return
}
fmt.Fprintf(w, `<div class="gno-form_markdown">%s</div>`+"\n", out.String())
}
Loading