Incorporate the collapsible code block (#39)

* Incorporate the collapsible code block

* Press tilda 3 times
This commit is contained in:
Šarūnas Nejus 2020-11-30 07:46:29 +00:00 committed by GitHub
parent 27e7af5b85
commit bcf2acb39f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 6 deletions

View File

@ -92,6 +92,33 @@ for example:
Ticket: ${0} -->
```
### Code Blocks
If you have long code blocks, you can make them collapsible with the [Code Block Macro]:
```bash collapse
...
some long bash code block
...
```
And you can also add a title:
```bash collapse title Some long long bash function
...
some long bash code block
...
```
You can collapse or have a title without language or any mix, but the language
must stay in the front _if it is given_:
[<language>] ["collapse"] ["title" <your title>]
[Code Block Macro]: https://confluence.atlassian.com/doc/code-block-macro-139390.html
## Template & Macros
By default, mark provides several built-in templates and macros:
* template `ac:status` to include badge-like text, which accepts following

View File

@ -3,6 +3,8 @@ package mark
import (
"bytes"
"regexp"
"strconv"
"strings"
"github.com/kovetskiy/mark/pkg/mark/stdlib"
"github.com/reconquest/pkg/log"
@ -15,6 +17,37 @@ type ConfluenceRenderer struct {
Stdlib *stdlib.Lib
}
func ParseLanguage(lang string) string {
// lang takes the following form: language? "collapse"? ("title"? <any string>*)?
// let's split it by spaces
paramlist := strings.Fields(lang)
// get the word in question, aka the first one
first := lang
if len(paramlist) > 0 {
first = paramlist[0]
}
if first == "collapse" || first == "title" {
// collapsing or including a title without a language
return ""
}
// the default case with language being the first one
return first
}
func ParseTitle(lang string) string {
index := strings.Index(lang, "title")
if index >= 0 {
// it's found, check if title is given and return it
start := index + 6
if len(lang) > start {
return lang[start:]
}
}
return ""
}
func (renderer ConfluenceRenderer) BlockCode(
out *bytes.Buffer,
text []byte,
@ -25,9 +58,13 @@ func (renderer ConfluenceRenderer) BlockCode(
"ac:code",
struct {
Language string
Collapse string
Title string
Text string
}{
lang,
ParseLanguage(lang),
strconv.FormatBool(strings.Contains(lang, "collapse")),
ParseTitle(lang),
string(text),
},
)

View File

@ -105,11 +105,12 @@ func templates(api *confluence.API) (*template.Template, error) {
// This template is used for rendering code in ```
`ac:code`: text(
`<ac:structured-macro ac:name="code">`,
`<ac:parameter ac:name="language">{{ .Language }}</ac:parameter>`,
`<ac:parameter ac:name="collapse">false</ac:parameter>`,
`<ac:plain-text-body><![CDATA[{{ .Text | cdata }}]]></ac:plain-text-body>`,
`</ac:structured-macro>`,
`<ac:structured-macro ac:name="code">{{printf "\n"}}`,
`<ac:parameter ac:name="language">{{ .Language }}</ac:parameter>{{printf "\n"}}`,
`<ac:parameter ac:name="collapse">{{ .Collapse }}</ac:parameter>{{printf "\n"}}`,
`<ac:parameter ac:name="title">{{ .Title }}</ac:parameter>{{printf "\n"}}`,
`<ac:plain-text-body><![CDATA[{{ .Text | cdata }}]]></ac:plain-text-body>{{printf "\n"}}`,
`</ac:structured-macro>{{printf "\n"}}`,
),
`ac:status`: text(