upgrade to blackfriday v2, fixes #53 and #61, add tests

This commit is contained in:
Egor Kovetskiy 2021-02-02 17:43:31 +03:00
parent e190cdfb34
commit 7f6466a882
8 changed files with 124 additions and 66 deletions

2
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
github.com/alecthomas/colour v0.1.0 // indirect github.com/alecthomas/colour v0.1.0 // indirect
github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 // 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/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/go-yaml/yaml v2.1.0+incompatible // indirect github.com/go-yaml/yaml v2.1.0+incompatible // indirect
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 // 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/pkg v0.0.0-20201028091908-8e9a5e0226ef
github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4 github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4
github.com/russross/blackfriday v1.5.2 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/sergi/go-diff v1.1.0 // indirect
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.2.8 gopkg.in/yaml.v2 v2.2.8

3
go.sum
View File

@ -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/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 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 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 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -1,18 +1,18 @@
package mark package mark
import ( import (
"bytes" "io"
"regexp" "regexp"
"strconv" "strconv"
"strings" "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"
"github.com/russross/blackfriday" bf "github.com/russross/blackfriday/v2"
) )
type ConfluenceRenderer struct { type ConfluenceRenderer struct {
blackfriday.Renderer bf.Renderer
Stdlib *stdlib.Lib Stdlib *stdlib.Lib
} }
@ -48,30 +48,36 @@ func ParseTitle(lang string) string {
return "" return ""
} }
func (renderer ConfluenceRenderer) BlockCode( func (renderer ConfluenceRenderer) RenderNode(
out *bytes.Buffer, writer io.Writer,
text []byte, node *bf.Node,
lang string, entering bool,
) { ) bf.WalkStatus {
renderer.Stdlib.Templates.ExecuteTemplate( if node.Type == bf.CodeBlock {
out, lang := string(node.Info)
"ac:code", renderer.Stdlib.Templates.ExecuteTemplate(
struct { writer,
Language string "ac:code",
Collapse string struct {
Title string Language string
Text string Collapse string
}{ Title string
ParseLanguage(lang), Text string
strconv.FormatBool(strings.Contains(lang, "collapse")), }{
ParseTitle(lang), ParseLanguage(lang),
string(text), 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 // 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. // <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
func CompileMarkdown( func CompileMarkdown(
markdown []byte, markdown []byte,
@ -79,7 +85,7 @@ func CompileMarkdown(
) string { ) string {
log.Tracef(nil, "rendering markdown:\n%s", string(markdown)) log.Tracef(nil, "rendering markdown:\n%s", string(markdown))
colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`) colon := regexp.MustCompile(`---bf-COLON---`)
tags := regexp.MustCompile(`<(/?\S+?):(\S+?)>`) tags := regexp.MustCompile(`<(/?\S+?):(\S+?)>`)
@ -89,37 +95,37 @@ func CompileMarkdown(
) )
renderer := ConfluenceRenderer{ renderer := ConfluenceRenderer{
Renderer: blackfriday.HtmlRenderer( Renderer: bf.NewHTMLRenderer(
blackfriday.HTML_USE_XHTML| bf.HTMLRendererParameters{
blackfriday.HTML_USE_SMARTYPANTS| Flags: bf.UseXHTML |
blackfriday.HTML_SMARTYPANTS_FRACTIONS| bf.Smartypants |
blackfriday.HTML_SMARTYPANTS_DASHES| bf.SmartypantsFractions |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES, bf.SmartypantsDashes |
"", "", bf.SmartypantsLatexDashes,
},
), ),
Stdlib: stdlib, Stdlib: stdlib,
} }
html := blackfriday.MarkdownOptions( html := bf.Run(
markdown, markdown,
renderer, bf.WithRenderer(renderer),
blackfriday.Options{ bf.WithExtensions(
Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS | bf.NoIntraEmphasis|
blackfriday.EXTENSION_TABLES | bf.Tables|
blackfriday.EXTENSION_FENCED_CODE | bf.FencedCode|
blackfriday.EXTENSION_AUTOLINK | bf.Autolink|
blackfriday.EXTENSION_LAX_HTML_BLOCKS | bf.LaxHTMLBlocks|
blackfriday.EXTENSION_STRIKETHROUGH | bf.Strikethrough|
blackfriday.EXTENSION_SPACE_HEADERS | bf.SpaceHeadings|
blackfriday.EXTENSION_HEADER_IDS | bf.HeadingIDs|
blackfriday.EXTENSION_AUTO_HEADER_IDS | bf.AutoHeadingIDs|
blackfriday.EXTENSION_TITLEBLOCK | bf.Titleblock|
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK | bf.BackslashLineBreak|
blackfriday.EXTENSION_DEFINITION_LISTS | bf.DefinitionLists|
blackfriday.EXTENSION_HARD_LINE_BREAK | bf.NoEmptyLineBeforeBlock,
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK, ),
},
) )
html = colon.ReplaceAll(html, []byte(`:`)) html = colon.ReplaceAll(html, []byte(`:`))

View File

@ -36,7 +36,7 @@
</ac:rich-text-body> </ac:rich-text-body>
</ac:structured-macro> </ac:structured-macro>
<p>text<br /> <p>text
text 2</p> text 2</p>
<ac:structured-macro ac:name="expand"> <ac:structured-macro ac:name="expand">
<ac:parameter ac:name="title"></ac:parameter> <ac:parameter ac:name="title"></ac:parameter>
@ -50,3 +50,39 @@ text 2</p>
</ac:structured-macro> </ac:structured-macro>
</ac:rich-text-body> </ac:rich-text-body>
</ac:structured-macro> </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>

View File

@ -15,3 +15,15 @@ text 2
```unknown ```unknown
unknown code 2 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
```

View File

@ -1,16 +1,15 @@
<ul> <ul>
<li>dash 1-1<br /></li> <li>dash 1-1</li>
<li>dash 1-2<br /></li> <li>dash 1-2</li>
<li>dash 1-3<br /> <li>dash 1-3
<ul> <ul>
<li>dash 1-3-1<br /></li> <li>dash 1-3-1</li>
<li>dash 1-3-2<br /></li> <li>dash 1-3-2</li>
<li>dash 1-3-3<br /> <li>dash 1-3-3
<ul> <ul>
<li>dash 1-3-3-1<br /> <li>dash 1-3-3-1</li>
<br /></li>
</ul></li> </ul></li>
</ul></li> </ul></li>
</ul> </ul>
@ -18,7 +17,7 @@
<p>text</p> <p>text</p>
<ul> <ul>
<li>a<br /></li> <li>a</li>
<li>b<br /></li> <li>b</li>
<li>c<br /></li> <li>c</li>
</ul> </ul>

View File

@ -1,4 +1,4 @@
<p>one-1<br /> <p>one-1
one-2</p> one-2</p>
<p>two-1</p> <p>two-1</p>
@ -9,7 +9,7 @@ one-2</p>
<p>three-2</p> <p>three-2</p>
<p>space-1<br /> <p>space-1
space-2</p> space-2</p>
<p>2space-1<br /> <p>2space-1<br />

View File

@ -1,5 +1,5 @@
<p><b>bold</b><br /> <p><b>bold</b>
<strong>bold</strong></p> <strong>bold</strong></p>
<p><i>vitalik</i><br /> <p><i>vitalik</i>
<em>vitalik</em></p> <em>vitalik</em></p>