Skip to content

Commit

Permalink
Merge pull request kovetskiy#268 from mrueg/fix-codeblock
Browse files Browse the repository at this point in the history
Render codeblocks properly
  • Loading branch information
mrueg authored Mar 29, 2023
2 parents fef6692 + 6265c7f commit fca934f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
54 changes: 50 additions & 4 deletions pkg/mark/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *ConfluenceRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegister
// reg.Register(ast.KindHeading, r.renderNode)
reg.Register(ast.KindBlockquote, r.renderBlockQuote)
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.KindList, r.renderNode)
// reg.Register(ast.KindListItem, r.renderNode)
Expand Down Expand Up @@ -307,9 +307,8 @@ func (r *ConfluenceRenderer) goldmarkRenderLink(w util.BufWriter, source []byte,
return ast.WalkContinue, nil
}

// renderCodeBlock renders a (Fenced)CodeBlock
func (r *ConfluenceRenderer) renderCodeBlock(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {

// renderFencedCodeBlock renders a FencedCodeBlock
func (r *ConfluenceRenderer) renderFencedCodeBlock(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
Expand Down Expand Up @@ -384,6 +383,53 @@ func (r *ConfluenceRenderer) renderCodeBlock(writer util.BufWriter, source []byt
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
// equivalent, because goldmark markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> because of the autolink
Expand Down
13 changes: 13 additions & 0 deletions pkg/mark/testdata/codes.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@
<ac:parameter ac:name="collapse">true</ac:parameter>
<ac:plain-text-body><![CDATA[collapse-no-title]]></ac:plain-text-body>
</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:parameter ac:name="showSource">true</ac:parameter>
<ac:parameter ac:name="collapse">false</ac:parameter>
Expand Down
9 changes: 9 additions & 0 deletions pkg/mark/testdata/codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ collapse-and-title
collapse-no-title
```

```nested
code
``` more code ```
even more code
```

indented code block
with multiple lines

```mermaid
graph TD;
A-->B;
Expand Down

0 comments on commit fca934f

Please sign in to comment.