Make captions not suck

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2018-08-02 09:41:49 +00:00
parent 4c3e11dbb9
commit 1c84e4fb3c
3 changed files with 40 additions and 19 deletions

View File

@ -907,8 +907,8 @@ func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
if p.extensions&MmarkCaptions == 0 {
p.addBlock(codeBlock)
finalizeCodeBlock(codeBlock)
} else {
return beg
}
if captionContent, consumed := p.caption(data[beg:]); consumed > 0 {
figure := &ast.CaptionFigure{}
@ -924,7 +924,6 @@ func (p *Parser) fencedCodeBlock(data []byte, doRender bool) int {
beg += consumed
}
}
}
return beg
}
@ -1184,8 +1183,6 @@ func (p *Parser) terminateBlockquote(data []byte, beg, end int) bool {
// parse a blockquote fragment
func (p *Parser) quote(data []byte) int {
quote := &ast.BlockQuote{}
block := p.addBlock(quote)
var raw bytes.Buffer
beg, end := 0, 0
for beg < len(data) {
@ -1214,8 +1211,30 @@ func (p *Parser) quote(data []byte) int {
raw.Write(data[beg:end])
beg = end
}
if p.extensions&MmarkCaptions == 0 {
block := p.addBlock(&ast.BlockQuote{})
p.block(raw.Bytes())
p.finalize(block)
return end
}
if captionContent, consumed := p.caption(data[end:]); consumed > 0 {
figure := &ast.CaptionFigure{}
caption := &ast.Caption{}
p.inline(caption, captionContent)
p.addBlock(figure)
block := &ast.BlockQuote{}
p.addChild(block)
p.block(raw.Bytes())
p.finalize(block)
p.addChild(caption)
p.finalize(figure)
end += consumed
}
return end
}

View File

@ -215,7 +215,7 @@ func canNodeContain(n ast.Node, v ast.Node) bool {
return ok
case *ast.CaptionFigure:
switch v.(type) {
case *ast.CodeBlock, *ast.Caption:
case *ast.CodeBlock, *ast.BlockQuote, *ast.Caption:
return true
default:
return false

4
testdata/mmark.test vendored
View File

@ -41,4 +41,6 @@ Caption: Shakespeare.
<figure>
<blockquote>
<p>To be, or not to be</p>
</blockquote><figcaption>Shakespeare.</figcaption></figure>
</blockquote>
<figcaption>Shakespeare.</figcaption>
</figure>