Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate the collapsible code block #39

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion pkg/mark/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package mark
import (
"bytes"
"regexp"
"strconv"
"strings"

"github.com/kovetskiy/mark/pkg/mark/stdlib"
"github.com/reconquest/pkg/log"
Expand All @@ -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,
Expand All @@ -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),
},
)
Expand Down
11 changes: 6 additions & 5 deletions pkg/mark/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down