6 * Shaare's descriptions are parsed with Markdown.
9 require_once 'Parsedown.php';
12 * Parse linklist descriptions.
14 * @param array $data linklist data.
16 * @return mixed linklist data parsed in markdown (and converted to HTML).
18 function hook_markdown_render_linklist($data)
20 foreach ($data['links'] as &$value) {
21 $value['description'] = process_markdown($value['description']);
28 * Parse daily descriptions.
30 * @param array $data daily data.
32 * @return mixed daily data parsed in markdown (and converted to HTML).
34 function hook_markdown_render_daily($data)
36 // Manipulate columns data
37 foreach ($data['cols'] as &$value) {
38 foreach ($value as &$value2) {
39 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
47 * When link list is displayed, include markdown CSS.
49 * @param array $data includes data.
51 * @return mixed - includes data with markdown CSS file added.
53 function hook_markdown_render_includes($data)
55 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
56 || $data['_PAGE_'] == Router
::$PAGE_DAILY
57 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
60 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
67 * Hook render_editlink.
68 * Adds an help link to markdown syntax.
70 * @param array $data data passed to plugin
72 * @return array altered $data.
74 function hook_markdown_render_editlink($data)
76 // Load help HTML into a string
77 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
83 * Remove HTML links auto generated by Shaarli core system.
84 * Keeps HREF attributes.
86 * @param string $description input description text.
88 * @return string $description without HTML links.
90 function reverse_text2clickable($description)
92 return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description);
96 * Remove <br> tag to let markdown handle it.
98 * @param string $description input description text.
100 * @return string $description without <br> tags.
102 function reverse_nl2br($description)
104 return preg_replace('!<br */
?>!im
', '', $description);
108 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
110 * @param string $description input description text.
112 * @return string $description without HTML links.
114 function reverse_space2nbsp($description)
116 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
120 * Remove dangerous HTML tags (tags, iframe, etc.).
121 * Doesn't affect
<code
> content (already escaped by Parsedown
).
123 * @param
string $description input description text
.
125 * @return string given
string escaped
.
127 function sanitize_html($description)
137 foreach ($escapeTags as $tag) {
138 $description = preg_replace_callback(
139 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
140 function ($match) { return escape($match
[0]); },
143 $description = preg_replace(
144 '#(<[^>]+)on[a-z]*="[^"]*"#is',
151 * Render shaare contents through Markdown parser.
152 * 1. Remove HTML generated by Shaarli core.
153 * 2. Reverse the escape function.
154 * 3. Generate markdown descriptions.
155 * 4. Sanitize sensible HTML tags for security.
156 * 5. Wrap description in 'markdown
' CSS class.
158 * @param string $description input description text.
160 * @return string HTML processed $description.
162 function process_markdown($description)
164 $parsedown = new Parsedown();
166 $processedDescription = $description;
167 $processedDescription = reverse_text2clickable($processedDescription);
168 $processedDescription = reverse_nl2br($processedDescription);
169 $processedDescription = reverse_space2nbsp($processedDescription);
170 $processedDescription = unescape($processedDescription);
171 $processedDescription = $parsedown
172 ->setMarkupEscaped(false)
173 ->setBreaksEnabled(true)
174 ->text($processedDescription);
175 $processedDescription = sanitize_html($processedDescription);
177 if(!empty($processedDescription)){
178 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
181 return $processedDescription;