List items#
Problems#
We are interested in sublists indentation rules for all types of lists, and integer overflows in case of ordered lists.
For ordered lists, we are not concerned about using 0
or negative numbers
as list markers so these cases will not be considered. Infact ordred lists
generated by md-toc will always start from 1
.
Talking about indentation rules, I need to mention that the user is responsible for generating a correct markdown list according to the parser’s rules. Let’s see this example:
# foo
## bar
### baz
no problem here because this is rendered by md-toc, using github
as parser,
with:
- [foo](#foo)
- [bar](#bar)
- [baz](#baz)
Now, let’s take the previous example and reverse the order of the lines:
### baz
## bar
# foo
and this is what md-toc renders using github
:
- [baz](#baz)
- [foo](#foo)
- [bar](#bar)
while the user might expect this:
- [baz](#baz)
- [foo](#foo)
- [bar](#bar)
Indentation#
cmark
, github
, gitlab
#
List indentation for sublists with this parser is based on the previous state, as stated in the Commonmark spec, at section 5.2:
“The most important thing to notice is that the position of the text after the list marker determines how much indentation is needed in subsequent blocks in the list item. If the list marker takes up two spaces of indentation, and there are three spaces between the list marker and the next character other than a space or tab, then blocks must be indented five spaces in order to fall under the list item.”
This is also true with the specular case: if our new list element needs less indentation than the one processed currently, we have to use the same number of indentation spaces used somewhere earlier in the list.
redcarpet
#
The following C function returns the first non-whitespace character
after the list marker. The value of 0
is returned if the input
line is not a list element. List item rules are explained in the
single line comments.
1/* prefix_uli • returns unordered list item prefix */
2static size_t
3prefix_uli(uint8_t *data, size_t size)
4{
5 size_t i = 0;
6
7 // There can be up to 3 whitespaces before the list marker.
8 if (i < size && data[i] == ' ') i++;
9 if (i < size && data[i] == ' ') i++;
10 if (i < size && data[i] == ' ') i++;
11
12 // The next non-whitespace character must be a list marker and
13 // the character after the list marker must be a whitespace.
14 if (i + 1 >= size ||
15 (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
16 data[i + 1] != ' ')
17 return 0;
18
19 // Check that the next line is not a header
20 // that uses the `-` or `=` characters as markers.
21 if (is_next_headerline(data + i, size - i))
22 return 0;
23
24 // Return the first non whitespace character after the list marker.
25 return i + 2;
26}
As far as I can tell from the previous and other functions, on a new list block the 4 spaces indentation rule applies:
This means that anything that has more than 3 whitespaces is considered as sublist. The only exception seems to be for the first sublist in a list block, in which that case even a single whitespace counts as a sublist. The 4 spaces indentation rule appllies nontheless, so to keep things simple md-toc will always use 4 whitespaces for sublists. Apparently, ordered and unordered lists share the same proprieties.
Let’s see this example:
- I
- am
- foo
stop
- I
- am
- foo
This is how redcarpet renders it once you run $ redcarpet
:
<ul>
<li>I
<ul>
<li>am
<ul>
<li>foo</li>
</ul></li>
</ul></li>
</ul>
<p>stop</p>
<ul>
<li>I
<ul>
<li>am
<ul>
<li>foo</li>
</ul></li>
</ul></li>
</ul>
What follows is an extract of a C function in redcarpet that parses list items. I have added all the single line comments.
1/* parse_listitem • parsing of a single list item */
2/* assuming initial prefix is already removed */
3static size_t
4parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data,
5size_t size, int *flags)
6{
7 struct buf *work = 0, *inter = 0;
8 size_t beg = 0, end, pre, sublist = 0, orgpre = 0, i;
9 int in_empty = 0, has_inside_empty = 0, in_fence = 0;
10
11 // This is the base case, usually of indentation 0 but it can be
12 // from 0 to 3 spaces. If it was 4 spaces it would be a code
13 // block.
14 /* keeping track of the first indentation prefix */
15 while (orgpre < 3 && orgpre < size && data[orgpre] == ' ')
16 orgpre++;
17
18 // Get the first index of string after the list marker. Try both
19 // ordered and unordered lists
20 beg = prefix_uli(data, size);
21 if (!beg)
22 beg = prefix_oli(data, size);
23
24 if (!beg)
25 return 0;
26
27 /* skipping to the beginning of the following line */
28 end = beg;
29 while (end < size && data[end - 1] != '\n')
30 end++;
31 // Iterate line by line using the '\n' character as delimiter.
32 /* process the following lines */
33 while (beg < size) {
34 size_t has_next_uli = 0, has_next_oli = 0;
35
36 // Go to the next line.
37 end++;
38
39 // Find the end of the line.
40 while (end < size && data[end - 1] != '\n')
41 end++;
42
43 // Skip the next line if it is empty.
44 /* process an empty line */
45 if (is_empty(data + beg, end - beg)) {
46 in_empty = 1;
47 beg = end;
48 continue;
49 }
50
51 // Count up to 4 characters of indentation.
52 // If we have 4 characters then it might be a sublist.
53 // Note that this is an offset and does not point to an
54 // index in the actual line string.
55 /* calculating the indentation */
56 i = 0;
57 while (i < 4 && beg + i < end && data[beg + i] == ' ')
58 i++;
59
60 pre = i;
61
62 /* Only check for new list items if we are **not** inside
63 * a fenced code block */
64 if (!in_fence) {
65 has_next_uli = prefix_uli(data + beg + i, end - beg - i);
66 has_next_oli = prefix_oli(data + beg + i, end - beg - i);
67 }
68
69 /* checking for ul/ol switch */
70 if (in_empty && (
71 ((*flags & MKD_LIST_ORDERED) && has_next_uli) ||
72 (!(*flags & MKD_LIST_ORDERED) && has_next_oli))){
73 *flags |= MKD_LI_END;
74 break; /* the following item must have same list type */
75 }
76
77 // Determine if we are dealing with:
78 // - an empty line
79 // - a new list item
80 // - a sublist
81 /* checking for a new item */
82 if ((has_next_uli && !is_hrule(data + beg + i, end - beg - i)) || has_next_oli) {
83 if (in_empty)
84 has_inside_empty = 1;
85
86 // The next list item's indentation (pre) must be the same as
87 // the previous one (orgpre), otherwise it might be a
88 // sublist.
89 if (pre == orgpre) /* the following item must have */
90 break; /* the same indentation */
91
92 // If the indentation does not match the previous one then
93 // assume that it is a sublist. Check later whether it is
94 // or not.
95 if (!sublist)
96 sublist = work->size;
97 }
98 /* joining only indented stuff after empty lines */
99 else if (in_empty && i < 4 && data[beg] != '\t') {
100 *flags |= MKD_LI_END;
101 break;
102 }
103 else if (in_empty) {
104 // Add a line delimiter to the next line if it is missing.
105 bufputc(work, '\n');
106 has_inside_empty = 1;
107 }
108
109 in_empty = 0;
110 beg = end;
111 }
112
113 if (*flags & MKD_LI_BLOCK) {
114 /* intermediate render of block li */
115 if (sublist && sublist < work->size) {
116 parse_block(inter, rndr, work->data, sublist);
117 parse_block(inter, rndr, work->data + sublist, work->size - sublist);
118 }
119 else
120 parse_block(inter, rndr, work->data, work->size);
121}
According to the code, parse_listitem
is called indirectly by
parse_block
(via parse_list
), but parse_block
is called directly
by parse_listitem
so the code analysis
is not trivial. For this reason I might be mistaken about the 4 spaces
indentation rule.
Here is an extract of the parse_block
function with the calls to
parse_list
:
1/* parse_block • parsing of one block, returning next uint8_t to parse */
2static void
3parse_block(struct buf *ob, struct sd_markdown *rndr,
4uint8_t *data, size_t size)
5{
6 while (beg < size) {
7
8 else if (prefix_uli(txt_data, end))
9 beg += parse_list(ob, rndr, txt_data, end, 0);
10
11 else if (prefix_oli(txt_data, end))
12 beg += parse_list(ob, rndr, txt_data, end, MKD_LIST_ORDERED);
13 }
14}
Overflows#
cmark
, github
, gitlab
#
Ordered list markers cannot exceed 99999999
according to
the following. If that is the case then a GithubOverflowOrderedListMarker
exception is raised:
redcarpet
#
Apparently there are no cases of ordered list marker overflows:
Notes on ordered lists#
cmark
, github
, gitlab
#
Ordered list markers may start with any integer (except special cases). any following number is ignored and subsequent numeration is progressive:
However, when you try this in practice this is not always true: nested lists do not follow the specifications. See:
Markers cannot be negative:
redcarpet
#
Ordered lists do not use the start
HTML attribute:
any number is ignored and lists starts from 1. See: