6 * Shaare's descriptions are parsed with Markdown.
10 * If this tag is used on a shaare, the description won't be processed by Parsedown.
12 define('NO_MD_TAG', 'nomarkdown');
15 * Parse linklist descriptions.
17 * @param array $data linklist data.
18 * @param ConfigManager $conf instance.
20 * @return mixed linklist data parsed in markdown (and converted to HTML).
22 function hook_markdown_render_linklist($data, $conf)
24 foreach ($data['links'] as &$value) {
25 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
26 $value = stripNoMarkdownTag($value);
29 $value['description'] = process_markdown(
30 $value['description'],
31 $conf->get('security.markdown_escape', true),
32 $conf->get('security.allowed_protocols')
39 * Parse feed linklist descriptions.
41 * @param array $data linklist data.
42 * @param ConfigManager $conf instance.
44 * @return mixed linklist data parsed in markdown (and converted to HTML).
46 function hook_markdown_render_feed($data, $conf)
48 foreach ($data['links'] as &$value) {
49 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
50 $value = stripNoMarkdownTag($value);
53 $value['description'] = process_markdown(
54 $value['description'],
55 $conf->get('security.markdown_escape', true),
56 $conf->get('security.allowed_protocols')
64 * Parse daily descriptions.
66 * @param array $data daily data.
67 * @param ConfigManager $conf instance.
69 * @return mixed daily data parsed in markdown (and converted to HTML).
71 function hook_markdown_render_daily($data, $conf)
73 //var_dump($data);die;
74 // Manipulate columns data
75 foreach ($data['linksToDisplay'] as &$value) {
76 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
77 $value = stripNoMarkdownTag($value);
80 $value['formatedDescription'] = process_markdown(
81 $value['formatedDescription'],
82 $conf->get('security.markdown_escape', true),
83 $conf->get('security.allowed_protocols')
91 * Check if noMarkdown is set in tags.
93 * @param string $tags tag list
95 * @return bool true if markdown should be disabled on this link.
97 function noMarkdownTag($tags)
99 return preg_match('/(^|\s)'. NO_MD_TAG
.'(\s|$)/', $tags);
103 * Remove the no-markdown meta tag so it won't be displayed.
105 * @param array $link Link data.
107 * @return array Updated link without no markdown tag.
109 function stripNoMarkdownTag($link)
111 if (! empty($link['taglist'])) {
112 $offset = array_search(NO_MD_TAG
, $link['taglist']);
113 if ($offset !== false) {
114 unset($link['taglist'][$offset]);
118 if (!empty($link['tags'])) {
119 str_replace(NO_MD_TAG
, '', $link['tags']);
126 * When link list is displayed, include markdown CSS.
128 * @param array $data includes data.
130 * @return mixed - includes data with markdown CSS file added.
132 function hook_markdown_render_includes($data)
134 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
135 || $data['_PAGE_'] == Router
::$PAGE_DAILY
136 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
139 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
146 * Hook render_editlink.
147 * Adds an help link to markdown syntax.
149 * @param array $data data passed to plugin
151 * @return array altered $data.
153 function hook_markdown_render_editlink($data)
155 // Load help HTML into a string
156 $txt = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
158 t('Description will be rendered with'),
159 t('Markdown syntax documentation'),
160 t('Markdown syntax'),
162 $data['edit_link_plugin'][] = vsprintf($txt, $translations);
163 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
164 if (! in_array(NO_MD_TAG
, $data['tags'])) {
165 $data['tags'][NO_MD_TAG
] = 0;
173 * Remove HTML links auto generated by Shaarli core system.
174 * Keeps HREF attributes.
176 * @param string $description input description text.
178 * @return string $description without HTML links.
180 function reverse_text2clickable($description)
182 $descriptionLines = explode(PHP_EOL
, $description);
183 $descriptionOut = '';
184 $codeBlockOn = false;
187 foreach ($descriptionLines as $descriptionLine) {
188 // Detect line of code: starting with 4 spaces,
189 // except lists which can start with +/*/- or `2.` after spaces.
190 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
191 // Detect and toggle block of code
193 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
195 elseif (preg_match('/^```/', $descriptionLine) > 0) {
196 $codeBlockOn = false;
199 $hashtagTitle = ' title="Hashtag [^"]+"';
200 // Reverse `inline code` hashtags.
201 $descriptionLine = preg_replace(
202 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
207 // Reverse all links in code blocks, only non hashtag elsewhere.
208 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
209 $descriptionLine = preg_replace(
210 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
215 $descriptionOut .= $descriptionLine;
216 if ($lineCount++ < count($descriptionLines) - 1) {
217 $descriptionOut .= PHP_EOL;
220 return $descriptionOut;
224 * Remove <br> tag to let markdown handle it.
226 * @param string $description input description text.
228 * @return string $description without <br> tags.
230 function reverse_nl2br($description)
232 return preg_replace('!<br */
?>!im
', '', $description);
236 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
238 * @param string $description input description text.
240 * @return string $description without HTML links.
242 function reverse_space2nbsp($description)
244 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
248 * Replace not whitelisted protocols with http:// in given description.
250 * @param string $description input description text.
251 * @param array $allowedProtocols list of allowed protocols.
253 * @return string $description without malicious link.
255 function filter_protocols($description, $allowedProtocols)
257 return preg_replace_callback(
259 function ($match) use ($allowedProtocols) {
260 return ']('. whitelist_protocols($match[1], $allowedProtocols) .')';
267 * Remove dangerous HTML tags (tags, iframe, etc.).
268 * Doesn't affect <code> content (already escaped by Parsedown).
270 * @param string $description input description text.
272 * @return string given string escaped.
274 function sanitize_html($description)
284 foreach ($escapeTags as $tag) {
285 $description = preg_replace_callback(
286 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
287 function ($match) { return escape($match
[0]); },
290 $description = preg_replace(
291 '#(<[^>]+)on[a-z]*="?[^ "]*"?#is',
298 * Render shaare contents through Markdown parser.
299 * 1. Remove HTML generated by Shaarli core.
300 * 2. Reverse the escape function.
301 * 3. Generate markdown descriptions.
302 * 4. Sanitize sensible HTML tags for security.
303 * 5. Wrap description in 'markdown
' CSS class.
305 * @param string $description input description text.
306 * @param bool $escape escape HTML entities
308 * @return string HTML processed $description.
310 function process_markdown($description, $escape = true, $allowedProtocols = [])
312 $parsedown = new Parsedown();
314 $processedDescription = $description;
315 $processedDescription = reverse_nl2br($processedDescription);
316 $processedDescription = reverse_space2nbsp($processedDescription);
317 $processedDescription = reverse_text2clickable($processedDescription);
318 $processedDescription = filter_protocols($processedDescription, $allowedProtocols);
319 $processedDescription = unescape($processedDescription);
320 $processedDescription = $parsedown
321 ->setMarkupEscaped($escape)
322 ->setBreaksEnabled(true)
323 ->text($processedDescription);
324 $processedDescription = sanitize_html($processedDescription);
326 if(!empty($processedDescription)){
327 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
330 return $processedDescription;
334 * This function is never called, but contains translation calls for GNU gettext extraction.
336 function markdown_dummy_translation()
339 t('Render shaare description with Markdown syntax
.<br
><strong
>Warning
</strong
>:
340 If your shaared descriptions contained HTML tags before enabling the markdown plugin
,
341 enabling it might
break your page
.
342 See the
<a href
="https://github.com/shaarli/Shaarli/tree/master/plugins/markdown#html-rendering">README
</a
>.');