Render CodeBlocks properly

This commit is contained in:
Manuel Rüger 2023-03-29 15:31:13 +02:00
parent fef66925d1
commit 6265c7ff81
3 changed files with 72 additions and 4 deletions

View File

@ -68,7 +68,7 @@ func (r *ConfluenceRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegister
// reg.Register(ast.KindHeading, r.renderNode) // reg.Register(ast.KindHeading, r.renderNode)
reg.Register(ast.KindBlockquote, r.renderBlockQuote) reg.Register(ast.KindBlockquote, r.renderBlockQuote)
reg.Register(ast.KindCodeBlock, r.renderCodeBlock) reg.Register(ast.KindCodeBlock, r.renderCodeBlock)
reg.Register(ast.KindFencedCodeBlock, r.renderCodeBlock) reg.Register(ast.KindFencedCodeBlock, r.renderFencedCodeBlock)
// reg.Register(ast.KindHTMLBlock, r.renderNode) // reg.Register(ast.KindHTMLBlock, r.renderNode)
// reg.Register(ast.KindList, r.renderNode) // reg.Register(ast.KindList, r.renderNode)
// reg.Register(ast.KindListItem, r.renderNode) // reg.Register(ast.KindListItem, r.renderNode)
@ -307,9 +307,8 @@ func (r *ConfluenceRenderer) goldmarkRenderLink(w util.BufWriter, source []byte,
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
// renderCodeBlock renders a (Fenced)CodeBlock // renderFencedCodeBlock renders a FencedCodeBlock
func (r *ConfluenceRenderer) renderCodeBlock(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { func (r *ConfluenceRenderer) renderFencedCodeBlock(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering { if !entering {
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
@ -384,6 +383,53 @@ func (r *ConfluenceRenderer) renderCodeBlock(writer util.BufWriter, source []byt
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
// renderCodeBlock renders a CodeBlock
func (r *ConfluenceRenderer) renderCodeBlock(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
linenumbers := false
firstline := 0
theme := ""
collapse := false
lang := ""
title := ""
var lval []byte
lines := node.Lines().Len()
for i := 0; i < lines; i++ {
line := node.Lines().At(i)
lval = append(lval, line.Value(source)...)
}
err := r.Stdlib.Templates.ExecuteTemplate(
writer,
"ac:code",
struct {
Language string
Collapse bool
Title string
Theme string
Linenumbers bool
Firstline int
Text string
}{
lang,
collapse,
title,
theme,
linenumbers,
firstline,
strings.TrimSuffix(string(lval), "\n"),
},
)
if err != nil {
return ast.WalkStop, err
}
return ast.WalkContinue, nil
}
// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped // compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
// equivalent, because goldmark markdown parser replaces that tags with // equivalent, because goldmark markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> because of the autolink // <a href="ac:rich-text-body">ac:rich-text-body</a> because of the autolink

View File

@ -44,6 +44,19 @@ text 2</p>
<ac:parameter ac:name="collapse">true</ac:parameter> <ac:parameter ac:name="collapse">true</ac:parameter>
<ac:plain-text-body><![CDATA[collapse-no-title]]></ac:plain-text-body> <ac:plain-text-body><![CDATA[collapse-no-title]]></ac:plain-text-body>
</ac:structured-macro> </ac:structured-macro>
<ac:structured-macro ac:name="code">
<ac:parameter ac:name="language">nested</ac:parameter>
<ac:parameter ac:name="collapse">false</ac:parameter>
<ac:plain-text-body><![CDATA[code
``` more code ```
even more code]]></ac:plain-text-body>
</ac:structured-macro>
<ac:structured-macro ac:name="code">
<ac:parameter ac:name="language"></ac:parameter>
<ac:parameter ac:name="collapse">false</ac:parameter>
<ac:plain-text-body><![CDATA[indented code block
with multiple lines]]></ac:plain-text-body>
</ac:structured-macro>
<ac:structured-macro ac:name="cloudscript-confluence-mermaid"> <ac:structured-macro ac:name="cloudscript-confluence-mermaid">
<ac:parameter ac:name="showSource">true</ac:parameter> <ac:parameter ac:name="showSource">true</ac:parameter>
<ac:parameter ac:name="collapse">false</ac:parameter> <ac:parameter ac:name="collapse">false</ac:parameter>

View File

@ -33,6 +33,15 @@ collapse-and-title
collapse-no-title collapse-no-title
``` ```
```nested
code
``` more code ```
even more code
```
indented code block
with multiple lines
```mermaid ```mermaid
graph TD; graph TD;
A-->B; A-->B;