From: ArthurHoaro Date: Sun, 13 Nov 2016 15:51:21 +0000 (+0100) Subject: Markdown: fixes feed rendering with nomarkdown tag X-Git-Tag: v0.8.1~8^2 X-Git-Url: https://git.immae.eu/?p=github%2Fshaarli%2FShaarli.git;a=commitdiff_plain;h=266e3fe5c8961aaf089bad16b9e4c54de1aaff40 Markdown: fixes feed rendering with nomarkdown tag * make sure we match exactly `nomarkdown` tag * pass the whole link data to stripNoMarkdownTag() to: * strip the noMD tag in taglist (array) * strip the tag in tags (string) Fixes #689 tmp --- diff --git a/plugins/markdown/README.md b/plugins/markdown/README.md index 4f021871..196005e7 100644 --- a/plugins/markdown/README.md +++ b/plugins/markdown/README.md @@ -20,26 +20,35 @@ The directory structure should look like: |--- markdown.css |--- markdown.meta |--- markdown.php - |--- Parsedown.php |--- README.md ``` To enable the plugin, just check it in the plugin administration page. -You can also add `markdown` to your list of enabled plugins in `data/config.php` -(`ENABLED_PLUGINS` array). +You can also add `markdown` to your list of enabled plugins in `data/config.json.php` +(`general.enabled_plugins` list). This should look like: ``` -$GLOBALS['config']['ENABLED_PLUGINS'] = array('qrcode', 'any_other_plugin', 'markdown') +"general": { + "enabled_plugins": [ + "markdown", + [...] + ], +} ``` +Parsedown parsing library is imported using Composer. If you installed Shaarli using `git`, +or the `master` branch, run + + composer update --no-dev --prefer-dist + ### No Markdown tag -If the tag `.nomarkdown` is set for a shaare, it won't be converted to Markdown syntax. +If the tag `nomarkdown` is set for a shaare, it won't be converted to Markdown syntax. -> Note: it's a private tag (leading dot), so it won't be displayed to visitors. +> Note: this is a special tag, so it won't be displayed in link list. ### Known issue diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php index a764b6fa..0cf6e6e2 100644 --- a/plugins/markdown/markdown.php +++ b/plugins/markdown/markdown.php @@ -22,7 +22,7 @@ function hook_markdown_render_linklist($data) { foreach ($data['links'] as &$value) { if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { - $value['taglist'] = stripNoMarkdownTag($value['taglist']); + $value = stripNoMarkdownTag($value); continue; } $value['description'] = process_markdown($value['description']); @@ -41,7 +41,7 @@ function hook_markdown_render_feed($data) { foreach ($data['links'] as &$value) { if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { - $value['tags'] = stripNoMarkdownTag($value['tags']); + $value = stripNoMarkdownTag($value); continue; } $value['description'] = process_markdown($value['description']); @@ -63,6 +63,7 @@ function hook_markdown_render_daily($data) foreach ($data['cols'] as &$value) { foreach ($value as &$value2) { if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) { + $value2 = stripNoMarkdownTag($value2); continue; } $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); @@ -81,20 +82,30 @@ function hook_markdown_render_daily($data) */ function noMarkdownTag($tags) { - return strpos($tags, NO_MD_TAG) !== false; + return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags); } /** * Remove the no-markdown meta tag so it won't be displayed. * - * @param string $tags Tag list. + * @param array $link Link data. * - * @return string tag list without no markdown tag. + * @return array Updated link without no markdown tag. */ -function stripNoMarkdownTag($tags) +function stripNoMarkdownTag($link) { - unset($tags[array_search(NO_MD_TAG, $tags)]); - return array_values($tags); + if (! empty($link['taglist'])) { + $offset = array_search(NO_MD_TAG, $link['taglist']); + if ($offset !== false) { + unset($link['taglist'][$offset]); + } + } + + if (!empty($link['tags'])) { + str_replace(NO_MD_TAG, '', $link['tags']); + } + + return $link; } /** diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php index 12bdda24..17ef2280 100644 --- a/tests/plugins/PluginMarkdownTest.php +++ b/tests/plugins/PluginMarkdownTest.php @@ -8,8 +8,8 @@ require_once 'application/Utils.php'; require_once 'plugins/markdown/markdown.php'; /** - * Class PlugQrcodeTest - * Unit test for the QR-Code plugin + * Class PluginMarkdownTest + * Unit test for the Markdown plugin */ class PluginMarkdownTest extends PHPUnit_Framework_TestCase { @@ -130,8 +130,11 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase )) ); - $data = hook_markdown_render_linklist($data); - $this->assertEquals($str, $data['links'][0]['description']); + $processed = hook_markdown_render_linklist($data); + $this->assertEquals($str, $processed['links'][0]['description']); + + $processed = hook_markdown_render_feed($data); + $this->assertEquals($str, $processed['links'][0]['description']); $data = array( // Columns data @@ -152,6 +155,24 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); } + /** + * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). + */ + function testNoMarkdownNotExcactlyMatching() + { + $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; + $data = array( + 'links' => array(array( + 'description' => $str, + 'tags' => '.' . NO_MD_TAG, + 'taglist' => array('.'. NO_MD_TAG), + )) + ); + + $data = hook_markdown_render_feed($data); + $this->assertContains('', $data['links'][0]['description']); + } + /** * Test hashtag links processed with markdown. */