From 5266a62685cfb9c449bb6543f6da77e61af4bbf5 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 14 Dec 2023 00:04:20 +0800 Subject: [PATCH] chore: implement html renderer --- plugin/gomark/parser/paragraph.go | 9 ++--- plugin/gomark/parser/paragraph_test.go | 26 ++++++++++++++ plugin/gomark/parser/parser_test.go | 25 ++++++++++++-- plugin/gomark/renderer/html/html.go | 43 ++++++++++++++++++++++-- plugin/gomark/renderer/html/html_test.go | 12 +++++-- 5 files changed, 103 insertions(+), 12 deletions(-) diff --git a/plugin/gomark/parser/paragraph.go b/plugin/gomark/parser/paragraph.go index 41c598d3..a40c9962 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.go index 387bc06a..e2f50249 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.go index c5fed67c..22cc771f 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.go index 9c8759c8..ce84207c 100644 --- a/plugin/gomark/renderer/html/html.go +++ b/plugin/gomark/renderer/html/html.go @@ -11,17 +11,17 @@ import ( // nolint type 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("") } + case *ast.BoldItalic: + r.output.WriteString("") + r.output.WriteString(n.Content) + r.output.WriteString("") + case *ast.Bold: + r.output.WriteString("") + r.output.WriteString(n.Content) + r.output.WriteString("") + case *ast.Italic: + r.output.WriteString("") + r.output.WriteString(n.Content) + r.output.WriteString("") + case *ast.Code: + r.output.WriteString("") + r.output.WriteString(n.Content) + r.output.WriteString("") + case *ast.Link: + r.output.WriteString(``) + r.output.WriteString(n.Text) + r.output.WriteString("") + case *ast.Image: + r.output.WriteString(``)
+		r.output.WriteString(n.AltText)
+		r.output.WriteString(``) + case *ast.Tag: + r.output.WriteString(``) + r.output.WriteString(`# `) + r.output.WriteString(n.Content) + r.output.WriteString(``) + case *ast.Strikethrough: + r.output.WriteString(``) + r.output.WriteString(n.Content) + r.output.WriteString(``) 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.go index 83dc5f0d..30b78ee0 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: `
Hello
world!
`, }, + { + text: "*Hello* world!", + expected: `

Hello world!

`, + }, + { + text: "**Hello** world!", + expected: `

Hello world!

`, + }, } 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) } }