From 622be6e25f454a5bb1fd6428769ad8a13c1f838d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Thu, 4 May 2023 14:53:33 +0200 Subject: [PATCH] Support customizable layouts --- README.md | 30 ++++++++++++ pkg/mark/markdown.go | 78 ++++++++++++++++++++++++++++++- pkg/mark/testdata/pagelayout.html | 18 +++++++ pkg/mark/testdata/pagelayout.md | 21 +++++++++ 4 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 pkg/mark/testdata/pagelayout.html create mode 100644 pkg/mark/testdata/pagelayout.md diff --git a/README.md b/README.md index 531a0ab..30b1c12 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,36 @@ The key's value must be a string which defines the template's content. ``` +### Customizing the page layout + +If you set the Layout to plain, the page layout can be customized using HTML comments inside the markdown: + +``` + + + + + +More Content + + +More Content + + +Even More Content + + + + + +Still More Content + + + + +``` + +Please be aware that mark does not validate the layout, so it's your responsibility to create a valid layout. ### Code Blocks diff --git a/pkg/mark/markdown.go b/pkg/mark/markdown.go index 133581a..c0da166 100644 --- a/pkg/mark/markdown.go +++ b/pkg/mark/markdown.go @@ -77,7 +77,7 @@ func (r *ConfluenceRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegister reg.Register(ast.KindBlockquote, r.renderBlockQuote) reg.Register(ast.KindCodeBlock, r.renderCodeBlock) reg.Register(ast.KindFencedCodeBlock, r.renderFencedCodeBlock) - // reg.Register(ast.KindHTMLBlock, r.renderNode) + reg.Register(ast.KindHTMLBlock, r.renderHTMLBlock) // reg.Register(ast.KindList, r.renderNode) // reg.Register(ast.KindListItem, r.renderNode) // reg.Register(ast.KindParagraph, r.renderNode) @@ -533,6 +533,82 @@ func (r *ConfluenceRenderer) renderImage(writer util.BufWriter, source []byte, n return ast.WalkSkipChildren, nil } +func (r *ConfluenceRenderer) renderHTMLBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + if !entering { + return r.goldmarkRenderHTMLBlock(w, source, node, entering) + } + + n := node.(*ast.HTMLBlock) + l := n.Lines().Len() + for i := 0; i < l; i++ { + line := n.Lines().At(i) + + switch strings.Trim(string(line.Value(source)), "\n") { + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + case "": + _, _ = w.WriteString("\n") + return ast.WalkContinue, nil + + } + } + return r.goldmarkRenderHTMLBlock(w, source, node, entering) + +} + +func (r *ConfluenceRenderer) goldmarkRenderHTMLBlock(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + n := node.(*ast.HTMLBlock) + if entering { + if r.Unsafe { + l := n.Lines().Len() + for i := 0; i < l; i++ { + line := n.Lines().At(i) + r.Writer.SecureWrite(w, line.Value(source)) + } + } else { + _, _ = w.WriteString("\n") + } + } else { + if n.HasClosure() { + if r.Unsafe { + closure := n.ClosureLine + r.Writer.SecureWrite(w, closure.Value(source)) + } else { + _, _ = w.WriteString("\n") + } + } + } + return ast.WalkContinue, nil +} + func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, mermaidProvider string) (string, []Attachment) { log.Tracef(nil, "rendering markdown:\n%s", string(markdown)) diff --git a/pkg/mark/testdata/pagelayout.html b/pkg/mark/testdata/pagelayout.html new file mode 100644 index 0000000..66f5783 --- /dev/null +++ b/pkg/mark/testdata/pagelayout.html @@ -0,0 +1,18 @@ + + + +

More Content

+
+ +

More Content

+
+ +

Even More Content

+
+
+ + +

Still More Content

+
+
+
diff --git a/pkg/mark/testdata/pagelayout.md b/pkg/mark/testdata/pagelayout.md new file mode 100644 index 0000000..a4e6f08 --- /dev/null +++ b/pkg/mark/testdata/pagelayout.md @@ -0,0 +1,21 @@ + + + + +More Content + + +More Content + + +Even More Content + + + + + +Still More Content + + + +