Skip to content

Commit be43e27

Browse files
authored
Merge pull request #142 from thephpleague/ordered-lists
Ordered list improvements
2 parents 1b66954 + 951d67e commit be43e27

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
44

55
## [Unreleased][unreleased]
66

7+
### Added
8+
- Added support for ordered lists starting at numbers other than 1
9+
10+
### Fixed
11+
- Fixed overly-eager escaping of list-like text (#141)
12+
713
## [4.5.0]
814
### Added
915
- Added configuration option for list item style (#135, #136)

src/Converter/ListItemConverter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public function convert(ElementInterface $element)
4848
return $prefix . $list_item_style . ' ' . $value . "\n";
4949
}
5050

51-
$number = $element->getSiblingPosition();
51+
if ($list_type === 'ol' && $start = $element->getParent()->getAttribute('start')) {
52+
$number = $start + $element->getSiblingPosition() - 1;
53+
} else {
54+
$number = $element->getSiblingPosition();
55+
}
5256

5357
return $prefix . $number . '. ' . $value . "\n";
5458
}

src/Converter/ParagraphConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ private function escapeOtherCharactersRegex($line)
109109
{
110110
$regExs = array(
111111
// Match numbers ending on ')' or '.' that are at the beginning of the line.
112-
'/^[0-9]+(?=\)|\.)/'
112+
// They will be escaped if immediately followed by a space or newline.
113+
'/^[0-9]+(?=(\)|\.)( |$))/'
113114
);
114115

115116
foreach ($regExs as $i) {

tests/HtmlConverterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function test_lists()
133133
$this->html_gives_markdown("<ol>\n <li>Item A</li>\n <li>Item B</li>\n</ol>", "1. Item A\n2. Item B");
134134
$this->html_gives_markdown('<ol><li> Item A</li><li> Item B</li></ol>', "1. Item A\n2. Item B");
135135
$this->html_gives_markdown('<ol><li> <h3> Item A</h3><p>Description</p></li><li> Item B</li></ol>', "1. ### Item A\n \n Description\n2. Item B");
136+
$this->html_gives_markdown('<ol start="120"><li>Item A</li><li>Item B</li></ol>', "120. Item A\n121. Item B");
136137
}
137138

138139
public function test_nested_lists()
@@ -142,6 +143,14 @@ public function test_nested_lists()
142143
$this->html_gives_markdown('<ol><li>Item A<ul><li>Nested A</li></ul></li><li>Item B</li></ol>', "1. Item A\n - Nested A\n2. Item B");
143144
}
144145

146+
public function test_list_like_things_which_arent_lists()
147+
{
148+
$this->html_gives_markdown('<p>120.<p>', '120\.');
149+
$this->html_gives_markdown('<p>120. <p>', '120\.');
150+
$this->html_gives_markdown('<p>120.00<p>', '120.00');
151+
$this->html_gives_markdown('<p>120.00 USD<p>', '120.00 USD');
152+
}
153+
145154
public function test_code_samples()
146155
{
147156
$this->html_gives_markdown('<code>&lt;p&gt;Some sample HTML&lt;/p&gt;</code>', '`<p>Some sample HTML</p>`');

0 commit comments

Comments
 (0)