mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Fix for issue russross/blackfriday#239 (#372)
Issue russross/blackfriday#239 codeblock inside list.
This commit is contained in:
parent
3420fef033
commit
8c0d4cca94
22
block.go
22
block.go
|
@ -1258,6 +1258,7 @@ func (p *Markdown) listItem(data []byte, flags *ListType) int {
|
|||
// process the following lines
|
||||
containsBlankLine := false
|
||||
sublist := 0
|
||||
codeBlockMarker := ""
|
||||
|
||||
gatherlines:
|
||||
for line < len(data) {
|
||||
|
@ -1291,6 +1292,27 @@ gatherlines:
|
|||
|
||||
chunk := data[line+indentIndex : i]
|
||||
|
||||
if p.extensions&FencedCode != 0 {
|
||||
// determine if in or out of codeblock
|
||||
// if in codeblock, ignore normal list processing
|
||||
_, marker := isFenceLine(chunk, nil, codeBlockMarker)
|
||||
if marker != "" {
|
||||
if codeBlockMarker == "" {
|
||||
// start of codeblock
|
||||
codeBlockMarker = marker
|
||||
} else {
|
||||
// end of codeblock.
|
||||
codeBlockMarker = ""
|
||||
}
|
||||
}
|
||||
// we are in a codeblock, write line, and continue
|
||||
if codeBlockMarker != "" || marker != "" {
|
||||
raw.Write(data[line+indentIndex : i])
|
||||
line = i
|
||||
continue gatherlines
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate how this line fits in
|
||||
switch {
|
||||
// is this a nested list item?
|
||||
|
|
|
@ -1475,6 +1475,44 @@ func TestFencedCodeBlock_EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK(t *testing.T) {
|
|||
doTestsBlock(t, tests, FencedCode|NoEmptyLineBeforeBlock)
|
||||
}
|
||||
|
||||
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, FencedCode)
|
||||
}
|
||||
|
||||
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```\ncode\n2. two</li>\n</ol>\n",
|
||||
|
||||
"1. one\n\n ```\n - code\n\n2. two\n",
|
||||
"<ol>\n<li>one\n```\n- code\n2. two</li>\n</ol>\n",
|
||||
}
|
||||
doTestsBlock(t, tests, FencedCode)
|
||||
}
|
||||
|
||||
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) {
|
||||
var tests = []string{
|
||||
"% Some title\n" +
|
||||
|
|
Loading…
Reference in New Issue
Block a user