aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2016-03-21 18:46:34 +0100
committerArthurHoaro <arthur@hoa.ro>2016-03-21 18:46:34 +0100
commit3ce20d9e84a715c6890988999a8ecac210a42d9c (patch)
tree5a33ac2b0915a4ebdaf362a1e055e1c52bed8f4f
parent890afc32f744859d11b97eb26ed5c030af9b4145 (diff)
downloadShaarli-3ce20d9e84a715c6890988999a8ecac210a42d9c.tar.gz
Shaarli-3ce20d9e84a715c6890988999a8ecac210a42d9c.tar.zst
Shaarli-3ce20d9e84a715c6890988999a8ecac210a42d9c.zip
Markdown: Add the 'meta-tag' `.nomarkdown` which prevent a shaare from being parsed with markdown
Also add the tag in tag list in edit_link, so it will appear on autocompletion.
-rw-r--r--plugins/markdown/markdown.php30
-rw-r--r--tests/plugins/PluginMarkdownTest.php37
2 files changed, 66 insertions, 1 deletions
diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php
index 5a702c7b..544ed22e 100644
--- a/plugins/markdown/markdown.php
+++ b/plugins/markdown/markdown.php
@@ -8,6 +8,12 @@
8 8
9require_once 'Parsedown.php'; 9require_once 'Parsedown.php';
10 10
11/*
12 * If this tag is used on a shaare, the description won't be processed by Parsedown.
13 * Using a private tag so it won't appear for visitors.
14 */
15define('NO_MD_TAG', '.nomarkdown');
16
11/** 17/**
12 * Parse linklist descriptions. 18 * Parse linklist descriptions.
13 * 19 *
@@ -18,6 +24,9 @@ require_once 'Parsedown.php';
18function hook_markdown_render_linklist($data) 24function hook_markdown_render_linklist($data)
19{ 25{
20 foreach ($data['links'] as &$value) { 26 foreach ($data['links'] as &$value) {
27 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
28 continue;
29 }
21 $value['description'] = process_markdown($value['description']); 30 $value['description'] = process_markdown($value['description']);
22 } 31 }
23 32
@@ -36,6 +45,9 @@ function hook_markdown_render_daily($data)
36 // Manipulate columns data 45 // Manipulate columns data
37 foreach ($data['cols'] as &$value) { 46 foreach ($data['cols'] as &$value) {
38 foreach ($value as &$value2) { 47 foreach ($value as &$value2) {
48 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
49 continue;
50 }
39 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); 51 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
40 } 52 }
41 } 53 }
@@ -44,6 +56,18 @@ function hook_markdown_render_daily($data)
44} 56}
45 57
46/** 58/**
59 * Check if noMarkdown is set in tags.
60 *
61 * @param string $tags tag list
62 *
63 * @return bool true if markdown should be disabled on this link.
64 */
65function noMarkdownTag($tags)
66{
67 return strpos($tags, NO_MD_TAG) !== false;
68}
69
70/**
47 * When link list is displayed, include markdown CSS. 71 * When link list is displayed, include markdown CSS.
48 * 72 *
49 * @param array $data includes data. 73 * @param array $data includes data.
@@ -75,6 +99,12 @@ function hook_markdown_render_editlink($data)
75{ 99{
76 // Load help HTML into a string 100 // Load help HTML into a string
77 $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html'); 101 $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
102
103 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
104 if (! in_array(NO_MD_TAG, $data['tags'])) {
105 $data['tags'][NO_MD_TAG] = 0;
106 }
107
78 return $data; 108 return $data;
79} 109}
80 110
diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php
index 8e1a128a..fa7e1d52 100644
--- a/tests/plugins/PluginMarkdownTest.php
+++ b/tests/plugins/PluginMarkdownTest.php
@@ -102,7 +102,8 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase
102 /** 102 /**
103 * Test sanitize_html(). 103 * Test sanitize_html().
104 */ 104 */
105 function testSanitizeHtml() { 105 function testSanitizeHtml()
106 {
106 $input = '< script src="js.js"/>'; 107 $input = '< script src="js.js"/>';
107 $input .= '< script attr>alert(\'xss\');</script>'; 108 $input .= '< script attr>alert(\'xss\');</script>';
108 $input .= '<style> * { display: none }</style>'; 109 $input .= '<style> * { display: none }</style>';
@@ -114,4 +115,38 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase
114 $input = escape($input); 115 $input = escape($input);
115 $this->assertEquals($input, sanitize_html($input)); 116 $this->assertEquals($input, sanitize_html($input));
116 } 117 }
118
119 /**
120 * Test the no markdown tag.
121 */
122 function testNoMarkdownTag()
123 {
124 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
125 $data = array(
126 'links' => array(array(
127 'description' => $str,
128 'tags' => NO_MD_TAG
129 ))
130 );
131
132 $data = hook_markdown_render_linklist($data);
133 $this->assertEquals($str, $data['links'][0]['description']);
134
135 $data = array(
136 // Columns data
137 'cols' => array(
138 // First, second, third.
139 0 => array(
140 // nth link
141 0 => array(
142 'formatedDescription' => $str,
143 'tags' => NO_MD_TAG
144 ),
145 ),
146 ),
147 );
148
149 $data = hook_markdown_render_daily($data);
150 $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']);
151 }
117} 152}