Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
chore: implement html renderer
Steven5266a62685cf
diff --git a/plugin/gomark/parser/paragraph.go b/plugin/gomark/parser/paragraph.goindex 41c598d3f..a40c99621 100644--- a/plugin/gomark/parser/paragraph.go+++ b/plugin/gomark/parser/paragraph.go@@ -17,17 +17,18 @@ func NewParagraphParser() *ParagraphParser {func (*ParagraphParser) Match(tokens []*tokenizer.Token) (int, bool) {contentTokens := []*tokenizer.Token{}- cursor := 0- for ; cursor < len(tokens); cursor++ {- token := tokens[cursor]+ for _, token := range tokens {+ contentTokens = append(contentTokens, token)if token.Type == tokenizer.Newline {break}- contentTokens = append(contentTokens, token)}if len(contentTokens) == 0 {return 0, false}+ if len(contentTokens) == 1 && contentTokens[0].Type == tokenizer.Newline {+ return 0, false+ }return len(contentTokens), true}diff --git a/plugin/gomark/parser/paragraph_test.go b/plugin/gomark/parser/paragraph_test.goindex 387bc06ab..e2f502493 100644--- a/plugin/gomark/parser/paragraph_test.go+++ b/plugin/gomark/parser/paragraph_test.go@@ -18,6 +18,10 @@ func TestParagraphParser(t *testing.T) {text: "",paragraph: nil,},+ {+ text: "\n",+ paragraph: nil,+ },{text: "Hello world!",paragraph: &ast.Paragraph{@@ -28,6 +32,28 @@ func TestParagraphParser(t *testing.T) {},},},+ {+ text: "Hello world!\n",+ paragraph: &ast.Paragraph{+ Children: []ast.Node{+ &ast.Text{+ Content: "Hello world!",+ },+ &ast.LineBreak{},+ },+ },+ },+ {+ text: "Hello world!\n\nNew paragraph.",+ paragraph: &ast.Paragraph{+ Children: []ast.Node{+ &ast.Text{+ Content: "Hello world!",+ },+ &ast.LineBreak{},+ },+ },+ },}for _, test := range tests {diff --git a/plugin/gomark/parser/parser_test.go b/plugin/gomark/parser/parser_test.goindex c5fed67c1..22cc771f8 100644--- a/plugin/gomark/parser/parser_test.go+++ b/plugin/gomark/parser/parser_test.go@@ -57,9 +57,9 @@ func TestParser(t *testing.T) {&ast.Text{Content: "!",},+ &ast.LineBreak{},},},- &ast.LineBreak{},&ast.Paragraph{Children: []ast.Node{&ast.Text{@@ -84,15 +84,36 @@ func TestParser(t *testing.T) {&ast.Text{Content: "!",},+ &ast.LineBreak{},},},- &ast.LineBreak{},&ast.CodeBlock{Language: "javascript",Content: "console.log(\"Hello world!\");",},},},+ {+ text: "Hello world!\n\nNew paragraph.",+ nodes: []ast.Node{+ &ast.Paragraph{+ Children: []ast.Node{+ &ast.Text{+ Content: "Hello world!",+ },+ &ast.LineBreak{},+ },+ },+ &ast.LineBreak{},+ &ast.Paragraph{+ Children: []ast.Node{+ &ast.Text{+ Content: "New paragraph.",+ },+ },+ },+ },+ },}for _, test := range tests {diff --git a/plugin/gomark/renderer/html/html.go b/plugin/gomark/renderer/html/html.goindex 9c8759c81..ce84207cd 100644--- a/plugin/gomark/renderer/html/html.go+++ b/plugin/gomark/renderer/html/html.go@@ -11,17 +11,17 @@ import (// nolinttype HTMLRenderer struct {output *bytes.Buffer- context *renderContext+ context *RendererContext}-type renderContext struct {+type RendererContext struct {}// NewHTMLRenderer creates a new HTMLRenderer.func NewHTMLRenderer() *HTMLRenderer {return &HTMLRenderer{output: new(bytes.Buffer),- context: &renderContext{},+ context: &RendererContext{},}}@@ -57,6 +57,43 @@ func (r *HTMLRenderer) RenderNode(node ast.Node) {if prevSibling == nil || prevSibling.Type() != ast.NodeTypeBlockquote {r.output.WriteString("</blockquote>")}+ case *ast.BoldItalic:+ r.output.WriteString("<strong><em>")+ r.output.WriteString(n.Content)+ r.output.WriteString("</em></strong>")+ case *ast.Bold:+ r.output.WriteString("<strong>")+ r.output.WriteString(n.Content)+ r.output.WriteString("</strong>")+ case *ast.Italic:+ r.output.WriteString("<em>")+ r.output.WriteString(n.Content)+ r.output.WriteString("</em>")+ case *ast.Code:+ r.output.WriteString("<code>")+ r.output.WriteString(n.Content)+ r.output.WriteString("</code>")+ case *ast.Link:+ r.output.WriteString(`<a href="`)+ r.output.WriteString(n.URL)+ r.output.WriteString(`">`)+ r.output.WriteString(n.Text)+ r.output.WriteString("</a>")+ case *ast.Image:+ r.output.WriteString(`<img src="`)+ r.output.WriteString(n.URL)+ r.output.WriteString(`" alt="`)+ r.output.WriteString(n.AltText)+ r.output.WriteString(`" />`)+ case *ast.Tag:+ r.output.WriteString(`<span>`)+ r.output.WriteString(`# `)+ r.output.WriteString(n.Content)+ r.output.WriteString(`</span>`)+ case *ast.Strikethrough:+ r.output.WriteString(`<del>`)+ r.output.WriteString(n.Content)+ r.output.WriteString(`</del>`)case *ast.Text:r.output.WriteString(n.Content)default:diff --git a/plugin/gomark/renderer/html/html_test.go b/plugin/gomark/renderer/html/html_test.goindex 83dc5f0dc..30b78ee04 100644--- a/plugin/gomark/renderer/html/html_test.go+++ b/plugin/gomark/renderer/html/html_test.go@@ -22,6 +22,14 @@ func TestHTMLRenderer(t *testing.T) {text: "> Hello\n> world!",expected: `<blockquote>Hello<br>world!</blockquote>`,},+ {+ text: "*Hello* world!",+ expected: `<p><em>Hello</em> world!</p>`,+ },+ {+ text: "**Hello** world!",+ expected: `<p><strong>Hello</strong> world!</p>`,+ },}for _, test := range tests {@@ -29,8 +37,6 @@ func TestHTMLRenderer(t *testing.T) {nodes, err := parser.Parse(tokens)require.NoError(t, err)actual := NewHTMLRenderer().Render(nodes)- if actual != test.expected {- t.Errorf("expected: %s, actual: %s", test.expected, actual)- }+ require.Equal(t, test.expected, actual)}}