mirror of
https://github.com/danog/blackfriday.git
synced 2024-12-02 09:27:49 +01:00
Fix block parsing for fenced code blocks in lists (#476)
This is a backport of the initial fix in v2 (#372).
This commit is contained in:
parent
46c73eb196
commit
f1f45ab762
23
block.go
23
block.go
@ -1143,6 +1143,7 @@ func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int {
|
|||||||
// process the following lines
|
// process the following lines
|
||||||
containsBlankLine := false
|
containsBlankLine := false
|
||||||
sublist := 0
|
sublist := 0
|
||||||
|
codeBlockMarker := ""
|
||||||
|
|
||||||
gatherlines:
|
gatherlines:
|
||||||
for line < len(data) {
|
for line < len(data) {
|
||||||
@ -1170,6 +1171,28 @@ gatherlines:
|
|||||||
|
|
||||||
chunk := data[line+indent : i]
|
chunk := data[line+indent : i]
|
||||||
|
|
||||||
|
if p.flags&EXTENSION_FENCED_CODE != 0 {
|
||||||
|
// determine if in or out of codeblock
|
||||||
|
// if in codeblock, ignore normal list processing
|
||||||
|
_, marker := isFenceLine(chunk, nil, codeBlockMarker, false)
|
||||||
|
if marker != "" {
|
||||||
|
if codeBlockMarker == "" {
|
||||||
|
// start of codeblock
|
||||||
|
codeBlockMarker = marker
|
||||||
|
} else {
|
||||||
|
// end of codeblock.
|
||||||
|
*flags |= LIST_ITEM_CONTAINS_BLOCK
|
||||||
|
codeBlockMarker = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// we are in a codeblock, write line, and continue
|
||||||
|
if codeBlockMarker != "" || marker != "" {
|
||||||
|
raw.Write(data[line+indent : i])
|
||||||
|
line = i
|
||||||
|
continue gatherlines
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// evaluate how this line fits in
|
// evaluate how this line fits in
|
||||||
switch {
|
switch {
|
||||||
// is this a nested list item?
|
// is this a nested list item?
|
||||||
|
@ -1583,6 +1583,44 @@ func TestFencedCodeBlock_EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK(t *testing.T) {
|
|||||||
doTestsBlock(t, tests, EXTENSION_FENCED_CODE|EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK)
|
doTestsBlock(t, tests, EXTENSION_FENCED_CODE|EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListWithFencedCodeBlock(t *testing.T) {
|
||||||
|
var tests = []string{
|
||||||
|
"1. one\n\n ```\n code\n ```\n\n2. two\n",
|
||||||
|
"<ol>\n<li><p>one</p>\n\n<pre><code>code\n</code></pre></li>\n\n<li><p>two</p></li>\n</ol>\n",
|
||||||
|
// https://github.com/russross/blackfriday/issues/239
|
||||||
|
"1. one\n\n ```\n - code\n ```\n\n2. two\n",
|
||||||
|
"<ol>\n<li><p>one</p>\n\n<pre><code>- code\n</code></pre></li>\n\n<li><p>two</p></li>\n</ol>\n",
|
||||||
|
}
|
||||||
|
doTestsBlock(t, tests, EXTENSION_FENCED_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListWithMalformedFencedCodeBlock(t *testing.T) {
|
||||||
|
// Ensure that in the case of an unclosed fenced code block in a list,
|
||||||
|
// no source gets ommitted (even if it is malformed).
|
||||||
|
// See russross/blackfriday#372 for context.
|
||||||
|
var tests = []string{
|
||||||
|
"1. one\n\n ```\n code\n\n2. two\n",
|
||||||
|
"<ol>\n<li>one\n\n```\ncode\n\n2. two</li>\n</ol>\n",
|
||||||
|
|
||||||
|
"1. one\n\n ```\n - code\n\n2. two\n",
|
||||||
|
"<ol>\n<li>one\n\n```\n- code\n\n2. two</li>\n</ol>\n",
|
||||||
|
}
|
||||||
|
doTestsBlock(t, tests, EXTENSION_FENCED_CODE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListWithFencedCodeBlockNoExtensions(t *testing.T) {
|
||||||
|
// If there is a fenced code block in a list, and FencedCode is not set,
|
||||||
|
// lists should be processed normally.
|
||||||
|
var tests = []string{
|
||||||
|
"1. one\n\n ```\n code\n ```\n\n2. two\n",
|
||||||
|
"<ol>\n<li><p>one</p>\n\n<p><code>\ncode\n</code></p></li>\n\n<li><p>two</p></li>\n</ol>\n",
|
||||||
|
|
||||||
|
"1. one\n\n ```\n - code\n ```\n\n2. two\n",
|
||||||
|
"<ol>\n<li><p>one</p>\n\n<p>```</p>\n\n<ul>\n<li>code\n```</li>\n</ul></li>\n\n<li><p>two</p></li>\n</ol>\n",
|
||||||
|
}
|
||||||
|
doTestsBlock(t, tests, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) {
|
func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) {
|
||||||
var tests = []string{
|
var tests = []string{
|
||||||
"% Some title\n" +
|
"% Some title\n" +
|
||||||
|
Loading…
Reference in New Issue
Block a user