]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/markdown/markdown.php
Fix hashtags with markdown escape enabled
[github/shaarli/Shaarli.git] / plugins / markdown / markdown.php
CommitLineData
1be4afac
A
1<?php
2
3/**
4 * Plugin Markdown.
5 *
6 * Shaare's descriptions are parsed with Markdown.
7 */
8
dd6794cf
A
9use Shaarli\Config\ConfigManager;
10
3ce20d9e
A
11/*
12 * If this tag is used on a shaare, the description won't be processed by Parsedown.
3ce20d9e 13 */
8c4e6018 14define('NO_MD_TAG', 'nomarkdown');
3ce20d9e 15
1be4afac
A
16/**
17 * Parse linklist descriptions.
18 *
e0376101
A
19 * @param array $data linklist data.
20 * @param ConfigManager $conf instance.
1be4afac
A
21 *
22 * @return mixed linklist data parsed in markdown (and converted to HTML).
23 */
e0376101 24function hook_markdown_render_linklist($data, $conf)
1be4afac
A
25{
26 foreach ($data['links'] as &$value) {
3ce20d9e 27 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
266e3fe5 28 $value = stripNoMarkdownTag($value);
3ce20d9e
A
29 continue;
30 }
86ceea05
A
31 $value['description'] = process_markdown(
32 $value['description'],
33 $conf->get('security.markdown_escape', true),
34 $conf->get('security.allowed_protocols')
35 );
1be4afac 36 }
1be4afac
A
37 return $data;
38}
39
635d38c2
A
40/**
41 * Parse feed linklist descriptions.
42 *
43 * @param array $data linklist data.
e0376101 44 * @param ConfigManager $conf instance.
635d38c2
A
45 *
46 * @return mixed linklist data parsed in markdown (and converted to HTML).
47 */
e0376101 48function hook_markdown_render_feed($data, $conf)
635d38c2
A
49{
50 foreach ($data['links'] as &$value) {
51 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
266e3fe5 52 $value = stripNoMarkdownTag($value);
635d38c2
A
53 continue;
54 }
dd6794cf 55 $value['description'] = reverse_feed_permalink($value['description']);
86ceea05
A
56 $value['description'] = process_markdown(
57 $value['description'],
58 $conf->get('security.markdown_escape', true),
59 $conf->get('security.allowed_protocols')
60 );
635d38c2
A
61 }
62
63 return $data;
64}
65
1be4afac
A
66/**
67 * Parse daily descriptions.
68 *
e0376101
A
69 * @param array $data daily data.
70 * @param ConfigManager $conf instance.
1be4afac
A
71 *
72 * @return mixed daily data parsed in markdown (and converted to HTML).
73 */
e0376101 74function hook_markdown_render_daily($data, $conf)
1be4afac 75{
50142efd 76 //var_dump($data);die;
1be4afac 77 // Manipulate columns data
50142efd 78 foreach ($data['linksToDisplay'] as &$value) {
79 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
80 $value = stripNoMarkdownTag($value);
81 continue;
1be4afac 82 }
50142efd 83 $value['formatedDescription'] = process_markdown(
84 $value['formatedDescription'],
85 $conf->get('security.markdown_escape', true),
86 $conf->get('security.allowed_protocols')
87 );
1be4afac
A
88 }
89
90 return $data;
91}
92
3ce20d9e
A
93/**
94 * Check if noMarkdown is set in tags.
95 *
96 * @param string $tags tag list
97 *
98 * @return bool true if markdown should be disabled on this link.
99 */
100function noMarkdownTag($tags)
101{
266e3fe5 102 return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags);
3ce20d9e
A
103}
104
8c4e6018
A
105/**
106 * Remove the no-markdown meta tag so it won't be displayed.
107 *
266e3fe5 108 * @param array $link Link data.
8c4e6018 109 *
266e3fe5 110 * @return array Updated link without no markdown tag.
8c4e6018 111 */
266e3fe5 112function stripNoMarkdownTag($link)
8c4e6018 113{
266e3fe5
A
114 if (! empty($link['taglist'])) {
115 $offset = array_search(NO_MD_TAG, $link['taglist']);
116 if ($offset !== false) {
117 unset($link['taglist'][$offset]);
118 }
119 }
120
121 if (!empty($link['tags'])) {
122 str_replace(NO_MD_TAG, '', $link['tags']);
123 }
124
125 return $link;
8c4e6018
A
126}
127
1be4afac
A
128/**
129 * When link list is displayed, include markdown CSS.
130 *
131 * @param array $data includes data.
132 *
133 * @return mixed - includes data with markdown CSS file added.
134 */
135function hook_markdown_render_includes($data)
136{
137 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
138 || $data['_PAGE_'] == Router::$PAGE_DAILY
139 || $data['_PAGE_'] == Router::$PAGE_EDITLINK
140 ) {
50142efd 141
1be4afac
A
142 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
143 }
144
145 return $data;
146}
147
148/**
149 * Hook render_editlink.
150 * Adds an help link to markdown syntax.
151 *
152 * @param array $data data passed to plugin
153 *
154 * @return array altered $data.
155 */
156function hook_markdown_render_editlink($data)
157{
158 // Load help HTML into a string
12266213
A
159 $txt = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
160 $translations = [
161 t('Description will be rendered with'),
162 t('Markdown syntax documentation'),
163 t('Markdown syntax'),
164 ];
165 $data['edit_link_plugin'][] = vsprintf($txt, $translations);
3ce20d9e
A
166 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
167 if (! in_array(NO_MD_TAG, $data['tags'])) {
168 $data['tags'][NO_MD_TAG] = 0;
169 }
170
1be4afac
A
171 return $data;
172}
173
174
175/**
176 * Remove HTML links auto generated by Shaarli core system.
177 * Keeps HREF attributes.
178 *
179 * @param string $description input description text.
180 *
181 * @return string $description without HTML links.
182 */
183function reverse_text2clickable($description)
184{
9ccca401
A
185 $descriptionLines = explode(PHP_EOL, $description);
186 $descriptionOut = '';
187 $codeBlockOn = false;
188 $lineCount = 0;
189
190 foreach ($descriptionLines as $descriptionLine) {
c5941f31
A
191 // Detect line of code: starting with 4 spaces,
192 // except lists which can start with +/*/- or `2.` after spaces.
193 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
9ccca401
A
194 // Detect and toggle block of code
195 if (!$codeBlockOn) {
196 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
197 }
198 elseif (preg_match('/^```/', $descriptionLine) > 0) {
199 $codeBlockOn = false;
200 }
201
202 $hashtagTitle = ' title="Hashtag [^"]+"';
203 // Reverse `inline code` hashtags.
204 $descriptionLine = preg_replace(
205 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
206 '$1$2$3',
207 $descriptionLine
208 );
209
c5941f31
A
210 // Reverse all links in code blocks, only non hashtag elsewhere.
211 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
9ccca401 212 $descriptionLine = preg_replace(
c5941f31 213 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
9ccca401
A
214 '$1',
215 $descriptionLine
216 );
217
cb7940e2
A
218 // Make hashtag links markdown ready, otherwise the links will be ignored with escape set to true
219 if (!$codeBlockOn && !$codeLineOn) {
220 $descriptionLine = preg_replace(
221 '#<a href="([^ ]*)"'. $hashtagTitle .'>([^<]+)</a>#m',
222 '[$2]($1)',
223 $descriptionLine
224 );
225 }
226
9ccca401
A
227 $descriptionOut .= $descriptionLine;
228 if ($lineCount++ < count($descriptionLines) - 1) {
229 $descriptionOut .= PHP_EOL;
230 }
231 }
232 return $descriptionOut;
1be4afac
A
233}
234
235/**
236 * Remove <br> tag to let markdown handle it.
237 *
238 * @param string $description input description text.
239 *
240 * @return string $description without <br> tags.
241 */
242function reverse_nl2br($description)
243{
244 return preg_replace('!<br */?>!im', '', $description);
245}
246
247/**
248 * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
249 *
250 * @param string $description input description text.
251 *
252 * @return string $description without HTML links.
253 */
254function reverse_space2nbsp($description)
255{
256 return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
257}
258
dd6794cf
A
259function reverse_feed_permalink($description)
260{
261 return preg_replace('@&#8212; <a href="([^"]+)" title="[^"]+">(\w+)</a>$@im', '&#8212; [$2]($1)', $description);
262}
263
86ceea05
A
264/**
265 * Replace not whitelisted protocols with http:// in given description.
266 *
267 * @param string $description input description text.
268 * @param array $allowedProtocols list of allowed protocols.
269 *
270 * @return string $description without malicious link.
271 */
272function filter_protocols($description, $allowedProtocols)
273{
274 return preg_replace_callback(
275 '#]\((.*?)\)#is',
276 function ($match) use ($allowedProtocols) {
277 return ']('. whitelist_protocols($match[1], $allowedProtocols) .')';
278 },
279 $description
280 );
281}
282
1be4afac 283/**
2925687e
A
284 * Remove dangerous HTML tags (tags, iframe, etc.).
285 * Doesn't affect <code> content (already escaped by Parsedown).
1be4afac
A
286 *
287 * @param string $description input description text.
288 *
2925687e 289 * @return string given string escaped.
1be4afac 290 */
2925687e 291function sanitize_html($description)
1be4afac 292{
2925687e
A
293 $escapeTags = array(
294 'script',
295 'style',
296 'link',
297 'iframe',
298 'frameset',
299 'frame',
300 );
301 foreach ($escapeTags as $tag) {
302 $description = preg_replace_callback(
303 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
304 function ($match) { return escape($match[0]); },
305 $description);
306 }
307 $description = preg_replace(
b525810c 308 '#(<[^>]+\s)on[a-z]*="?[^ "]*"?#is',
2925687e
A
309 '$1',
310 $description);
311 return $description;
1be4afac
A
312}
313
314/**
315 * Render shaare contents through Markdown parser.
316 * 1. Remove HTML generated by Shaarli core.
2925687e
A
317 * 2. Reverse the escape function.
318 * 3. Generate markdown descriptions.
319 * 4. Sanitize sensible HTML tags for security.
320 * 5. Wrap description in 'markdown' CSS class.
1be4afac
A
321 *
322 * @param string $description input description text.
e0376101 323 * @param bool $escape escape HTML entities
1be4afac
A
324 *
325 * @return string HTML processed $description.
326 */
86ceea05 327function process_markdown($description, $escape = true, $allowedProtocols = [])
1be4afac
A
328{
329 $parsedown = new Parsedown();
330
331 $processedDescription = $description;
1be4afac
A
332 $processedDescription = reverse_nl2br($processedDescription);
333 $processedDescription = reverse_space2nbsp($processedDescription);
9ccca401 334 $processedDescription = reverse_text2clickable($processedDescription);
86ceea05 335 $processedDescription = filter_protocols($processedDescription, $allowedProtocols);
2925687e 336 $processedDescription = unescape($processedDescription);
1be4afac 337 $processedDescription = $parsedown
e0376101 338 ->setMarkupEscaped($escape)
1be4afac
A
339 ->setBreaksEnabled(true)
340 ->text($processedDescription);
2925687e 341 $processedDescription = sanitize_html($processedDescription);
841df2dd 342
343 if(!empty($processedDescription)){
344 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
345 }
1be4afac
A
346
347 return $processedDescription;
348}
12266213
A
349
350/**
351 * This function is never called, but contains translation calls for GNU gettext extraction.
352 */
353function markdown_dummy_translation()
354{
355 // meta
356 t('Render shaare description with Markdown syntax.<br><strong>Warning</strong>:
357If your shaared descriptions contained HTML tags before enabling the markdown plugin,
358enabling it might break your page.
359See the <a href="https://github.com/shaarli/Shaarli/tree/master/plugins/markdown#html-rendering">README</a>.');
360}