Skip to content

Commit 53b7800

Browse files
TheWolfNLjustmiles
authored andcommitted
fix macro's that depend on child element
1 parent 69cb976 commit 53b7800

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

lib/renderer/fencedcode.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,24 @@ import (
1313
// renders FencedCodeBlock nodes.
1414
type ConfluenceFencedCodeBlockHTMLRender struct {
1515
html.Config
16+
MacroContentKeys map[string]struct{}
1617
}
1718

19+
const (
20+
LanguageStringConfluenceMacro string = "CONFLUENCE-MACRO"
21+
22+
MacroContentKeyPlainTextBody string = "plain-text-body"
23+
MacroContentKeyRichTextBody string = "rich-text-body"
24+
)
25+
1826
// NewConfluenceFencedCodeBlockHTMLRender returns a new ConfluenceFencedCodeBlockHTMLRender.
1927
func NewConfluenceFencedCodeBlockHTMLRender(opts ...html.Option) renderer.NodeRenderer {
2028
r := &ConfluenceFencedCodeBlockHTMLRender{
2129
Config: html.NewConfig(),
30+
MacroContentKeys: map[string]struct{}{
31+
MacroContentKeyPlainTextBody: {},
32+
MacroContentKeyRichTextBody: {},
33+
},
2234
}
2335
for _, opt := range opts {
2436
opt.SetHTMLOption(&r.Config)
@@ -40,12 +52,15 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) renderConfluenceFencedCode(w util.
4052
if language != nil {
4153
langString = string(language)
4254
}
43-
if entering {
44-
// If it is a macro create the macro
45-
if langString == "CONFLUENCE-MACRO" {
55+
56+
switch langString {
57+
case LanguageStringConfluenceMacro:
58+
if entering {
4659
r.writeMacro(w, source, n)
47-
} else {
48-
// else insert a code-macro
60+
}
61+
default:
62+
if entering {
63+
// insert a code-macro
4964
s := `<ac:structured-macro ac:name="code" ac:schema-version="1">`
5065
s = s + `<ac:parameter ac:name="theme">Confluence</ac:parameter>`
5166
s = s + `<ac:parameter ac:name="linenumbers">true</ac:parameter>`
@@ -57,11 +72,10 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) renderConfluenceFencedCode(w util.
5772
s = s + `<ac:plain-text-body><![CDATA[ `
5873
_, _ = w.WriteString(s)
5974
r.writeLines(w, source, n)
75+
} else {
76+
s := ` ]]></ac:plain-text-body></ac:structured-macro>`
77+
_, _ = w.WriteString(s)
6078
}
61-
} else if langString != "CONFLUENCE-MACRO" {
62-
// No special handling for the CONFLUENCE-MACRO, just for the code macros
63-
s := ` ]]></ac:plain-text-body></ac:structured-macro>`
64-
_, _ = w.WriteString(s)
6579
}
6680
return ast.WalkContinue, nil
6781
}
@@ -94,8 +108,14 @@ func (r *ConfluenceFencedCodeBlockHTMLRender) writeMacro(w util.BufWriter, sourc
94108
value := strings.TrimSpace(keyValue[1])
95109
// If the key was not indented
96110
if key[0] == keyValue[0][0] {
97-
// we append a new attribute to the macro
98-
macrostart.WriteString(` ac:` + key + `="` + value + `"`)
111+
_, isContentKey := r.MacroContentKeys[key]
112+
if isContentKey {
113+
// we append this as a child element
114+
parameters.WriteString(`<ac:` + key + `>` + value + `</ac:` + key + `>`)
115+
} else {
116+
// we append a new attribute to the macro
117+
macrostart.WriteString(` ac:` + key + `="` + value + `"`)
118+
}
99119
} else {
100120
// It is aparameter to the macro
101121
parameters.WriteString(`<ac:parameter ac:name="` + key + `">` + value + `</ac:parameter>`)

0 commit comments

Comments
 (0)