mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
Incorporate the collapsible code block (#39)
* Incorporate the collapsible code block * Press tilda 3 times
This commit is contained in:
parent
27e7af5b85
commit
bcf2acb39f
27
README.md
27
README.md
@ -92,6 +92,33 @@ for example:
|
|||||||
Ticket: ${0} -->
|
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:
|
By default, mark provides several built-in templates and macros:
|
||||||
|
|
||||||
* template `ac:status` to include badge-like text, which accepts following
|
* template `ac:status` to include badge-like text, which accepts following
|
||||||
|
@ -3,6 +3,8 @@ package mark
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||||
"github.com/reconquest/pkg/log"
|
"github.com/reconquest/pkg/log"
|
||||||
@ -15,6 +17,37 @@ type ConfluenceRenderer struct {
|
|||||||
Stdlib *stdlib.Lib
|
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(
|
func (renderer ConfluenceRenderer) BlockCode(
|
||||||
out *bytes.Buffer,
|
out *bytes.Buffer,
|
||||||
text []byte,
|
text []byte,
|
||||||
@ -25,9 +58,13 @@ func (renderer ConfluenceRenderer) BlockCode(
|
|||||||
"ac:code",
|
"ac:code",
|
||||||
struct {
|
struct {
|
||||||
Language string
|
Language string
|
||||||
|
Collapse string
|
||||||
|
Title string
|
||||||
Text string
|
Text string
|
||||||
}{
|
}{
|
||||||
lang,
|
ParseLanguage(lang),
|
||||||
|
strconv.FormatBool(strings.Contains(lang, "collapse")),
|
||||||
|
ParseTitle(lang),
|
||||||
string(text),
|
string(text),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -105,11 +105,12 @@ func templates(api *confluence.API) (*template.Template, error) {
|
|||||||
|
|
||||||
// This template is used for rendering code in ```
|
// This template is used for rendering code in ```
|
||||||
`ac:code`: text(
|
`ac:code`: text(
|
||||||
`<ac:structured-macro ac:name="code">`,
|
`<ac:structured-macro ac:name="code">{{printf "\n"}}`,
|
||||||
`<ac:parameter ac:name="language">{{ .Language }}</ac:parameter>`,
|
`<ac:parameter ac:name="language">{{ .Language }}</ac:parameter>{{printf "\n"}}`,
|
||||||
`<ac:parameter ac:name="collapse">false</ac:parameter>`,
|
`<ac:parameter ac:name="collapse">{{ .Collapse }}</ac:parameter>{{printf "\n"}}`,
|
||||||
`<ac:plain-text-body><![CDATA[{{ .Text | cdata }}]]></ac:plain-text-body>`,
|
`<ac:parameter ac:name="title">{{ .Title }}</ac:parameter>{{printf "\n"}}`,
|
||||||
`</ac:structured-macro>`,
|
`<ac:plain-text-body><![CDATA[{{ .Text | cdata }}]]></ac:plain-text-body>{{printf "\n"}}`,
|
||||||
|
`</ac:structured-macro>{{printf "\n"}}`,
|
||||||
),
|
),
|
||||||
|
|
||||||
`ac:status`: text(
|
`ac:status`: text(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user