6 * Shaare's descriptions are parsed with Markdown.
9 require_once 'Parsedown.php';
12 * If this tag is used on a shaare, the description won't be processed by Parsedown.
13 * Using a private tag so it won't appear for visitors.
15 define('NO_MD_TAG', '.nomarkdown');
18 * Parse linklist descriptions.
20 * @param array $data linklist data.
22 * @return mixed linklist data parsed in markdown (and converted to HTML).
24 function hook_markdown_render_linklist($data)
26 foreach ($data['links'] as &$value) {
27 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
30 $value['description'] = process_markdown($value['description']);
37 * Parse daily descriptions.
39 * @param array $data daily data.
41 * @return mixed daily data parsed in markdown (and converted to HTML).
43 function hook_markdown_render_daily($data)
45 // Manipulate columns data
46 foreach ($data['cols'] as &$value) {
47 foreach ($value as &$value2) {
48 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
51 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
59 * Check if noMarkdown is set in tags.
61 * @param string $tags tag list
63 * @return bool true if markdown should be disabled on this link.
65 function noMarkdownTag($tags)
67 return strpos($tags, NO_MD_TAG
) !== false;
71 * When link list is displayed, include markdown CSS.
73 * @param array $data includes data.
75 * @return mixed - includes data with markdown CSS file added.
77 function hook_markdown_render_includes($data)
79 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
80 || $data['_PAGE_'] == Router
::$PAGE_DAILY
81 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
84 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
91 * Hook render_editlink.
92 * Adds an help link to markdown syntax.
94 * @param array $data data passed to plugin
96 * @return array altered $data.
98 function hook_markdown_render_editlink($data)
100 // Load help HTML into a string
101 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
103 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
104 if (! in_array(NO_MD_TAG
, $data['tags'])) {
105 $data['tags'][NO_MD_TAG
] = 0;
113 * Remove HTML links auto generated by Shaarli core system.
114 * Keeps HREF attributes.
116 * @param string $description input description text.
118 * @return string $description without HTML links.
120 function reverse_text2clickable($description)
122 return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description);
126 * Remove <br> tag to let markdown handle it.
128 * @param string $description input description text.
130 * @return string $description without <br> tags.
132 function reverse_nl2br($description)
134 return preg_replace('!<br */
?>!im
', '', $description);
138 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
140 * @param string $description input description text.
142 * @return string $description without HTML links.
144 function reverse_space2nbsp($description)
146 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
150 * Remove dangerous HTML tags (tags, iframe, etc.).
151 * Doesn't affect
<code
> content (already escaped by Parsedown
).
153 * @param
string $description input description text
.
155 * @return string given
string escaped
.
157 function sanitize_html($description)
167 foreach ($escapeTags as $tag) {
168 $description = preg_replace_callback(
169 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
170 function ($match) { return escape($match
[0]); },
173 $description = preg_replace(
174 '#(<[^>]+)on[a-z]*="[^"]*"#is',
181 * Render shaare contents through Markdown parser.
182 * 1. Remove HTML generated by Shaarli core.
183 * 2. Reverse the escape function.
184 * 3. Generate markdown descriptions.
185 * 4. Sanitize sensible HTML tags for security.
186 * 5. Wrap description in 'markdown
' CSS class.
188 * @param string $description input description text.
190 * @return string HTML processed $description.
192 function process_markdown($description)
194 $parsedown = new Parsedown();
196 $processedDescription = $description;
197 $processedDescription = reverse_text2clickable($processedDescription);
198 $processedDescription = reverse_nl2br($processedDescription);
199 $processedDescription = reverse_space2nbsp($processedDescription);
200 $processedDescription = unescape($processedDescription);
201 $processedDescription = $parsedown
202 ->setMarkupEscaped(false)
203 ->setBreaksEnabled(true)
204 ->text($processedDescription);
205 $processedDescription = sanitize_html($processedDescription);
207 if(!empty($processedDescription)){
208 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
211 return $processedDescription;