Skip to content

Commit fc090b2

Browse files
feat: configure start of ol list
1 parent a5828fd commit fc090b2

File tree

6 files changed

+217
-1
lines changed

6 files changed

+217
-1
lines changed

testdata/TestCommonmark/list/goldmark.golden

+58
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,65 @@
7575
<li>
7676
<p>22</p>
7777
</li>
78+
<li>
79+
<p>10</p>
80+
</li>
81+
<li>
82+
<p>11</p>
83+
</li>
84+
<li>
85+
<p>12</p>
86+
</li>
87+
<li>
88+
<p>13</p>
89+
</li>
90+
<li>
91+
<p>1</p>
92+
</li>
93+
<li>
94+
<p>2</p>
95+
</li>
96+
<li>
97+
<p>3</p>
98+
</li>
99+
<li>
100+
<p>4</p>
101+
</li>
102+
<li>
103+
<p>1</p>
104+
</li>
105+
<li>
106+
<p>2</p>
107+
</li>
108+
<li>
109+
<p>3</p>
110+
</li>
111+
<li>
112+
<p>4</p>
113+
</li>
114+
<li>
115+
<p>0</p>
116+
</li>
117+
<li>
118+
<p>1</p>
119+
</li>
120+
<li>
121+
<p>2</p>
122+
</li>
123+
<li>
124+
<p>3</p>
125+
</li>
126+
<li>
127+
<p>Echo the word &quot;foo&quot;</p>
128+
</li>
78129
</ol>
130+
<pre><code>echo('foo')
131+
</code></pre>
132+
<ol start="4">
133+
<li>Now echo &quot;bar&quot;</li>
134+
</ol>
135+
<pre><code>echo('bar')
136+
</code></pre>
79137
<ul>
80138
<li>
81139
<p>Link: <a href="https://example.com">example</a> works</p>

testdata/TestCommonmark/list/input.html

+41
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,47 @@
4444
</ol>
4545

4646

47+
<!--ordered list with different start-->
48+
<ol start="10">
49+
<li>10</li>
50+
<li>11</li>
51+
<li>12</li>
52+
<li>13</li>
53+
</ol>
54+
55+
<!--ordered list with invalid start-->
56+
<ol start="abc">
57+
<li>1</li>
58+
<li>2</li>
59+
<li>3</li>
60+
<li>4</li>
61+
</ol>
62+
63+
<!--ordered list with negative start-->
64+
<ol start="-2">
65+
<li>1</li>
66+
<li>2</li>
67+
<li>3</li>
68+
<li>4</li>
69+
</ol>
70+
71+
<!--for the programmers start at zero-->
72+
<ol start="0">
73+
<li>0</li>
74+
<li>1</li>
75+
<li>2</li>
76+
<li>3</li>
77+
</ol>
78+
79+
<!--usage of start-->
80+
<div>
81+
<ol start=3><li>Echo the word "foo"</ol></li>
82+
<pre><code>echo('foo')</code></pre>
83+
<ol start=4><li>Now echo "bar"</ol></li>
84+
<pre><code>echo('bar')</code></pre>
85+
</div>
86+
87+
4788
<!--ul list with link inside-->
4889
<ul>
4990
<li>Link: <a href="https://example.com" target="_blank">example</a> works</li>

testdata/TestCommonmark/list/output.asterisks.golden

+32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@
2727
24. ![](http://example.com/example.png)
2828
25. 22
2929

30+
10. 10
31+
11. 11
32+
12. 12
33+
13. 13
34+
35+
1. 1
36+
2. 2
37+
3. 3
38+
4. 4
39+
40+
1. 1
41+
2. 2
42+
3. 3
43+
4. 4
44+
45+
0. 0
46+
1. 1
47+
2. 2
48+
3. 3
49+
50+
3. Echo the word "foo"
51+
52+
```
53+
echo('foo')
54+
```
55+
56+
4. Now echo "bar"
57+
58+
```
59+
echo('bar')
60+
```
61+
3062
* Link: [example](https://example.com) works
3163
* Link:
3264
[example](https://example.com)

testdata/TestCommonmark/list/output.dash.golden

+32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@
2727
24. ![](http://example.com/example.png)
2828
25. 22
2929

30+
10. 10
31+
11. 11
32+
12. 12
33+
13. 13
34+
35+
1. 1
36+
2. 2
37+
3. 3
38+
4. 4
39+
40+
1. 1
41+
2. 2
42+
3. 3
43+
4. 4
44+
45+
0. 0
46+
1. 1
47+
2. 2
48+
3. 3
49+
50+
3. Echo the word "foo"
51+
52+
```
53+
echo('foo')
54+
```
55+
56+
4. Now echo "bar"
57+
58+
```
59+
echo('bar')
60+
```
61+
3062
- Link: [example](https://example.com) works
3163
- Link:
3264
[example](https://example.com)

testdata/TestCommonmark/list/output.plus.golden

+32
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@
2727
24. ![](http://example.com/example.png)
2828
25. 22
2929

30+
10. 10
31+
11. 11
32+
12. 12
33+
13. 13
34+
35+
1. 1
36+
2. 2
37+
3. 3
38+
4. 4
39+
40+
1. 1
41+
2. 2
42+
3. 3
43+
4. 4
44+
45+
0. 0
46+
1. 1
47+
2. 2
48+
3. 3
49+
50+
3. Echo the word "foo"
51+
52+
```
53+
echo('foo')
54+
```
55+
56+
4. Now echo "bar"
57+
58+
```
59+
echo('bar')
60+
```
61+
3062
+ Link: [example](https://example.com) works
3163
+ Link:
3264
[example](https://example.com)

utils.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,26 @@ func isWrapperListItem(s *goquery.Selection) bool {
359359
return noOwnText && childIsList
360360
}
361361

362+
// getListStart returns the integer from which the counting
363+
// for for the list items should start from.
364+
// -> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#start
365+
func getListStart(parent *goquery.Selection) int {
366+
val := parent.AttrOr("start", "")
367+
if val == "" {
368+
return 1
369+
}
370+
371+
num, err := strconv.Atoi(val)
372+
if err != nil {
373+
return 1
374+
}
375+
376+
if num < 0 {
377+
return 1
378+
}
379+
return num
380+
}
381+
362382
// getListPrefix returns the appropriate prefix for the list item.
363383
// For example "- ", "* ", "1. ", "01. ", ...
364384
func getListPrefix(opt *Options, s *goquery.Selection) string {
@@ -370,7 +390,8 @@ func getListPrefix(opt *Options, s *goquery.Selection) string {
370390
if parent.Is("ul") {
371391
return opt.BulletListMarker + " "
372392
} else if parent.Is("ol") {
373-
currentIndex := s.Index() + 1
393+
start := getListStart(parent)
394+
currentIndex := start + s.Index()
374395

375396
lastIndex := parent.Children().Last().Index() + 1
376397
maxLength := len(strconv.Itoa(lastIndex))

0 commit comments

Comments
 (0)