Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 150 additions & 11 deletions src/Parser/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1518,28 +1518,33 @@ protected function tryParseDiv(Node $parent, array $lines, int $start, ?array $l
$i = $start + 1;
$count = count($lines);
$closed = false;
// A fence-looking line inside an open paragraph is text, so it cannot hide the closer.
$state = [
'inFence' => false,
'fenceChar' => '',
'fenceLength' => 0,
'inComment' => false,
'commentLength' => 0,
'paragraphOpen' => false,
$scan = [
'state' => [
'inFence' => false,
'fenceChar' => '',
'fenceLength' => 0,
'inComment' => false,
'commentLength' => 0,
'paragraphOpen' => false,
],
'inHeading' => false,
'quoteParagraph' => false,
'listColumns' => [],
'fenceItem' => null,
];

while ($i < $count) {
$currentLine = $lines[$i];
if (
!$state['inFence'] && !$state['inComment']
!$scan['state']['inComment'] && !$this->fenceHoldsLine($currentLine, $scan)
&& $this->fencedBlockParser->isDivFenceCloser(ltrim($currentLine, " \t"), $fenceLength)
) {
$i++;
$closed = true;

break;
}
$this->trackBlockQuoteLazyState($currentLine, $state);
$this->trackDivBodyLine($currentLine, $scan);
$i++;
}

Expand Down Expand Up @@ -2180,6 +2185,130 @@ private function trackBlockQuoteLazyState(string $content, array &$state): void
$state['paragraphOpen'] = true;
}

/**
* Track a div body line closely enough to tell a code fence (which hides a `:::`)
* from a fence-looking line inside an open paragraph (which is text).
*
* @param string $line
* @param array{state: array{inFence:bool,fenceChar:string,fenceLength:int,inComment:bool,commentLength:int,paragraphOpen:bool}, inHeading: bool, quoteParagraph: bool, listColumns: array<int>, fenceItem: int|null} $scan
*/
private function trackDivBodyLine(string $line, array &$scan): void
{
$state = &$scan['state'];
$trimmed = ltrim($line, " \t");
$indent = strlen($line) - strlen($trimmed);
$innermostItem = $scan['listColumns'] !== [] ? end($scan['listColumns']) : null;
if ($state['inFence'] && !$this->fenceHoldsLine($line, $scan)) {
$state['inFence'] = false;
$state['paragraphOpen'] = false;
}
$wasInBlock = $state['inFence'] || $state['inComment'];
// A paragraph inside a list item or quote ends at a block line outside that container.
if (
!$wasInBlock && $state['paragraphOpen']
&& (
($scan['quoteParagraph'] && !str_starts_with($trimmed, '>'))
|| ($innermostItem !== null && $indent <= $innermostItem)
)
&& ($this->isBlockElementStart($trimmed) || $this->isSingleLineBlock($trimmed))
) {
$state['paragraphOpen'] = false;
}
$paragraphWasOpen = $state['paragraphOpen'];
$this->trackBlockQuoteLazyState($line, $state);
if (!$wasInBlock && $state['inFence']) {
while ($scan['listColumns'] !== [] && end($scan['listColumns']) >= $indent) {
array_pop($scan['listColumns']);
}
$scan['fenceItem'] = $scan['listColumns'] !== [] ? end($scan['listColumns']) : null;
}
if ($wasInBlock || IndentationHelper::isBlankLine($line)) {
$scan['inHeading'] = false;
$scan['quoteParagraph'] = false;

return;
}
if (!$paragraphWasOpen || !$state['paragraphOpen']) {
while ($scan['listColumns'] !== [] && end($scan['listColumns']) >= $indent) {
array_pop($scan['listColumns']);
}
if (
$this->listParser->parseListItemMarker($trimmed) !== null
&& !preg_match('/^(?:[-*][ \t]*){3,}$/', $trimmed)
) {
$scan['listColumns'][] = $indent;
}
}
if (!$paragraphWasOpen && $state['paragraphOpen'] && $this->onlyContainerMarkers($trimmed)) {
$state['paragraphOpen'] = false;
}
if (!$state['paragraphOpen']) {
$scan['inHeading'] = false;
$scan['quoteParagraph'] = false;

return;
}
if (!$paragraphWasOpen) {
$scan['quoteParagraph'] = str_starts_with($trimmed, '>');
}
// A heading (with its continuation lines), thematic break, table row or
// attribute line opens no paragraph that would turn a fence line into text.
$scan['inHeading'] = ($scan['inHeading'] && !$this->isBlockElementStart($trimmed) && !$this->isSingleLineBlock($trimmed))
|| (!$paragraphWasOpen && preg_match('/^#+(?:[ \t]|$)/', $trimmed) === 1);
if ($scan['inHeading'] || (!$paragraphWasOpen && $this->isSingleLineBlock($trimmed))) {
$state['paragraphOpen'] = false;
}
}

/**
* Whether the scanner's open fence holds $line; a fence inside a list item ends with the item.
*
* @param string $line
* @param array{state: array{inFence:bool,fenceChar:string,fenceLength:int,inComment:bool,commentLength:int,paragraphOpen:bool}, inHeading: bool, quoteParagraph: bool, listColumns: array<int>, fenceItem: int|null} $scan
*/
private function fenceHoldsLine(string $line, array $scan): bool
{
if (!$scan['state']['inFence']) {
return false;
}

return $scan['fenceItem'] === null || IndentationHelper::isBlankLine($line)
|| strspn($line, " \t") > $scan['fenceItem'];
}

/**
* Whether a line holds only quote or list markers (`>`, `- [ ]`, `> -`), which open no paragraph.
*/
private function onlyContainerMarkers(string $line): bool
{
for ($level = 0; $level < self::MAX_NESTING_DEPTH; $level++) {
$line = ltrim($line, " \t");
if ($line === '') {
return true;
}
if ($line[0] === '>') {
$line = substr($line, 1);

continue;
}
$marker = $this->listParser->parseListItemMarker($line);
if ($marker === null || preg_match('/^(?:[-*][ \t]*){3,}$/', $line)) {
return false;
}
$line = $marker['content'];
}

return false;
}

private function isSingleLineBlock(string $line): bool
{
return preg_match('/^(?:[-*][ \t]*){3,}$/', $line) === 1
|| $this->tableParser->isTableRow($line)
|| (str_starts_with($line, '{') && str_ends_with(rtrim($line), '}')
&& AttributeParser::isValid(substr(rtrim($line), 1, -1)));
}

/**
* @param string $content
* @param array{inFence:bool,fenceChar:string,fenceLength:int,inComment:bool,commentLength:int,paragraphOpen:bool} $state
Expand Down Expand Up @@ -3758,10 +3887,20 @@ protected function tryParseTable(Node $parent, array $lines, int $start): ?int
$lineWithoutRowAttrs = $this->tableParser->stripRowAttributes($currentLine);

// Trailing whitespace after the closing pipe is insignificant
// (parity with carve-js / carve-rs).
// (parity with carve-js / carve-rs). An escaped closing pipe ends no row.
if (!preg_match('/^\|.*\|[ \t]*$/', $lineWithoutRowAttrs)) {
break;
}
// Later rows follow the first row's rules: no escaped closing pipe, none inside a code span.
if (
$i > $start && !$this->tableParser->isTableRow($currentLine)
&& !(
$this->tableParser->isPotentialTableRowWithUnclosedCodeSpan($currentLine)
&& $this->canCloseCodeSpanWithContinuations($lines, $i, $count)
)
) {
break;
}

// Check if this is a separator row (attributes ignored on separator rows)
if ($this->tableParser->isSeparatorRow($lineWithoutRowAttrs)) {
Expand Down
87 changes: 87 additions & 0 deletions tests/TestCase/BlockStructureParityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,86 @@ public static function htmlProvider(): array
":::\ntext\n\n```\n:::\n```\n:::\n",
"<div>\n<p>text</p>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a heading still hides the closer' => [
":::\n# heading\n```\n:::\n```\n:::\n",
"<div>\n<h1 id=\"heading\">heading</h1>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a thematic break still hides the closer' => [
":::\n---\n```\n:::\n```\n:::\n",
"<div>\n<hr>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a table row still hides the closer' => [
":::\n| t |\n```\n:::\n```\n:::\n",
"<div>\n<table>\n<tr>\n<td>t</td>\n</tr>\n</table>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after an attribute line still hides the closer' => [
":::\n{.c}\n```\n:::\n```\n:::\n",
"<div>\n<pre class=\"c\"><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a heading continuation still hides the closer' => [
":::\n# h\nmore\n```\n:::\n```\n:::\n",
"<div>\n<h1 id=\"h-more\">h\nmore</h1>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'an unindented fence after a list item still hides the closer' => [
":::\n- i\n```\n:::\n```\n:::\n",
"<div>\n<ul>\n<li>\ni\n</li>\n</ul>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a quote still hides the closer' => [
":::\n> q\n```\n:::\n```\n:::\n",
"<div>\n<blockquote>\n<p>q</p>\n</blockquote>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after an item paragraph past a blank line still hides the closer' => [
":::\n- i\n\n para\n```\n:::\n```\n:::\n",
"<div>\n<ul>\n<li>\n<p>i</p>\n<p>para</p>\n</li>\n</ul>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'a fence after a quote that follows a list item still hides the closer' => [
":::\n- a\n> q\n ```\n:::\n ```\n:::\n",
"<div>\n<ul>\n<li>\na\n</li>\n</ul>\n<blockquote>\n<p>q</p>\n</blockquote>\n<pre><code>:::\n</code></pre>\n</div>\n",
],
'an attribute line after a list item still lets a fence hide the closer' => [
":::\n1. c\n - b\n{.c}\n ```\n```\n:::\n",
"<div>\n<ol>\n<li>\nc\n- b\n</li>\n</ol>\n<pre class=\"c\"><code></code></pre>\n</div>\n",
],
'an empty task item opens no paragraph for a fence line' => [
":::\n- [ ]\n`x`\n- a\n```\n:::\n",
"<div>\n<ul class=\"task-list\">\n<li>\n<input disabled=\"\" type=\"checkbox\"/>\n</li>\n</ul>\n<p><code>x</code>\n- a\n<code></code></p>\n</div>\n",
],
'empty quote lines open no paragraph for a fence line' => [
":::\n>\n>\n`x`\n ```\n :::\nafter\n",
"<div>\n<blockquote>\n</blockquote>\n<p><code>x</code>\n<code></code></p>\n</div>\n<p>after</p>\n",
],
'a fence inside a list item ends with the item' => [
":::\n- a\n# h\n- [ ]\n ```\n{.c}\n > r\n:::\n",
"<div>\n<ul>\n<li>\na\n</li>\n</ul>\n<h1 id=\"h\">h</h1>\n<ul class=\"task-list\">\n<li>\n<input disabled=\"\" type=\"checkbox\"/>\n<pre><code></code></pre>\n</li>\n</ul>\n<blockquote class=\"c\">\n<p>r</p>\n</blockquote>\n</div>\n",
],
'a table row ends the heading before a paragraph' => [
":::\n- x\n# h\n| t |\n`x`\n```\n:::\n",
"<div>\n<ul>\n<li>\nx\n</li>\n</ul>\n<h1 id=\"h\">h</h1>\n<table>\n<tr>\n<td>t</td>\n</tr>\n</table>\n<p><code>x</code>\n<code></code></p>\n</div>\n",
],
'a fence inside a list item ends at an unindented paragraph line' => [
":::\n- [ ]\n ```\nx\n:::\n",
"<div>\n<ul class=\"task-list\">\n<li>\n<input disabled=\"\" type=\"checkbox\"/>\n<pre><code></code></pre>\n</li>\n</ul>\n<p>x</p>\n</div>\n",
],
'a thematic break ends the heading before a paragraph' => [
":::\n# h\n- - -\n`x`\n```\n:::\n",
"<div>\n<h1 id=\"h\">h</h1>\n<hr>\n<p><code>x</code>\n<code></code></p>\n</div>\n",
],
'closing pipe inside an open code span ends the table' => [
"| a |\n| `a |\n",
"<table>\n<tr>\n<td>a</td>\n</tr>\n</table>\n<p>| <code>a |</code></p>\n",
],
'closing pipe inside an open code span on an indented row ends the table' => [
"| a |\n | `a |\n",
"<table>\n<tr>\n<td>a</td>\n</tr>\n</table>\n<p>| <code>a |</code></p>\n",
],
'escaped closing pipe ends the table' => [
"| a |\n| b \\|\n",
"<table>\n<tr>\n<td>a</td>\n</tr>\n</table>\n<p>| b |</p>\n",
],
'escaped closing pipe on an indented row ends the table' => [
"| a |\n | b \\|\n",
"<table>\n<tr>\n<td>a</td>\n</tr>\n</table>\n<p>| b |</p>\n",
],
'line before a sublist past a task box' => [
"- [ ] *\n `x`\n",
"<ul class=\"task-list\">\n<li>\n<input disabled=\"\" type=\"checkbox\"/>\n<ul>\n<li>\n</li>\n</ul>\n<code>x</code>\n</li>\n</ul>\n",
Expand All @@ -124,6 +204,13 @@ public static function htmlProvider(): array
];
}

public function testFencedCommentHidesTheDivCloser(): void
{
$html = (new DjotConverter())->convert("::: outer\n%%%\n:::\n%%%\nvisible\n:::\n");

$this->assertSame("<div class=\"outer\">\n<p>visible</p>\n</div>\n", $html);
}

#[DataProvider('htmlProvider')]
public function testMatchesDjotJs(string $input, string $expected): void
{
Expand Down
Loading