Skip to content

Commit

Permalink
feat: configure start of ol list
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Dec 27, 2023
1 parent a5828fd commit fc090b2
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 1 deletion.
58 changes: 58 additions & 0 deletions testdata/TestCommonmark/list/goldmark.golden
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,65 @@
<li>
<p>22</p>
</li>
<li>
<p>10</p>
</li>
<li>
<p>11</p>
</li>
<li>
<p>12</p>
</li>
<li>
<p>13</p>
</li>
<li>
<p>1</p>
</li>
<li>
<p>2</p>
</li>
<li>
<p>3</p>
</li>
<li>
<p>4</p>
</li>
<li>
<p>1</p>
</li>
<li>
<p>2</p>
</li>
<li>
<p>3</p>
</li>
<li>
<p>4</p>
</li>
<li>
<p>0</p>
</li>
<li>
<p>1</p>
</li>
<li>
<p>2</p>
</li>
<li>
<p>3</p>
</li>
<li>
<p>Echo the word &quot;foo&quot;</p>
</li>
</ol>
<pre><code>echo('foo')
</code></pre>
<ol start="4">
<li>Now echo &quot;bar&quot;</li>
</ol>
<pre><code>echo('bar')
</code></pre>
<ul>
<li>
<p>Link: <a href="https://example.com">example</a> works</p>
Expand Down
41 changes: 41 additions & 0 deletions testdata/TestCommonmark/list/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,47 @@
</ol>


<!--ordered list with different start-->
<ol start="10">
<li>10</li>
<li>11</li>
<li>12</li>
<li>13</li>
</ol>

<!--ordered list with invalid start-->
<ol start="abc">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>

<!--ordered list with negative start-->
<ol start="-2">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ol>

<!--for the programmers start at zero-->
<ol start="0">
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
</ol>

<!--usage of start-->
<div>
<ol start=3><li>Echo the word "foo"</ol></li>
<pre><code>echo('foo')</code></pre>
<ol start=4><li>Now echo "bar"</ol></li>
<pre><code>echo('bar')</code></pre>
</div>


<!--ul list with link inside-->
<ul>
<li>Link: <a href="https://example.com" target="_blank">example</a> works</li>
Expand Down
32 changes: 32 additions & 0 deletions testdata/TestCommonmark/list/output.asterisks.golden
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@
24. ![](http://example.com/example.png)
25. 22

10. 10
11. 11
12. 12
13. 13

1. 1
2. 2
3. 3
4. 4

1. 1
2. 2
3. 3
4. 4

0. 0
1. 1
2. 2
3. 3

3. Echo the word "foo"

```
echo('foo')
```

4. Now echo "bar"

```
echo('bar')
```

* Link: [example](https://example.com) works
* Link:
[example](https://example.com)
Expand Down
32 changes: 32 additions & 0 deletions testdata/TestCommonmark/list/output.dash.golden
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@
24. ![](http://example.com/example.png)
25. 22

10. 10
11. 11
12. 12
13. 13

1. 1
2. 2
3. 3
4. 4

1. 1
2. 2
3. 3
4. 4

0. 0
1. 1
2. 2
3. 3

3. Echo the word "foo"

```
echo('foo')
```

4. Now echo "bar"

```
echo('bar')
```

- Link: [example](https://example.com) works
- Link:
[example](https://example.com)
Expand Down
32 changes: 32 additions & 0 deletions testdata/TestCommonmark/list/output.plus.golden
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@
24. ![](http://example.com/example.png)
25. 22

10. 10
11. 11
12. 12
13. 13

1. 1
2. 2
3. 3
4. 4

1. 1
2. 2
3. 3
4. 4

0. 0
1. 1
2. 2
3. 3

3. Echo the word "foo"

```
echo('foo')
```

4. Now echo "bar"

```
echo('bar')
```

+ Link: [example](https://example.com) works
+ Link:
[example](https://example.com)
Expand Down
23 changes: 22 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,26 @@ func isWrapperListItem(s *goquery.Selection) bool {
return noOwnText && childIsList
}

// getListStart returns the integer from which the counting
// for for the list items should start from.
// -> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#start
func getListStart(parent *goquery.Selection) int {
val := parent.AttrOr("start", "")
if val == "" {
return 1
}

num, err := strconv.Atoi(val)
if err != nil {
return 1
}

if num < 0 {
return 1
}
return num
}

// getListPrefix returns the appropriate prefix for the list item.
// For example "- ", "* ", "1. ", "01. ", ...
func getListPrefix(opt *Options, s *goquery.Selection) string {
Expand All @@ -370,7 +390,8 @@ func getListPrefix(opt *Options, s *goquery.Selection) string {
if parent.Is("ul") {
return opt.BulletListMarker + " "
} else if parent.Is("ol") {
currentIndex := s.Index() + 1
start := getListStart(parent)
currentIndex := start + s.Index()

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

0 comments on commit fc090b2

Please sign in to comment.