From c5941f316a49c94eff354b63e75b3add98ac4aea Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 21 Oct 2016 12:38:38 +0200 Subject: [PATCH] Fix an issue with links not being reversed in code blocks Fixes #672 + Markdown to HTML unit test --- plugins/markdown/markdown.php | 11 ++++++----- tests/plugins/PluginMarkdownTest.php | 13 +++++++++++++ tests/plugins/resources/markdown.html | 24 ++++++++++++++++++++++++ tests/plugins/resources/markdown.md | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 tests/plugins/resources/markdown.html create mode 100644 tests/plugins/resources/markdown.md diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php index eb7bf23d..a764b6fa 100644 --- a/plugins/markdown/markdown.php +++ b/plugins/markdown/markdown.php @@ -155,8 +155,9 @@ function reverse_text2clickable($description) $lineCount = 0; foreach ($descriptionLines as $descriptionLine) { - // Detect line of code - $codeLineOn = preg_match('/^ /', $descriptionLine) > 0; + // Detect line of code: starting with 4 spaces, + // except lists which can start with +/*/- or `2.` after spaces. + $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0; // Detect and toggle block of code if (!$codeBlockOn) { $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0; @@ -173,10 +174,10 @@ function reverse_text2clickable($description) $descriptionLine ); - // Reverse hashtag links if we're in a code block. - $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : ''; + // Reverse all links in code blocks, only non hashtag elsewhere. + $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?'; $descriptionLine = preg_replace( - '!([^<]+)!m', + '#([^<]+)#m', '$1', $descriptionLine ); diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php index 3593a556..12bdda24 100644 --- a/tests/plugins/PluginMarkdownTest.php +++ b/tests/plugins/PluginMarkdownTest.php @@ -151,4 +151,17 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase $data = hook_markdown_render_daily($data); $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); } + + /** + * Test hashtag links processed with markdown. + */ + function testMarkdownHashtagLinks() + { + $md = file_get_contents('tests/plugins/resources/markdown.md'); + $md = format_description($md); + $html = file_get_contents('tests/plugins/resources/markdown.html'); + + $data = process_markdown($md); + $this->assertEquals($html, $data); + } } diff --git a/tests/plugins/resources/markdown.html b/tests/plugins/resources/markdown.html new file mode 100644 index 00000000..c0fbe7f4 --- /dev/null +++ b/tests/plugins/resources/markdown.html @@ -0,0 +1,24 @@ +
+
    +
  1. zero +
      +
    1. two
    2. +
    3. three
    4. +
    5. four
    6. +
    7. foo #foobar
    8. +
  2. +
+

#foobar foo lol #foo #bar

+

fsdfs http://link.tld #foobar http://link.tld

+
http://link.tld #foobar
+next #foo
+

Block:

+
lorem ipsum #foobar http://link.tld
+#foobar http://link.tld
\ No newline at end of file diff --git a/tests/plugins/resources/markdown.md b/tests/plugins/resources/markdown.md new file mode 100644 index 00000000..0b8be7c5 --- /dev/null +++ b/tests/plugins/resources/markdown.md @@ -0,0 +1,24 @@ +* test: + * [zero](http://link.tld) + + [two](http://link.tld) + - [three](http://link.tld) + +1. [zero](http://link.tld) + 2. [two](http://link.tld) + 3. [three](http://link.tld) + 4. [four](http://link.tld) + 5. foo #foobar + +#foobar foo `lol #foo` #bar + +fsdfs http://link.tld #foobar `http://link.tld` + + http://link.tld #foobar + next #foo + +Block: + +``` +lorem ipsum #foobar http://link.tld +#foobar http://link.tld +``` \ No newline at end of file -- 2.41.0