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
4 changes: 3 additions & 1 deletion src/Renderer/MarkdownRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ protected function escapeText(string $text): string
{
// Escape special Markdown characters in text
// But be careful not to over-escape
return preg_replace('/([\\\\`*_\[\]#])/', '\\\\$1', $text) ?? $text;
$escaped = preg_replace('/([\\\\`*_\[\]#])/', '\\\\$1', $text) ?? $text;

return preg_replace('/<(?=[A-Za-z\/!?])/', '\\\\<', $escaped) ?? $escaped;
}
}
38 changes: 38 additions & 0 deletions tests/TestCase/Renderer/MarkdownRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,44 @@ public function testInlineCode(): void
$this->assertStringContainsString('`print()`', $this->renderer->render($document));
}

public function testEscapesLessThanThatWouldOpenMarkup(): void
{
$document = $this->converter->parse('a<b>c');

$this->assertSame("a\\<b>c\n", $this->renderer->render($document));
}

public function testLeavesComparisonOperatorsUnescaped(): void
{
$document = $this->converter->parse('a < b and 3 > 2');

$this->assertSame("a < b and 3 > 2\n", $this->renderer->render($document));
}

public function testAutolinkLessThanIsNotEscaped(): void
{
$document = $this->converter->parse('<https://example.com>');

$this->assertSame("[https://example.com](https://example.com)\n", $this->renderer->render($document));
}

public function testCodeSpanLessThanIsNotEscaped(): void
{
$document = $this->converter->parse('`<b>`');

$this->assertSame("`<b>`\n", $this->renderer->render($document));
}

public function testLessThanEscapeLookahead(): void
{
$document = $this->converter->parse("5 < 6\n\nx<!DOCTYPE y\n\nz</b> w\n\nq<?php r");

$this->assertSame(
"5 < 6\n\nx\\<!DOCTYPE y\n\nz\\</b> w\n\nq\\<?php r\n",
$this->renderer->render($document),
);
}

public function testUnorderedList(): void
{
// Test dash marker
Expand Down
Loading