diff --git a/src/Parser/BlockParser.php b/src/Parser/BlockParser.php index 277a963..ded4094 100644 --- a/src/Parser/BlockParser.php +++ b/src/Parser/BlockParser.php @@ -1518,20 +1518,25 @@ 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++; @@ -1539,7 +1544,7 @@ protected function tryParseDiv(Node $parent, array $lines, int $start, ?array $l break; } - $this->trackBlockQuoteLazyState($currentLine, $state); + $this->trackDivBodyLine($currentLine, $scan); $i++; } @@ -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, 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, 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 @@ -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)) { diff --git a/tests/TestCase/BlockStructureParityTest.php b/tests/TestCase/BlockStructureParityTest.php index 487a94f..d8457b7 100644 --- a/tests/TestCase/BlockStructureParityTest.php +++ b/tests/TestCase/BlockStructureParityTest.php @@ -101,6 +101,86 @@ public static function htmlProvider(): array ":::\ntext\n\n```\n:::\n```\n:::\n", "
\n

text

\n
:::\n
\n
\n", ], + 'a fence after a heading still hides the closer' => [ + ":::\n# heading\n```\n:::\n```\n:::\n", + "
\n

heading

\n
:::\n
\n
\n", + ], + 'a fence after a thematic break still hides the closer' => [ + ":::\n---\n```\n:::\n```\n:::\n", + "
\n
\n
:::\n
\n
\n", + ], + 'a fence after a table row still hides the closer' => [ + ":::\n| t |\n```\n:::\n```\n:::\n", + "
\n\n\n\n\n
t
\n
:::\n
\n
\n", + ], + 'a fence after an attribute line still hides the closer' => [ + ":::\n{.c}\n```\n:::\n```\n:::\n", + "
\n
:::\n
\n
\n", + ], + 'a fence after a heading continuation still hides the closer' => [ + ":::\n# h\nmore\n```\n:::\n```\n:::\n", + "
\n

h\nmore

\n
:::\n
\n
\n", + ], + 'an unindented fence after a list item still hides the closer' => [ + ":::\n- i\n```\n:::\n```\n:::\n", + "
\n
    \n
  • \ni\n
  • \n
\n
:::\n
\n
\n", + ], + 'a fence after a quote still hides the closer' => [ + ":::\n> q\n```\n:::\n```\n:::\n", + "
\n
\n

q

\n
\n
:::\n
\n
\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", + "
\n
    \n
  • \n

    i

    \n

    para

    \n
  • \n
\n
:::\n
\n
\n", + ], + 'a fence after a quote that follows a list item still hides the closer' => [ + ":::\n- a\n> q\n ```\n:::\n ```\n:::\n", + "
\n
    \n
  • \na\n
  • \n
\n
\n

q

\n
\n
:::\n
\n
\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", + "
\n
    \n
  1. \nc\n- b\n
  2. \n
\n
\n
\n", + ], + 'an empty task item opens no paragraph for a fence line' => [ + ":::\n- [ ]\n`x`\n- a\n```\n:::\n", + "
\n
    \n
  • \n\n
  • \n
\n

x\n- a\n

\n
\n", + ], + 'empty quote lines open no paragraph for a fence line' => [ + ":::\n>\n>\n`x`\n ```\n :::\nafter\n", + "
\n
\n
\n

x\n

\n
\n

after

\n", + ], + 'a fence inside a list item ends with the item' => [ + ":::\n- a\n# h\n- [ ]\n ```\n{.c}\n > r\n:::\n", + "
\n
    \n
  • \na\n
  • \n
\n

h

\n
    \n
  • \n\n
    \n
  • \n
\n
\n

r

\n
\n
\n", + ], + 'a table row ends the heading before a paragraph' => [ + ":::\n- x\n# h\n| t |\n`x`\n```\n:::\n", + "
\n
    \n
  • \nx\n
  • \n
\n

h

\n\n\n\n\n
t
\n

x\n

\n
\n", + ], + 'a fence inside a list item ends at an unindented paragraph line' => [ + ":::\n- [ ]\n ```\nx\n:::\n", + "
\n
    \n
  • \n\n
    \n
  • \n
\n

x

\n
\n", + ], + 'a thematic break ends the heading before a paragraph' => [ + ":::\n# h\n- - -\n`x`\n```\n:::\n", + "
\n

h

\n
\n

x\n

\n
\n", + ], + 'closing pipe inside an open code span ends the table' => [ + "| a |\n| `a |\n", + "\n\n\n\n
a
\n

| a |

\n", + ], + 'closing pipe inside an open code span on an indented row ends the table' => [ + "| a |\n | `a |\n", + "\n\n\n\n
a
\n

| a |

\n", + ], + 'escaped closing pipe ends the table' => [ + "| a |\n| b \\|\n", + "\n\n\n\n
a
\n

| b |

\n", + ], + 'escaped closing pipe on an indented row ends the table' => [ + "| a |\n | b \\|\n", + "\n\n\n\n
a
\n

| b |

\n", + ], 'line before a sublist past a task box' => [ "- [ ] *\n `x`\n", "\n", @@ -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("
\n

visible

\n
\n", $html); + } + #[DataProvider('htmlProvider')] public function testMatchesDjotJs(string $input, string $expected): void {