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.
19 * @return mixed linklist data parsed in markdown (and converted to HTML).
21 function hook_markdown_render_linklist($data)
23 foreach ($data['links'] as &$value) {
24 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
25 $value = stripNoMarkdownTag($value);
28 $value['description'] = process_markdown($value['description']);
34 * Parse feed linklist descriptions.
36 * @param array $data linklist data.
38 * @return mixed linklist data parsed in markdown (and converted to HTML).
40 function hook_markdown_render_feed($data)
42 foreach ($data['links'] as &$value) {
43 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
44 $value = stripNoMarkdownTag($value);
47 $value['description'] = process_markdown($value['description']);
54 * Parse daily descriptions.
56 * @param array $data daily data.
58 * @return mixed daily data parsed in markdown (and converted to HTML).
60 function hook_markdown_render_daily($data)
62 // Manipulate columns data
63 foreach ($data['cols'] as &$value) {
64 foreach ($value as &$value2) {
65 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
66 $value2 = stripNoMarkdownTag($value2);
69 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
77 * Check if noMarkdown is set in tags.
79 * @param string $tags tag list
81 * @return bool true if markdown should be disabled on this link.
83 function noMarkdownTag($tags)
85 return preg_match('/(^|\s)'. NO_MD_TAG
.'(\s|$)/', $tags);
89 * Remove the no-markdown meta tag so it won't be displayed.
91 * @param array $link Link data.
93 * @return array Updated link without no markdown tag.
95 function stripNoMarkdownTag($link)
97 if (! empty($link['taglist'])) {
98 $offset = array_search(NO_MD_TAG
, $link['taglist']);
99 if ($offset !== false) {
100 unset($link['taglist'][$offset]);
104 if (!empty($link['tags'])) {
105 str_replace(NO_MD_TAG
, '', $link['tags']);
112 * When link list is displayed, include markdown CSS.
114 * @param array $data includes data.
116 * @return mixed - includes data with markdown CSS file added.
118 function hook_markdown_render_includes($data)
120 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
121 || $data['_PAGE_'] == Router
::$PAGE_DAILY
122 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
125 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
132 * Hook render_editlink.
133 * Adds an help link to markdown syntax.
135 * @param array $data data passed to plugin
137 * @return array altered $data.
139 function hook_markdown_render_editlink($data)
141 // Load help HTML into a string
142 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
144 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
145 if (! in_array(NO_MD_TAG
, $data['tags'])) {
146 $data['tags'][NO_MD_TAG
] = 0;
154 * Remove HTML links auto generated by Shaarli core system.
155 * Keeps HREF attributes.
157 * @param string $description input description text.
159 * @return string $description without HTML links.
161 function reverse_text2clickable($description)
163 $descriptionLines = explode(PHP_EOL
, $description);
164 $descriptionOut = '';
165 $codeBlockOn = false;
168 foreach ($descriptionLines as $descriptionLine) {
169 // Detect line of code: starting with 4 spaces,
170 // except lists which can start with +/*/- or `2.` after spaces.
171 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
172 // Detect and toggle block of code
174 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
176 elseif (preg_match('/^```/', $descriptionLine) > 0) {
177 $codeBlockOn = false;
180 $hashtagTitle = ' title="Hashtag [^"]+"';
181 // Reverse `inline code` hashtags.
182 $descriptionLine = preg_replace(
183 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
188 // Reverse all links in code blocks, only non hashtag elsewhere.
189 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
190 $descriptionLine = preg_replace(
191 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
196 $descriptionOut .= $descriptionLine;
197 if ($lineCount++ < count($descriptionLines) - 1) {
198 $descriptionOut .= PHP_EOL;
201 return $descriptionOut;
205 * Remove <br> tag to let markdown handle it.
207 * @param string $description input description text.
209 * @return string $description without <br> tags.
211 function reverse_nl2br($description)
213 return preg_replace('!<br */
?>!im
', '', $description);
217 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
219 * @param string $description input description text.
221 * @return string $description without HTML links.
223 function reverse_space2nbsp($description)
225 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
229 * Remove dangerous HTML tags (tags, iframe, etc.).
230 * Doesn't affect
<code
> content (already escaped by Parsedown
).
232 * @param
string $description input description text
.
234 * @return string given
string escaped
.
236 function sanitize_html($description)
246 foreach ($escapeTags as $tag) {
247 $description = preg_replace_callback(
248 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
249 function ($match) { return escape($match
[0]); },
252 $description = preg_replace(
253 '#(<[^>]+)on[a-z]*="[^"]*"#is',
260 * Render shaare contents through Markdown parser.
261 * 1. Remove HTML generated by Shaarli core.
262 * 2. Reverse the escape function.
263 * 3. Generate markdown descriptions.
264 * 4. Sanitize sensible HTML tags for security.
265 * 5. Wrap description in 'markdown
' CSS class.
267 * @param string $description input description text.
269 * @return string HTML processed $description.
271 function process_markdown($description)
273 $parsedown = new Parsedown();
275 $processedDescription = $description;
276 $processedDescription = reverse_nl2br($processedDescription);
277 $processedDescription = reverse_space2nbsp($processedDescription);
278 $processedDescription = reverse_text2clickable($processedDescription);
279 $processedDescription = unescape($processedDescription);
280 $processedDescription = $parsedown
281 ->setMarkupEscaped(false)
282 ->setBreaksEnabled(true)
283 ->text($processedDescription);
284 $processedDescription = sanitize_html($processedDescription);
286 if(!empty($processedDescription)){
287 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
290 return $processedDescription;