mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-23 13:22:40 +08:00
parent
e190cdfb34
commit
7f6466a882
2
go.mod
2
go.mod
@ -7,6 +7,7 @@ require (
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
|
||||
github.com/alecthomas/colour v0.1.0 // indirect
|
||||
github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
|
||||
github.com/go-yaml/yaml v2.1.0+incompatible // indirect
|
||||
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 // indirect
|
||||
@ -19,6 +20,7 @@ require (
|
||||
github.com/reconquest/pkg v0.0.0-20201028091908-8e9a5e0226ef
|
||||
github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4
|
||||
github.com/russross/blackfriday v1.5.2
|
||||
github.com/russross/blackfriday/v2 v2.1.0
|
||||
github.com/sergi/go-diff v1.1.0 // indirect
|
||||
github.com/stretchr/testify v1.5.1
|
||||
gopkg.in/yaml.v2 v2.2.8
|
||||
|
3
go.sum
3
go.sum
@ -48,6 +48,9 @@ github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4 h1:bcDXaT
|
||||
github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4/go.mod h1:OI1di2iiFSwX3D70iZjzdmCPPfssjOl+HX40tI3VaXA=
|
||||
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
|
||||
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
|
||||
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
|
@ -1,18 +1,18 @@
|
||||
package mark
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/reconquest/pkg/log"
|
||||
"github.com/russross/blackfriday"
|
||||
bf "github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
type ConfluenceRenderer struct {
|
||||
blackfriday.Renderer
|
||||
bf.Renderer
|
||||
|
||||
Stdlib *stdlib.Lib
|
||||
}
|
||||
@ -48,30 +48,36 @@ func ParseTitle(lang string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (renderer ConfluenceRenderer) BlockCode(
|
||||
out *bytes.Buffer,
|
||||
text []byte,
|
||||
lang string,
|
||||
) {
|
||||
renderer.Stdlib.Templates.ExecuteTemplate(
|
||||
out,
|
||||
"ac:code",
|
||||
struct {
|
||||
Language string
|
||||
Collapse string
|
||||
Title string
|
||||
Text string
|
||||
}{
|
||||
ParseLanguage(lang),
|
||||
strconv.FormatBool(strings.Contains(lang, "collapse")),
|
||||
ParseTitle(lang),
|
||||
string(text),
|
||||
},
|
||||
)
|
||||
func (renderer ConfluenceRenderer) RenderNode(
|
||||
writer io.Writer,
|
||||
node *bf.Node,
|
||||
entering bool,
|
||||
) bf.WalkStatus {
|
||||
if node.Type == bf.CodeBlock {
|
||||
lang := string(node.Info)
|
||||
renderer.Stdlib.Templates.ExecuteTemplate(
|
||||
writer,
|
||||
"ac:code",
|
||||
struct {
|
||||
Language string
|
||||
Collapse string
|
||||
Title string
|
||||
Text string
|
||||
}{
|
||||
ParseLanguage(lang),
|
||||
strconv.FormatBool(strings.Contains(lang, "collapse")),
|
||||
ParseTitle(lang),
|
||||
string(node.Literal),
|
||||
},
|
||||
)
|
||||
|
||||
return bf.GoToNext
|
||||
}
|
||||
return renderer.Renderer.RenderNode(writer, node, entering)
|
||||
}
|
||||
|
||||
// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
|
||||
// equivalent, because blackfriday markdown parser replaces that tags with
|
||||
// equivalent, because bf markdown parser replaces that tags with
|
||||
// <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
|
||||
func CompileMarkdown(
|
||||
markdown []byte,
|
||||
@ -79,7 +85,7 @@ func CompileMarkdown(
|
||||
) string {
|
||||
log.Tracef(nil, "rendering markdown:\n%s", string(markdown))
|
||||
|
||||
colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`)
|
||||
colon := regexp.MustCompile(`---bf-COLON---`)
|
||||
|
||||
tags := regexp.MustCompile(`<(/?\S+?):(\S+?)>`)
|
||||
|
||||
@ -89,37 +95,37 @@ func CompileMarkdown(
|
||||
)
|
||||
|
||||
renderer := ConfluenceRenderer{
|
||||
Renderer: blackfriday.HtmlRenderer(
|
||||
blackfriday.HTML_USE_XHTML|
|
||||
blackfriday.HTML_USE_SMARTYPANTS|
|
||||
blackfriday.HTML_SMARTYPANTS_FRACTIONS|
|
||||
blackfriday.HTML_SMARTYPANTS_DASHES|
|
||||
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
|
||||
"", "",
|
||||
Renderer: bf.NewHTMLRenderer(
|
||||
bf.HTMLRendererParameters{
|
||||
Flags: bf.UseXHTML |
|
||||
bf.Smartypants |
|
||||
bf.SmartypantsFractions |
|
||||
bf.SmartypantsDashes |
|
||||
bf.SmartypantsLatexDashes,
|
||||
},
|
||||
),
|
||||
|
||||
Stdlib: stdlib,
|
||||
}
|
||||
|
||||
html := blackfriday.MarkdownOptions(
|
||||
html := bf.Run(
|
||||
markdown,
|
||||
renderer,
|
||||
blackfriday.Options{
|
||||
Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
|
||||
blackfriday.EXTENSION_TABLES |
|
||||
blackfriday.EXTENSION_FENCED_CODE |
|
||||
blackfriday.EXTENSION_AUTOLINK |
|
||||
blackfriday.EXTENSION_LAX_HTML_BLOCKS |
|
||||
blackfriday.EXTENSION_STRIKETHROUGH |
|
||||
blackfriday.EXTENSION_SPACE_HEADERS |
|
||||
blackfriday.EXTENSION_HEADER_IDS |
|
||||
blackfriday.EXTENSION_AUTO_HEADER_IDS |
|
||||
blackfriday.EXTENSION_TITLEBLOCK |
|
||||
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
|
||||
blackfriday.EXTENSION_DEFINITION_LISTS |
|
||||
blackfriday.EXTENSION_HARD_LINE_BREAK |
|
||||
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK,
|
||||
},
|
||||
bf.WithRenderer(renderer),
|
||||
bf.WithExtensions(
|
||||
bf.NoIntraEmphasis|
|
||||
bf.Tables|
|
||||
bf.FencedCode|
|
||||
bf.Autolink|
|
||||
bf.LaxHTMLBlocks|
|
||||
bf.Strikethrough|
|
||||
bf.SpaceHeadings|
|
||||
bf.HeadingIDs|
|
||||
bf.AutoHeadingIDs|
|
||||
bf.Titleblock|
|
||||
bf.BackslashLineBreak|
|
||||
bf.DefinitionLists|
|
||||
bf.NoEmptyLineBeforeBlock,
|
||||
),
|
||||
)
|
||||
|
||||
html = colon.ReplaceAll(html, []byte(`:`))
|
||||
|
38
pkg/mark/testdata/codes.html
vendored
38
pkg/mark/testdata/codes.html
vendored
@ -36,7 +36,7 @@
|
||||
</ac:rich-text-body>
|
||||
</ac:structured-macro>
|
||||
|
||||
<p>text<br />
|
||||
<p>text
|
||||
text 2</p>
|
||||
<ac:structured-macro ac:name="expand">
|
||||
<ac:parameter ac:name="title"></ac:parameter>
|
||||
@ -50,3 +50,39 @@ text 2</p>
|
||||
</ac:structured-macro>
|
||||
</ac:rich-text-body>
|
||||
</ac:structured-macro>
|
||||
<ac:structured-macro ac:name="expand">
|
||||
<ac:parameter ac:name="title">A b c</ac:parameter>
|
||||
<ac:rich-text-body>
|
||||
<ac:structured-macro ac:name="code">
|
||||
<ac:parameter ac:name="language">sh</ac:parameter>
|
||||
<ac:parameter ac:name="collapse">false</ac:parameter>
|
||||
<ac:parameter ac:name="title">A b c</ac:parameter>
|
||||
<ac:plain-text-body><![CDATA[no-collapse-title
|
||||
]]></ac:plain-text-body>
|
||||
</ac:structured-macro>
|
||||
</ac:rich-text-body>
|
||||
</ac:structured-macro>
|
||||
<ac:structured-macro ac:name="expand">
|
||||
<ac:parameter ac:name="title">A b c</ac:parameter>
|
||||
<ac:rich-text-body>
|
||||
<ac:structured-macro ac:name="code">
|
||||
<ac:parameter ac:name="language">bash</ac:parameter>
|
||||
<ac:parameter ac:name="collapse">true</ac:parameter>
|
||||
<ac:parameter ac:name="title">A b c</ac:parameter>
|
||||
<ac:plain-text-body><![CDATA[collapse-and-title
|
||||
]]></ac:plain-text-body>
|
||||
</ac:structured-macro>
|
||||
</ac:rich-text-body>
|
||||
</ac:structured-macro>
|
||||
<ac:structured-macro ac:name="expand">
|
||||
<ac:parameter ac:name="title"></ac:parameter>
|
||||
<ac:rich-text-body>
|
||||
<ac:structured-macro ac:name="code">
|
||||
<ac:parameter ac:name="language">c</ac:parameter>
|
||||
<ac:parameter ac:name="collapse">true</ac:parameter>
|
||||
<ac:parameter ac:name="title"></ac:parameter>
|
||||
<ac:plain-text-body><![CDATA[collapse-no-title
|
||||
]]></ac:plain-text-body>
|
||||
</ac:structured-macro>
|
||||
</ac:rich-text-body>
|
||||
</ac:structured-macro>
|
||||
|
12
pkg/mark/testdata/codes.md
vendored
12
pkg/mark/testdata/codes.md
vendored
@ -15,3 +15,15 @@ text 2
|
||||
```unknown
|
||||
unknown code 2
|
||||
```
|
||||
|
||||
```sh title A b c
|
||||
no-collapse-title
|
||||
```
|
||||
|
||||
```bash collapse title A b c
|
||||
collapse-and-title
|
||||
```
|
||||
|
||||
```c collapse
|
||||
collapse-no-title
|
||||
```
|
||||
|
21
pkg/mark/testdata/lists.html
vendored
21
pkg/mark/testdata/lists.html
vendored
@ -1,16 +1,15 @@
|
||||
<ul>
|
||||
<li>dash 1-1<br /></li>
|
||||
<li>dash 1-2<br /></li>
|
||||
<li>dash 1-3<br />
|
||||
<li>dash 1-1</li>
|
||||
<li>dash 1-2</li>
|
||||
<li>dash 1-3
|
||||
|
||||
<ul>
|
||||
<li>dash 1-3-1<br /></li>
|
||||
<li>dash 1-3-2<br /></li>
|
||||
<li>dash 1-3-3<br />
|
||||
<li>dash 1-3-1</li>
|
||||
<li>dash 1-3-2</li>
|
||||
<li>dash 1-3-3
|
||||
|
||||
<ul>
|
||||
<li>dash 1-3-3-1<br />
|
||||
<br /></li>
|
||||
<li>dash 1-3-3-1</li>
|
||||
</ul></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
@ -18,7 +17,7 @@
|
||||
<p>text</p>
|
||||
|
||||
<ul>
|
||||
<li>a<br /></li>
|
||||
<li>b<br /></li>
|
||||
<li>c<br /></li>
|
||||
<li>a</li>
|
||||
<li>b</li>
|
||||
<li>c</li>
|
||||
</ul>
|
||||
|
4
pkg/mark/testdata/newlines.html
vendored
4
pkg/mark/testdata/newlines.html
vendored
@ -1,4 +1,4 @@
|
||||
<p>one-1<br />
|
||||
<p>one-1
|
||||
one-2</p>
|
||||
|
||||
<p>two-1</p>
|
||||
@ -9,7 +9,7 @@ one-2</p>
|
||||
|
||||
<p>three-2</p>
|
||||
|
||||
<p>space-1<br />
|
||||
<p>space-1
|
||||
space-2</p>
|
||||
|
||||
<p>2space-1<br />
|
||||
|
4
pkg/mark/testdata/tags.html
vendored
4
pkg/mark/testdata/tags.html
vendored
@ -1,5 +1,5 @@
|
||||
<p><b>bold</b><br />
|
||||
<p><b>bold</b>
|
||||
<strong>bold</strong></p>
|
||||
|
||||
<p><i>vitalik</i><br />
|
||||
<p><i>vitalik</i>
|
||||
<em>vitalik</em></p>
|
||||
|
Loading…
x
Reference in New Issue
Block a user