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['taglist'] = stripNoMarkdownTag($value['taglist']);
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['tags'] = stripNoMarkdownTag($value['tags']);
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'])) {
68 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
76 * Check if noMarkdown is set in tags.
78 * @param string $tags tag list
80 * @return bool true if markdown should be disabled on this link.
82 function noMarkdownTag($tags)
84 return strpos($tags, NO_MD_TAG
) !== false;
88 * Remove the no-markdown meta tag so it won't be displayed.
90 * @param string $tags Tag list.
92 * @return string tag list without no markdown tag.
94 function stripNoMarkdownTag($tags)
96 unset($tags[array_search(NO_MD_TAG
, $tags)]);
97 return array_values($tags);
101 * When link list is displayed, include markdown CSS.
103 * @param array $data includes data.
105 * @return mixed - includes data with markdown CSS file added.
107 function hook_markdown_render_includes($data)
109 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
110 || $data['_PAGE_'] == Router
::$PAGE_DAILY
111 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
114 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
121 * Hook render_editlink.
122 * Adds an help link to markdown syntax.
124 * @param array $data data passed to plugin
126 * @return array altered $data.
128 function hook_markdown_render_editlink($data)
130 // Load help HTML into a string
131 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
133 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
134 if (! in_array(NO_MD_TAG
, $data['tags'])) {
135 $data['tags'][NO_MD_TAG
] = 0;
143 * Remove HTML links auto generated by Shaarli core system.
144 * Keeps HREF attributes.
146 * @param string $description input description text.
148 * @return string $description without HTML links.
150 function reverse_text2clickable($description)
152 $descriptionLines = explode(PHP_EOL
, $description);
153 $descriptionOut = '';
154 $codeBlockOn = false;
157 foreach ($descriptionLines as $descriptionLine) {
158 // Detect line of code: starting with 4 spaces,
159 // except lists which can start with +/*/- or `2.` after spaces.
160 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
161 // Detect and toggle block of code
163 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
165 elseif (preg_match('/^```/', $descriptionLine) > 0) {
166 $codeBlockOn = false;
169 $hashtagTitle = ' title="Hashtag [^"]+"';
170 // Reverse `inline code` hashtags.
171 $descriptionLine = preg_replace(
172 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
177 // Reverse all links in code blocks, only non hashtag elsewhere.
178 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
179 $descriptionLine = preg_replace(
180 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
185 $descriptionOut .= $descriptionLine;
186 if ($lineCount++ < count($descriptionLines) - 1) {
187 $descriptionOut .= PHP_EOL;
190 return $descriptionOut;
194 * Remove <br> tag to let markdown handle it.
196 * @param string $description input description text.
198 * @return string $description without <br> tags.
200 function reverse_nl2br($description)
202 return preg_replace('!<br */
?>!im
', '', $description);
206 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
208 * @param string $description input description text.
210 * @return string $description without HTML links.
212 function reverse_space2nbsp($description)
214 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
218 * Remove dangerous HTML tags (tags, iframe, etc.).
219 * Doesn't affect
<code
> content (already escaped by Parsedown
).
221 * @param
string $description input description text
.
223 * @return string given
string escaped
.
225 function sanitize_html($description)
235 foreach ($escapeTags as $tag) {
236 $description = preg_replace_callback(
237 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
238 function ($match) { return escape($match
[0]); },
241 $description = preg_replace(
242 '#(<[^>]+)on[a-z]*="[^"]*"#is',
249 * Render shaare contents through Markdown parser.
250 * 1. Remove HTML generated by Shaarli core.
251 * 2. Reverse the escape function.
252 * 3. Generate markdown descriptions.
253 * 4. Sanitize sensible HTML tags for security.
254 * 5. Wrap description in 'markdown
' CSS class.
256 * @param string $description input description text.
258 * @return string HTML processed $description.
260 function process_markdown($description)
262 $parsedown = new Parsedown();
264 $processedDescription = $description;
265 $processedDescription = reverse_nl2br($processedDescription);
266 $processedDescription = reverse_space2nbsp($processedDescription);
267 $processedDescription = reverse_text2clickable($processedDescription);
268 $processedDescription = unescape($processedDescription);
269 $processedDescription = $parsedown
270 ->setMarkupEscaped(false)
271 ->setBreaksEnabled(true)
272 ->text($processedDescription);
273 $processedDescription = sanitize_html($processedDescription);
275 if(!empty($processedDescription)){
276 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
279 return $processedDescription;