mirror of
https://github.com/TECHNOFAB11/docsonnet.git
synced 2025-12-12 06:20:12 +01:00
feat: rewrite render
This commit is contained in:
parent
5fbee517fe
commit
7308eecaf3
10 changed files with 352 additions and 198 deletions
130
pkg/md/md.go
Normal file
130
pkg/md/md.go
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
package md
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Elem interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type JoinType struct {
|
||||
elems []Elem
|
||||
with string
|
||||
}
|
||||
|
||||
func (p JoinType) String() string {
|
||||
s := ""
|
||||
for _, e := range p.elems {
|
||||
s += p.with + e.String()
|
||||
}
|
||||
return strings.TrimPrefix(s, p.with)
|
||||
}
|
||||
|
||||
func Paragraph(elems ...Elem) JoinType {
|
||||
return JoinType{elems: elems, with: " "}
|
||||
}
|
||||
|
||||
func Doc(elems ...Elem) JoinType {
|
||||
return JoinType{elems: elems, with: "\n\n"}
|
||||
}
|
||||
|
||||
type TextType struct {
|
||||
content string
|
||||
}
|
||||
|
||||
func (t TextType) String() string {
|
||||
return t.content
|
||||
}
|
||||
|
||||
func Text(text string) TextType {
|
||||
return TextType{content: text}
|
||||
}
|
||||
|
||||
type HeadlineType struct {
|
||||
level int
|
||||
content string
|
||||
}
|
||||
|
||||
func (h HeadlineType) String() string {
|
||||
return strings.Repeat("#", h.level) + " " + h.content
|
||||
}
|
||||
|
||||
func Headline(level int, content string) HeadlineType {
|
||||
return HeadlineType{
|
||||
level: level,
|
||||
content: content,
|
||||
}
|
||||
}
|
||||
|
||||
type SurroundType struct {
|
||||
body Elem
|
||||
surround string
|
||||
}
|
||||
|
||||
func (s SurroundType) String() string {
|
||||
return s.surround + s.body.String() + s.surround
|
||||
}
|
||||
|
||||
func Bold(e Elem) SurroundType {
|
||||
return SurroundType{body: e, surround: "**"}
|
||||
}
|
||||
|
||||
func Italic(e Elem) SurroundType {
|
||||
return SurroundType{body: e, surround: "*"}
|
||||
}
|
||||
|
||||
func Code(e Elem) SurroundType {
|
||||
return SurroundType{body: e, surround: "`"}
|
||||
}
|
||||
|
||||
type CodeBlockType struct {
|
||||
lang string
|
||||
snippet string
|
||||
}
|
||||
|
||||
func (c CodeBlockType) String() string {
|
||||
return fmt.Sprintf("```%s\n%s\n```", c.lang, c.snippet)
|
||||
}
|
||||
|
||||
func CodeBlock(lang, snippet string) CodeBlockType {
|
||||
return CodeBlockType{lang: lang, snippet: snippet}
|
||||
}
|
||||
|
||||
type ListType struct {
|
||||
elems []Elem
|
||||
}
|
||||
|
||||
func (l ListType) String() string {
|
||||
s := ""
|
||||
for _, e := range l.elems {
|
||||
switch t := e.(type) {
|
||||
case ListType:
|
||||
s += "\n " + strings.Join(strings.Split(t.String(), "\n"), "\n ")
|
||||
default:
|
||||
s += "\n* " + t.String()
|
||||
}
|
||||
}
|
||||
return strings.TrimPrefix(s, "\n")
|
||||
}
|
||||
|
||||
func List(elems ...Elem) ListType {
|
||||
return ListType{elems: elems}
|
||||
}
|
||||
|
||||
type LinkType struct {
|
||||
desc Elem
|
||||
href string
|
||||
}
|
||||
|
||||
func (l LinkType) String() string {
|
||||
return fmt.Sprintf("[%s](%s)", l.desc.String(), l.href)
|
||||
}
|
||||
|
||||
func Link(desc Elem, href string) LinkType {
|
||||
return LinkType{
|
||||
desc: desc,
|
||||
href: href,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue