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