]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/markdown/markdown.php
namespacing: \Shaarli\Router
[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 9use Shaarli\Config\ConfigManager;
a932f486 10use Shaarli\Router;
dd6794cf 11
3ce20d9e
A
12/*
13 * If this tag is used on a shaare, the description won't be processed by Parsedown.
3ce20d9e 14 */
8c4e6018 15define('NO_MD_TAG', 'nomarkdown');
3ce20d9e 16
1be4afac
A
17/**
18 * Parse linklist descriptions.
19 *
e0376101
A
20 * @param array $data linklist data.
21 * @param ConfigManager $conf instance.
1be4afac
A
22 *
23 * @return mixed linklist data parsed in markdown (and converted to HTML).
24 */
e0376101 25function hook_markdown_render_linklist($data, $conf)
1be4afac
A
26{
27 foreach ($data['links'] as &$value) {
3ce20d9e 28 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
266e3fe5 29 $value = stripNoMarkdownTag($value);
3ce20d9e
A
30 continue;
31 }
a120fb29 32 $value['description_src'] = $value['description'];
86ceea05
A
33 $value['description'] = process_markdown(
34 $value['description'],
35 $conf->get('security.markdown_escape', true),
36 $conf->get('security.allowed_protocols')
37 );
1be4afac 38 }
1be4afac
A
39 return $data;
40}
41
635d38c2
A
42/**
43 * Parse feed linklist descriptions.
44 *
45 * @param array $data linklist data.
e0376101 46 * @param ConfigManager $conf instance.
635d38c2
A
47 *
48 * @return mixed linklist data parsed in markdown (and converted to HTML).
49 */
e0376101 50function hook_markdown_render_feed($data, $conf)
635d38c2
A
51{
52 foreach ($data['links'] as &$value) {
53 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
266e3fe5 54 $value = stripNoMarkdownTag($value);
635d38c2
A
55 continue;
56 }
dd6794cf 57 $value['description'] = reverse_feed_permalink($value['description']);
86ceea05
A
58 $value['description'] = process_markdown(
59 $value['description'],
60 $conf->get('security.markdown_escape', true),
61 $conf->get('security.allowed_protocols')
62 );
635d38c2
A
63 }
64
65 return $data;
66}
67
1be4afac
A
68/**
69 * Parse daily descriptions.
70 *
e0376101
A
71 * @param array $data daily data.
72 * @param ConfigManager $conf instance.
1be4afac
A
73 *
74 * @return mixed daily data parsed in markdown (and converted to HTML).
75 */
e0376101 76function hook_markdown_render_daily($data, $conf)
1be4afac 77{
50142efd 78 //var_dump($data);die;
1be4afac 79 // Manipulate columns data
50142efd 80 foreach ($data['linksToDisplay'] as &$value) {
81 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
82 $value = stripNoMarkdownTag($value);
83 continue;
1be4afac 84 }
50142efd 85 $value['formatedDescription'] = process_markdown(
86 $value['formatedDescription'],
87 $conf->get('security.markdown_escape', true),
88 $conf->get('security.allowed_protocols')
89 );
1be4afac
A
90 }
91
92 return $data;
93}
94
3ce20d9e
A
95/**
96 * Check if noMarkdown is set in tags.
97 *
98 * @param string $tags tag list
99 *
100 * @return bool true if markdown should be disabled on this link.
101 */
102function noMarkdownTag($tags)
103{
266e3fe5 104 return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags);
3ce20d9e
A
105}
106
8c4e6018
A
107/**
108 * Remove the no-markdown meta tag so it won't be displayed.
109 *
266e3fe5 110 * @param array $link Link data.
8c4e6018 111 *
266e3fe5 112 * @return array Updated link without no markdown tag.
8c4e6018 113 */
266e3fe5 114function stripNoMarkdownTag($link)
8c4e6018 115{
266e3fe5
A
116 if (! empty($link['taglist'])) {
117 $offset = array_search(NO_MD_TAG, $link['taglist']);
118 if ($offset !== false) {
119 unset($link['taglist'][$offset]);
120 }
121 }
122
123 if (!empty($link['tags'])) {
124 str_replace(NO_MD_TAG, '', $link['tags']);
125 }
126
127 return $link;
8c4e6018
A
128}
129
1be4afac
A
130/**
131 * When link list is displayed, include markdown CSS.
132 *
133 * @param array $data includes data.
134 *
135 * @return mixed - includes data with markdown CSS file added.
136 */
137function hook_markdown_render_includes($data)
138{
139 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
140 || $data['_PAGE_'] == Router::$PAGE_DAILY
141 || $data['_PAGE_'] == Router::$PAGE_EDITLINK
142 ) {
1be4afac
A
143 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
144 }
145
146 return $data;
147}
148
149/**
150 * Hook render_editlink.
151 * Adds an help link to markdown syntax.
152 *
153 * @param array $data data passed to plugin
154 *
155 * @return array altered $data.
156 */
157function hook_markdown_render_editlink($data)
158{
159 // Load help HTML into a string
12266213
A
160 $txt = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
161 $translations = [
162 t('Description will be rendered with'),
163 t('Markdown syntax documentation'),
164 t('Markdown syntax'),
165 ];
166 $data['edit_link_plugin'][] = vsprintf($txt, $translations);
3ce20d9e
A
167 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
168 if (! in_array(NO_MD_TAG, $data['tags'])) {
169 $data['tags'][NO_MD_TAG] = 0;
170 }
171
1be4afac
A
172 return $data;
173}
174
175
176/**
177 * Remove HTML links auto generated by Shaarli core system.
178 * Keeps HREF attributes.
179 *
180 * @param string $description input description text.
181 *
182 * @return string $description without HTML links.
183 */
184function reverse_text2clickable($description)
185{
9ccca401
A
186 $descriptionLines = explode(PHP_EOL, $description);
187 $descriptionOut = '';
188 $codeBlockOn = false;
189 $lineCount = 0;
190
191 foreach ($descriptionLines as $descriptionLine) {
c5941f31
A
192 // Detect line of code: starting with 4 spaces,
193 // except lists which can start with +/*/- or `2.` after spaces.
194 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
9ccca401
A
195 // Detect and toggle block of code
196 if (!$codeBlockOn) {
197 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
a0ab3c3f 198 } elseif (preg_match('/^```/', $descriptionLine) > 0) {
9ccca401
A
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',
a0ab3c3f
V
304 function ($match) {
305 return escape($match[0]);
306 },
307 $description
308 );
2925687e
A
309 }
310 $description = preg_replace(
b525810c 311 '#(<[^>]+\s)on[a-z]*="?[^ "]*"?#is',
2925687e 312 '$1',
a0ab3c3f
V
313 $description
314 );
2925687e 315 return $description;
1be4afac
A
316}
317
318/**
319 * Render shaare contents through Markdown parser.
320 * 1. Remove HTML generated by Shaarli core.
2925687e
A
321 * 2. Reverse the escape function.
322 * 3. Generate markdown descriptions.
323 * 4. Sanitize sensible HTML tags for security.
324 * 5. Wrap description in 'markdown' CSS class.
1be4afac
A
325 *
326 * @param string $description input description text.
e0376101 327 * @param bool $escape escape HTML entities
1be4afac
A
328 *
329 * @return string HTML processed $description.
330 */
86ceea05 331function process_markdown($description, $escape = true, $allowedProtocols = [])
1be4afac
A
332{
333 $parsedown = new Parsedown();
334
335 $processedDescription = $description;
1be4afac
A
336 $processedDescription = reverse_nl2br($processedDescription);
337 $processedDescription = reverse_space2nbsp($processedDescription);
9ccca401 338 $processedDescription = reverse_text2clickable($processedDescription);
86ceea05 339 $processedDescription = filter_protocols($processedDescription, $allowedProtocols);
2925687e 340 $processedDescription = unescape($processedDescription);
1be4afac 341 $processedDescription = $parsedown
e0376101 342 ->setMarkupEscaped($escape)
1be4afac
A
343 ->setBreaksEnabled(true)
344 ->text($processedDescription);
2925687e 345 $processedDescription = sanitize_html($processedDescription);
841df2dd 346
a0ab3c3f 347 if (!empty($processedDescription)) {
841df2dd 348 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
349 }
1be4afac
A
350
351 return $processedDescription;
352}
12266213
A
353
354/**
355 * This function is never called, but contains translation calls for GNU gettext extraction.
356 */
357function markdown_dummy_translation()
358{
359 // meta
360 t('Render shaare description with Markdown syntax.<br><strong>Warning</strong>:
361If your shaared descriptions contained HTML tags before enabling the markdown plugin,
362enabling it might break your page.
363See the <a href="https://github.com/shaarli/Shaarli/tree/master/plugins/markdown#html-rendering">README</a>.');
364}