6 * Shaare's descriptions are parsed with Markdown.
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Plugin\PluginManager
;
14 * If this tag is used on a shaare, the description won't be processed by Parsedown.
16 define('NO_MD_TAG', 'nomarkdown');
19 * Parse linklist descriptions.
21 * @param array $data linklist data.
22 * @param ConfigManager $conf instance.
24 * @return mixed linklist data parsed in markdown (and converted to HTML).
26 function hook_markdown_render_linklist($data, $conf)
28 foreach ($data['links'] as &$value) {
29 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
30 $value = stripNoMarkdownTag($value);
33 $value['description_src'] = $value['description'];
34 $value['description'] = process_markdown(
35 $value['description'],
36 $conf->get('security.markdown_escape', true),
37 $conf->get('security.allowed_protocols')
44 * Parse feed linklist descriptions.
46 * @param array $data linklist data.
47 * @param ConfigManager $conf instance.
49 * @return mixed linklist data parsed in markdown (and converted to HTML).
51 function hook_markdown_render_feed($data, $conf)
53 foreach ($data['links'] as &$value) {
54 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
55 $value = stripNoMarkdownTag($value);
58 $value['description'] = reverse_feed_permalink($value['description']);
59 $value['description'] = process_markdown(
60 $value['description'],
61 $conf->get('security.markdown_escape', true),
62 $conf->get('security.allowed_protocols')
70 * Parse daily descriptions.
72 * @param array $data daily data.
73 * @param ConfigManager $conf instance.
75 * @return mixed daily data parsed in markdown (and converted to HTML).
77 function hook_markdown_render_daily($data, $conf)
79 //var_dump($data);die;
80 // Manipulate columns data
81 foreach ($data['linksToDisplay'] as &$value) {
82 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
83 $value = stripNoMarkdownTag($value);
86 $value['formatedDescription'] = process_markdown(
87 $value['formatedDescription'],
88 $conf->get('security.markdown_escape', true),
89 $conf->get('security.allowed_protocols')
97 * Check if noMarkdown is set in tags.
99 * @param string $tags tag list
101 * @return bool true if markdown should be disabled on this link.
103 function noMarkdownTag($tags)
105 return preg_match('/(^|\s)'. NO_MD_TAG
.'(\s|$)/', $tags);
109 * Remove the no-markdown meta tag so it won't be displayed.
111 * @param array $link Link data.
113 * @return array Updated link without no markdown tag.
115 function stripNoMarkdownTag($link)
117 if (! empty($link['taglist'])) {
118 $offset = array_search(NO_MD_TAG
, $link['taglist']);
119 if ($offset !== false) {
120 unset($link['taglist'][$offset]);
124 if (!empty($link['tags'])) {
125 str_replace(NO_MD_TAG
, '', $link['tags']);
132 * When link list is displayed, include markdown CSS.
134 * @param array $data includes data.
136 * @return mixed - includes data with markdown CSS file added.
138 function hook_markdown_render_includes($data)
140 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
141 || $data['_PAGE_'] == Router
::$PAGE_DAILY
142 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
144 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
151 * Hook render_editlink.
152 * Adds an help link to markdown syntax.
154 * @param array $data data passed to plugin
156 * @return array altered $data.
158 function hook_markdown_render_editlink($data)
160 // Load help HTML into a string
161 $txt = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
163 t('Description will be rendered with'),
164 t('Markdown syntax documentation'),
165 t('Markdown syntax'),
167 $data['edit_link_plugin'][] = vsprintf($txt, $translations);
168 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
169 if (! in_array(NO_MD_TAG
, $data['tags'])) {
170 $data['tags'][NO_MD_TAG
] = 0;
178 * Remove HTML links auto generated by Shaarli core system.
179 * Keeps HREF attributes.
181 * @param string $description input description text.
183 * @return string $description without HTML links.
185 function reverse_text2clickable($description)
187 $descriptionLines = explode(PHP_EOL
, $description);
188 $descriptionOut = '';
189 $codeBlockOn = false;
192 foreach ($descriptionLines as $descriptionLine) {
193 // Detect line of code: starting with 4 spaces,
194 // except lists which can start with +/*/- or `2.` after spaces.
195 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
196 // Detect and toggle block of code
198 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
199 } elseif (preg_match('/^```/', $descriptionLine) > 0) {
200 $codeBlockOn = false;
203 $hashtagTitle = ' title="Hashtag [^"]+"';
204 // Reverse `inline code` hashtags.
205 $descriptionLine = preg_replace(
206 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
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',
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',
228 $descriptionOut .= $descriptionLine;
229 if ($lineCount++
< count($descriptionLines) - 1) {
230 $descriptionOut .= PHP_EOL
;
233 return $descriptionOut;
237 * Remove <br> tag to let markdown handle it.
239 * @param string $description input description text.
241 * @return string $description without <br> tags.
243 function reverse_nl2br($description)
245 return preg_replace('!<br */?>!im', '', $description);
249 * Remove HTML spaces ' ' auto generated by Shaarli core system.
251 * @param string $description input description text.
253 * @return string $description without HTML links.
255 function reverse_space2nbsp($description)
257 return preg_replace('/(^| ) /m', '$1 ', $description);
260 function reverse_feed_permalink($description)
262 return preg_replace('@— <a href="([^"]+)" title="[^"]+">(\w+)</a>$@im', '— [$2]($1)', $description);
266 * Replace not whitelisted protocols with http:// in given description.
268 * @param string $description input description text.
269 * @param array $allowedProtocols list of allowed protocols.
271 * @return string $description without malicious link.
273 function filter_protocols($description, $allowedProtocols)
275 return preg_replace_callback(
277 function ($match) use ($allowedProtocols) {
278 return ']('. whitelist_protocols($match[1], $allowedProtocols) .')';
285 * Remove dangerous HTML tags (tags, iframe, etc.).
286 * Doesn't affect <code> content (already escaped by Parsedown).
288 * @param string $description input description text.
290 * @return string given string escaped.
292 function sanitize_html($description)
302 foreach ($escapeTags as $tag) {
303 $description = preg_replace_callback(
304 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
306 return escape($match[0]);
311 $description = preg_replace(
312 '#(<[^>]+\s)on[a-z]*="?[^ "]*"?#is',
320 * Render shaare contents through Markdown parser.
321 * 1. Remove HTML generated by Shaarli core.
322 * 2. Reverse the escape function.
323 * 3. Generate markdown descriptions.
324 * 4. Sanitize sensible HTML tags for security.
325 * 5. Wrap description in 'markdown
' CSS class.
327 * @param string $description input description text.
328 * @param bool $escape escape HTML entities
330 * @return string HTML processed $description.
332 function process_markdown($description, $escape = true, $allowedProtocols = [])
334 $parsedown = new Parsedown();
336 $processedDescription = $description;
337 $processedDescription = reverse_nl2br($processedDescription);
338 $processedDescription = reverse_space2nbsp($processedDescription);
339 $processedDescription = reverse_text2clickable($processedDescription);
340 $processedDescription = filter_protocols($processedDescription, $allowedProtocols);
341 $processedDescription = unescape($processedDescription);
342 $processedDescription = $parsedown
343 ->setMarkupEscaped($escape)
344 ->setBreaksEnabled(true)
345 ->text($processedDescription);
346 $processedDescription = sanitize_html($processedDescription);
348 if (!empty($processedDescription)) {
349 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
352 return $processedDescription;
356 * This function is never called, but contains translation calls for GNU gettext extraction.
358 function markdown_dummy_translation()
361 t('Render shaare description with Markdown syntax
.<br
><strong
>Warning
</strong
>:
362 If your shaared descriptions contained HTML tags before enabling the markdown plugin
,
363 enabling it might
break your page
.
364 See the
<a href
="https://github.com/shaarli/Shaarli/tree/master/plugins/markdown#html-rendering">README
</a
>.');