From 53b78005a877273cbe680e98fa5d269f754dc7b2 Mon Sep 17 00:00:00 2001 From: Tjeerd Ritsma Date: Wed, 14 Dec 2022 14:53:37 +0100 Subject: [PATCH] fix macro's that depend on child element --- lib/renderer/fencedcode.go | 42 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/renderer/fencedcode.go b/lib/renderer/fencedcode.go index 06fe9e0..2746d08 100644 --- a/lib/renderer/fencedcode.go +++ b/lib/renderer/fencedcode.go @@ -13,12 +13,24 @@ import ( // renders FencedCodeBlock nodes. type ConfluenceFencedCodeBlockHTMLRender struct { html.Config + MacroContentKeys map[string]struct{} } +const ( + LanguageStringConfluenceMacro string = "CONFLUENCE-MACRO" + + MacroContentKeyPlainTextBody string = "plain-text-body" + MacroContentKeyRichTextBody string = "rich-text-body" +) + // NewConfluenceFencedCodeBlockHTMLRender returns a new ConfluenceFencedCodeBlockHTMLRender. func NewConfluenceFencedCodeBlockHTMLRender(opts ...html.Option) renderer.NodeRenderer { r := &ConfluenceFencedCodeBlockHTMLRender{ Config: html.NewConfig(), + MacroContentKeys: map[string]struct{}{ + MacroContentKeyPlainTextBody: {}, + MacroContentKeyRichTextBody: {}, + }, } for _, opt := range opts { opt.SetHTMLOption(&r.Config) @@ -40,12 +52,15 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) renderConfluenceFencedCode(w util. if language != nil { langString = string(language) } - if entering { - // If it is a macro create the macro - if langString == "CONFLUENCE-MACRO" { + + switch langString { + case LanguageStringConfluenceMacro: + if entering { r.writeMacro(w, source, n) - } else { - // else insert a code-macro + } + default: + if entering { + // insert a code-macro s := `` s = s + `Confluence` s = s + `true` @@ -57,11 +72,10 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) renderConfluenceFencedCode(w util. s = s + `` + _, _ = w.WriteString(s) } - } else if langString != "CONFLUENCE-MACRO" { - // No special handling for the CONFLUENCE-MACRO, just for the code macros - s := ` ]]>` - _, _ = w.WriteString(s) } return ast.WalkContinue, nil } @@ -94,8 +108,14 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) writeMacro(w util.BufWriter, sourc value := strings.TrimSpace(keyValue[1]) // If the key was not indented if key[0] == keyValue[0][0] { - // we append a new attribute to the macro - macrostart.WriteString(` ac:` + key + `="` + value + `"`) + _, isContentKey := r.MacroContentKeys[key] + if isContentKey { + // we append this as a child element + parameters.WriteString(`` + value + ``) + } else { + // we append a new attribute to the macro + macrostart.WriteString(` ac:` + key + `="` + value + `"`) + } } else { // It is aparameter to the macro parameters.WriteString(`` + value + ``)