Skip to content

Commit 0891af9

Browse files
committed
Prefer list of items over single item for list content
Changes the fuzzy handling of `content` properties on BulletList and OrderedList. A list of blocks is now interpreted as a list of single-block list items instead of a single item containing the given list. This allows for more intuitive behavior of the `content` property of BulletList and OrderedList elements.
1 parent ca7b48f commit 0891af9

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/Text/Pandoc/Lua/Marshal/Block.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ setBlockContent _ = \case
307307
c -> throwM . luaException @e $
308308
"expected definition items, got " <> contentTypeDescription c
309309
listItemContent = \case
310-
ContentBlocks blks -> [blks]
310+
ContentBlocks blks -> map (:[]) blks
311311
ContentLines lns -> map ((:[]) . Plain) lns
312312
ContentListItems itms -> itms
313313
c -> throwM . luaException @e $

src/Text/Pandoc/Lua/Marshal/Content.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ data Content
3939
| ContentDefItems [([Inline], [[Block]])]
4040
| ContentListItems [[Block]]
4141

42-
-- | Gets the text property of an Inline, if present.
42+
-- | Returns a human-readable type description; used for error messages.
4343
contentTypeDescription :: Content -> String
4444
contentTypeDescription = \case
4545
ContentBlocks {} -> "list of Block items"

test/test-block.lua

+30-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ return {
99
group 'BlockQuote' {
1010
test('access content via property `content`', function ()
1111
local elem = BlockQuote{'word'}
12-
assert.are_same(elem.content, {Plain 'word'})
12+
assert.are_equal(elem.content, Blocks{Plain 'word'})
1313
assert.are_equal(type(elem.content), 'table')
1414

1515
elem.content = {
@@ -29,16 +29,40 @@ return {
2929
test('access items via property `content`', function ()
3030
local para = Para 'one'
3131
local blist = BulletList{{para}}
32-
assert.are_same({{para}}, blist.content)
32+
assert.are_equal(List{Blocks{para}}, blist.content)
33+
end),
34+
test('property `content` is a list of Block lists', function ()
35+
local items = List{Blocks{Plain 'item 1'}, Blocks{Plain 'item 2'}}
36+
local blist = BulletList{}
37+
blist.content = items
38+
assert.are_equal(items, blist:clone().content)
3339
end),
3440
test('property `content` uses fuzzy marshalling', function ()
35-
local old = Plain 'old'
3641
local new = Plain 'new'
37-
local blist = BulletList{{old}}
42+
local blist = BulletList{{Plain 'old'}}
3843
blist.content = {{new}}
39-
assert.are_same({{new}}, blist:clone().content)
44+
assert.are_equal(List{Blocks{new}}, blist:clone().content)
4045
blist.content = new
41-
assert.are_same({{new}}, blist:clone().content)
46+
assert.are_equal(List{Blocks{new}}, blist:clone().content)
47+
end),
48+
test('property `content` prioritizes lists', function ()
49+
local blist = BulletList{}
50+
local one, two = Para 'one', Plain 'two'
51+
blist.content = {one, two}
52+
assert.are_equal(
53+
List{Blocks{one}, Blocks{two}},
54+
blist:clone().content
55+
)
56+
end),
57+
test('mixing types works', function ()
58+
local one = Plain 'one'
59+
local two = 'two'
60+
local blist = BulletList{}
61+
blist.content = {one, two}
62+
assert.are_same(
63+
List{Blocks{one}, Blocks{Plain(two)}},
64+
blist:clone().content
65+
)
4266
end),
4367
},
4468
group 'CodeBlock' {

0 commit comments

Comments
 (0)