Skip to content

Commit

Permalink
Fix incorrect preference for multi-letter alpha over Roman
Browse files Browse the repository at this point in the history
  • Loading branch information
aboks committed Oct 25, 2022
1 parent 832ddf6 commit 7e47aab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ Supported configuration options:
</ol>
```
Multi-letter alphabetic numerals can consist of at most 3 characters, which should be enough for a
typical list.
typical list. When a list starts with a numeral that can be both Roman or multi-letter alphabetic,
like "II", it is considered to be Roman.

Versioning
----------
Expand Down
4 changes: 2 additions & 2 deletions src/ListParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function parse(ContextInterface $context, Cursor $cursor): bool
$isValidRoman = false;
}
$isValidAlpha = strlen($matches[1]) === 1 || $this->config->get('allow_multi_letter', false);
$preferRomanOverAlpha = $withinRomanList || (!$withinAlphaList && $matches[1] === 'I');
$preferRomanOverAlpha = $withinRomanList || (!$withinAlphaList && ($matches[1] === 'I' || strlen($matches[1]) > 1));

if ($isValidRoman && (!$isValidAlpha || $preferRomanOverAlpha)) {
$data->start = $parsedRomanNumber;
Expand All @@ -124,7 +124,7 @@ public function parse(ContextInterface $context, Cursor $cursor): bool
$isValidRoman = false;
}
$isValidAlpha = strlen($matches[1]) === 1 || $this->config->get('allow_multi_letter', false);
$preferRomanOverAlpha = $withinRomanList || (!$withinAlphaList && $matches[1] === 'i');
$preferRomanOverAlpha = $withinRomanList || (!$withinAlphaList && ($matches[1] === 'i' || strlen($matches[1]) > 1));

if ($isValidRoman && (!$isValidAlpha || $preferRomanOverAlpha)) {
$data->start = $parsedRomanNumber;
Expand Down
40 changes: 40 additions & 0 deletions test/MultiLetterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,46 @@ public function testDoesNotSupportMixingUppercaseAndLowercaseLetters(): void
<p>Aa) foo
Ab) bar
Ac) baz</p>
HTML;

$this->assertMarkdownIsConvertedTo($expectedHtml, $markdown, [
'allow_multi_letter' => true,
]);
}

public function testPrefersRomanNumeralsOverMultiLetterAlphabeticNumerals(): void
{
$markdown = <<<MD
II) foo
III) bar
IV) baz
MD;
$expectedHtml = <<<HTML
<ol type="I" start="2">
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ol>
HTML;

$this->assertMarkdownIsConvertedTo($expectedHtml, $markdown, [
'allow_multi_letter' => true,
]);
}

public function testPrefersMultiLetterAlphabeticNumeralsOverRomanNumeralsWhenAlreadyInAnAlphabeticList(): void
{
$markdown = <<<MD
IH) foo
II) bar
IJ) baz
MD;
$expectedHtml = <<<HTML
<ol type="A" start="242">
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ol>
HTML;

$this->assertMarkdownIsConvertedTo($expectedHtml, $markdown, [
Expand Down

0 comments on commit 7e47aab

Please sign in to comment.