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($value['description'], $conf->get('security.markdown_escape', true));
35 * Parse feed linklist descriptions.
37 * @param array $data linklist data.
38 * @param ConfigManager $conf instance.
40 * @return mixed linklist data parsed in markdown (and converted to HTML).
42 function hook_markdown_render_feed($data, $conf)
44 foreach ($data['links'] as &$value) {
45 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
46 $value = stripNoMarkdownTag($value);
49 $value['description'] = process_markdown($value['description'], $conf->get('security.markdown_escape', true));
56 * Parse daily descriptions.
58 * @param array $data daily data.
59 * @param ConfigManager $conf instance.
61 * @return mixed daily data parsed in markdown (and converted to HTML).
63 function hook_markdown_render_daily($data, $conf)
65 // Manipulate columns data
66 foreach ($data['cols'] as &$value) {
67 foreach ($value as &$value2) {
68 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
69 $value2 = stripNoMarkdownTag($value2);
72 $value2['formatedDescription'] = process_markdown(
73 $value2['formatedDescription'],
74 $conf->get('security.markdown_escape', true)
83 * Check if noMarkdown is set in tags.
85 * @param string $tags tag list
87 * @return bool true if markdown should be disabled on this link.
89 function noMarkdownTag($tags)
91 return preg_match('/(^|\s)'. NO_MD_TAG
.'(\s|$)/', $tags);
95 * Remove the no-markdown meta tag so it won't be displayed.
97 * @param array $link Link data.
99 * @return array Updated link without no markdown tag.
101 function stripNoMarkdownTag($link)
103 if (! empty($link['taglist'])) {
104 $offset = array_search(NO_MD_TAG
, $link['taglist']);
105 if ($offset !== false) {
106 unset($link['taglist'][$offset]);
110 if (!empty($link['tags'])) {
111 str_replace(NO_MD_TAG
, '', $link['tags']);
118 * When link list is displayed, include markdown CSS.
120 * @param array $data includes data.
122 * @return mixed - includes data with markdown CSS file added.
124 function hook_markdown_render_includes($data)
126 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
127 || $data['_PAGE_'] == Router
::$PAGE_DAILY
128 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
131 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
138 * Hook render_editlink.
139 * Adds an help link to markdown syntax.
141 * @param array $data data passed to plugin
143 * @return array altered $data.
145 function hook_markdown_render_editlink($data)
147 // Load help HTML into a string
148 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
150 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
151 if (! in_array(NO_MD_TAG
, $data['tags'])) {
152 $data['tags'][NO_MD_TAG
] = 0;
160 * Remove HTML links auto generated by Shaarli core system.
161 * Keeps HREF attributes.
163 * @param string $description input description text.
165 * @return string $description without HTML links.
167 function reverse_text2clickable($description)
169 $descriptionLines = explode(PHP_EOL
, $description);
170 $descriptionOut = '';
171 $codeBlockOn = false;
174 foreach ($descriptionLines as $descriptionLine) {
175 // Detect line of code: starting with 4 spaces,
176 // except lists which can start with +/*/- or `2.` after spaces.
177 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
178 // Detect and toggle block of code
180 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
182 elseif (preg_match('/^```/', $descriptionLine) > 0) {
183 $codeBlockOn = false;
186 $hashtagTitle = ' title="Hashtag [^"]+"';
187 // Reverse `inline code` hashtags.
188 $descriptionLine = preg_replace(
189 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
194 // Reverse all links in code blocks, only non hashtag elsewhere.
195 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
196 $descriptionLine = preg_replace(
197 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
202 $descriptionOut .= $descriptionLine;
203 if ($lineCount++ < count($descriptionLines) - 1) {
204 $descriptionOut .= PHP_EOL;
207 return $descriptionOut;
211 * Remove <br> tag to let markdown handle it.
213 * @param string $description input description text.
215 * @return string $description without <br> tags.
217 function reverse_nl2br($description)
219 return preg_replace('!<br */
?>!im
', '', $description);
223 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
225 * @param string $description input description text.
227 * @return string $description without HTML links.
229 function reverse_space2nbsp($description)
231 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
235 * Remove dangerous HTML tags (tags, iframe, etc.).
236 * Doesn't affect
<code
> content (already escaped by Parsedown
).
238 * @param
string $description input description text
.
240 * @return string given
string escaped
.
242 function sanitize_html($description)
252 foreach ($escapeTags as $tag) {
253 $description = preg_replace_callback(
254 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
255 function ($match) { return escape($match
[0]); },
258 $description = preg_replace(
259 '#(<[^>]+)on[a-z]*="?[^ "]*"?#is',
266 * Render shaare contents through Markdown parser.
267 * 1. Remove HTML generated by Shaarli core.
268 * 2. Reverse the escape function.
269 * 3. Generate markdown descriptions.
270 * 4. Sanitize sensible HTML tags for security.
271 * 5. Wrap description in 'markdown
' CSS class.
273 * @param string $description input description text.
274 * @param bool $escape escape HTML entities
276 * @return string HTML processed $description.
278 function process_markdown($description, $escape = true)
280 $parsedown = new Parsedown();
282 $processedDescription = $description;
283 $processedDescription = reverse_nl2br($processedDescription);
284 $processedDescription = reverse_space2nbsp($processedDescription);
285 $processedDescription = reverse_text2clickable($processedDescription);
286 $processedDescription = unescape($processedDescription);
287 $processedDescription = $parsedown
288 ->setMarkupEscaped($escape)
289 ->setBreaksEnabled(true)
290 ->text($processedDescription);
291 $processedDescription = sanitize_html($processedDescription);
293 if(!empty($processedDescription)){
294 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
297 return $processedDescription;