diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-11-13 16:51:21 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-11-22 10:26:03 +0100 |
commit | 266e3fe5c8961aaf089bad16b9e4c54de1aaff40 (patch) | |
tree | de524c1b2478670cf3a7c5563ef076f4ef0c72de | |
parent | f5f6a4b7e21f39c916fda11eebb1c8df17e1ad4a (diff) | |
download | Shaarli-266e3fe5c8961aaf089bad16b9e4c54de1aaff40.tar.gz Shaarli-266e3fe5c8961aaf089bad16b9e4c54de1aaff40.tar.zst Shaarli-266e3fe5c8961aaf089bad16b9e4c54de1aaff40.zip |
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
-rw-r--r-- | plugins/markdown/README.md | 21 | ||||
-rw-r--r-- | plugins/markdown/markdown.php | 27 | ||||
-rw-r--r-- | tests/plugins/PluginMarkdownTest.php | 29 |
3 files changed, 59 insertions, 18 deletions
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: | |||
20 | |--- markdown.css | 20 | |--- markdown.css |
21 | |--- markdown.meta | 21 | |--- markdown.meta |
22 | |--- markdown.php | 22 | |--- markdown.php |
23 | |--- Parsedown.php | ||
24 | |--- README.md | 23 | |--- README.md |
25 | ``` | 24 | ``` |
26 | 25 | ||
27 | To enable the plugin, just check it in the plugin administration page. | 26 | To enable the plugin, just check it in the plugin administration page. |
28 | 27 | ||
29 | You can also add `markdown` to your list of enabled plugins in `data/config.php` | 28 | You can also add `markdown` to your list of enabled plugins in `data/config.json.php` |
30 | (`ENABLED_PLUGINS` array). | 29 | (`general.enabled_plugins` list). |
31 | 30 | ||
32 | This should look like: | 31 | This should look like: |
33 | 32 | ||
34 | ``` | 33 | ``` |
35 | $GLOBALS['config']['ENABLED_PLUGINS'] = array('qrcode', 'any_other_plugin', 'markdown') | 34 | "general": { |
35 | "enabled_plugins": [ | ||
36 | "markdown", | ||
37 | [...] | ||
38 | ], | ||
39 | } | ||
36 | ``` | 40 | ``` |
37 | 41 | ||
42 | Parsedown parsing library is imported using Composer. If you installed Shaarli using `git`, | ||
43 | or the `master` branch, run | ||
44 | |||
45 | composer update --no-dev --prefer-dist | ||
46 | |||
38 | ### No Markdown tag | 47 | ### No Markdown tag |
39 | 48 | ||
40 | If the tag `.nomarkdown` is set for a shaare, it won't be converted to Markdown syntax. | 49 | If the tag `nomarkdown` is set for a shaare, it won't be converted to Markdown syntax. |
41 | 50 | ||
42 | > Note: it's a private tag (leading dot), so it won't be displayed to visitors. | 51 | > Note: this is a special tag, so it won't be displayed in link list. |
43 | 52 | ||
44 | ### Known issue | 53 | ### Known issue |
45 | 54 | ||
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) | |||
22 | { | 22 | { |
23 | foreach ($data['links'] as &$value) { | 23 | foreach ($data['links'] as &$value) { |
24 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { | 24 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { |
25 | $value['taglist'] = stripNoMarkdownTag($value['taglist']); | 25 | $value = stripNoMarkdownTag($value); |
26 | continue; | 26 | continue; |
27 | } | 27 | } |
28 | $value['description'] = process_markdown($value['description']); | 28 | $value['description'] = process_markdown($value['description']); |
@@ -41,7 +41,7 @@ function hook_markdown_render_feed($data) | |||
41 | { | 41 | { |
42 | foreach ($data['links'] as &$value) { | 42 | foreach ($data['links'] as &$value) { |
43 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { | 43 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { |
44 | $value['tags'] = stripNoMarkdownTag($value['tags']); | 44 | $value = stripNoMarkdownTag($value); |
45 | continue; | 45 | continue; |
46 | } | 46 | } |
47 | $value['description'] = process_markdown($value['description']); | 47 | $value['description'] = process_markdown($value['description']); |
@@ -63,6 +63,7 @@ function hook_markdown_render_daily($data) | |||
63 | foreach ($data['cols'] as &$value) { | 63 | foreach ($data['cols'] as &$value) { |
64 | foreach ($value as &$value2) { | 64 | foreach ($value as &$value2) { |
65 | if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) { | 65 | if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) { |
66 | $value2 = stripNoMarkdownTag($value2); | ||
66 | continue; | 67 | continue; |
67 | } | 68 | } |
68 | $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); | 69 | $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); |
@@ -81,20 +82,30 @@ function hook_markdown_render_daily($data) | |||
81 | */ | 82 | */ |
82 | function noMarkdownTag($tags) | 83 | function noMarkdownTag($tags) |
83 | { | 84 | { |
84 | return strpos($tags, NO_MD_TAG) !== false; | 85 | return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags); |
85 | } | 86 | } |
86 | 87 | ||
87 | /** | 88 | /** |
88 | * Remove the no-markdown meta tag so it won't be displayed. | 89 | * Remove the no-markdown meta tag so it won't be displayed. |
89 | * | 90 | * |
90 | * @param string $tags Tag list. | 91 | * @param array $link Link data. |
91 | * | 92 | * |
92 | * @return string tag list without no markdown tag. | 93 | * @return array Updated link without no markdown tag. |
93 | */ | 94 | */ |
94 | function stripNoMarkdownTag($tags) | 95 | function stripNoMarkdownTag($link) |
95 | { | 96 | { |
96 | unset($tags[array_search(NO_MD_TAG, $tags)]); | 97 | if (! empty($link['taglist'])) { |
97 | return array_values($tags); | 98 | $offset = array_search(NO_MD_TAG, $link['taglist']); |
99 | if ($offset !== false) { | ||
100 | unset($link['taglist'][$offset]); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | if (!empty($link['tags'])) { | ||
105 | str_replace(NO_MD_TAG, '', $link['tags']); | ||
106 | } | ||
107 | |||
108 | return $link; | ||
98 | } | 109 | } |
99 | 110 | ||
100 | /** | 111 | /** |
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'; | |||
8 | require_once 'plugins/markdown/markdown.php'; | 8 | require_once 'plugins/markdown/markdown.php'; |
9 | 9 | ||
10 | /** | 10 | /** |
11 | * Class PlugQrcodeTest | 11 | * Class PluginMarkdownTest |
12 | * Unit test for the QR-Code plugin | 12 | * Unit test for the Markdown plugin |
13 | */ | 13 | */ |
14 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase | 14 | class PluginMarkdownTest extends PHPUnit_Framework_TestCase |
15 | { | 15 | { |
@@ -130,8 +130,11 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |||
130 | )) | 130 | )) |
131 | ); | 131 | ); |
132 | 132 | ||
133 | $data = hook_markdown_render_linklist($data); | 133 | $processed = hook_markdown_render_linklist($data); |
134 | $this->assertEquals($str, $data['links'][0]['description']); | 134 | $this->assertEquals($str, $processed['links'][0]['description']); |
135 | |||
136 | $processed = hook_markdown_render_feed($data); | ||
137 | $this->assertEquals($str, $processed['links'][0]['description']); | ||
135 | 138 | ||
136 | $data = array( | 139 | $data = array( |
137 | // Columns data | 140 | // Columns data |
@@ -153,6 +156,24 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase | |||
153 | } | 156 | } |
154 | 157 | ||
155 | /** | 158 | /** |
159 | * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). | ||
160 | */ | ||
161 | function testNoMarkdownNotExcactlyMatching() | ||
162 | { | ||
163 | $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; | ||
164 | $data = array( | ||
165 | 'links' => array(array( | ||
166 | 'description' => $str, | ||
167 | 'tags' => '.' . NO_MD_TAG, | ||
168 | 'taglist' => array('.'. NO_MD_TAG), | ||
169 | )) | ||
170 | ); | ||
171 | |||
172 | $data = hook_markdown_render_feed($data); | ||
173 | $this->assertContains('<em>', $data['links'][0]['description']); | ||
174 | } | ||
175 | |||
176 | /** | ||
156 | * Test hashtag links processed with markdown. | 177 | * Test hashtag links processed with markdown. |
157 | */ | 178 | */ |
158 | function testMarkdownHashtagLinks() | 179 | function testMarkdownHashtagLinks() |