From 3065e2e65ccfdf9fa12413bbce8a2119a93e81af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Sun, 29 Nov 2020 11:50:25 +0000 Subject: [PATCH 1/2] Incorporate the collapsible code block --- README.md | 27 +++++++++++++++++++++++++++ pkg/mark/markdown.go | 39 ++++++++++++++++++++++++++++++++++++++- pkg/mark/stdlib/stdlib.go | 11 ++++++----- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ecefa6f8..148169e3 100644 --- a/README.md +++ b/README.md @@ -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_: + + [] ["collapse"] ["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 diff --git a/pkg/mark/markdown.go b/pkg/mark/markdown.go index d79b5867..30055959 100644 --- a/pkg/mark/markdown.go +++ b/pkg/mark/markdown.go @@ -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"? *)? + // 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), }, ) diff --git a/pkg/mark/stdlib/stdlib.go b/pkg/mark/stdlib/stdlib.go index 1644ce0e..ec6daea5 100644 --- a/pkg/mark/stdlib/stdlib.go +++ b/pkg/mark/stdlib/stdlib.go @@ -105,11 +105,12 @@ func templates(api *confluence.API) (*template.Template, error) { // This template is used for rendering code in ``` `ac:code`: text( - ``, - `{{ .Language }}`, - `false`, - ``, - ``, + `{{printf "\n"}}`, + `{{ .Language }}{{printf "\n"}}`, + `{{ .Collapse }}{{printf "\n"}}`, + `{{ .Title }}{{printf "\n"}}`, + `{{printf "\n"}}`, + `{{printf "\n"}}`, ), `ac:status`: text( From 06c3ed542ea6cfef427b9570431da77bf8cd6be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Sun, 29 Nov 2020 11:57:49 +0000 Subject: [PATCH 2/2] Press tilda 3 times --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 148169e3..f223ba4c 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ must stay in the front _if it is given_: [] ["collapse"] ["title" ] -[code block macro]: https://confluence.atlassian.com/doc/code-block-macro-139390.html +[Code Block Macro]: https://confluence.atlassian.com/doc/code-block-macro-139390.html ## Template & Macros