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.
14 define('NO_MD_TAG', 'nomarkdown');
17 * Parse linklist descriptions.
19 * @param array $data linklist data.
21 * @return mixed linklist data parsed in markdown (and converted to HTML).
23 function hook_markdown_render_linklist($data)
25 foreach ($data['links'] as &$value) {
26 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
27 $value['taglist'] = stripNoMarkdownTag($value['taglist']);
30 $value['description'] = process_markdown($value['description']);
36 * Parse feed linklist descriptions.
38 * @param array $data linklist data.
40 * @return mixed linklist data parsed in markdown (and converted to HTML).
42 function hook_markdown_render_feed($data)
44 foreach ($data['links'] as &$value) {
45 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
46 $value['tags'] = stripNoMarkdownTag($value['tags']);
49 $value['description'] = process_markdown($value['description']);
56 * Parse daily descriptions.
58 * @param array $data daily data.
60 * @return mixed daily data parsed in markdown (and converted to HTML).
62 function hook_markdown_render_daily($data)
64 // Manipulate columns data
65 foreach ($data['cols'] as &$value) {
66 foreach ($value as &$value2) {
67 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
70 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
78 * Check if noMarkdown is set in tags.
80 * @param string $tags tag list
82 * @return bool true if markdown should be disabled on this link.
84 function noMarkdownTag($tags)
86 return strpos($tags, NO_MD_TAG
) !== false;
90 * Remove the no-markdown meta tag so it won't be displayed.
92 * @param string $tags Tag list.
94 * @return string tag list without no markdown tag.
96 function stripNoMarkdownTag($tags)
98 unset($tags[array_search(NO_MD_TAG
, $tags)]);
99 return array_values($tags);
103 * When link list is displayed, include markdown CSS.
105 * @param array $data includes data.
107 * @return mixed - includes data with markdown CSS file added.
109 function hook_markdown_render_includes($data)
111 if ($data['_PAGE_'] == Router
::$PAGE_LINKLIST
112 || $data['_PAGE_'] == Router
::$PAGE_DAILY
113 || $data['_PAGE_'] == Router
::$PAGE_EDITLINK
116 $data['css_files'][] = PluginManager
::$PLUGINS_PATH . '/markdown/markdown.css';
123 * Hook render_editlink.
124 * Adds an help link to markdown syntax.
126 * @param array $data data passed to plugin
128 * @return array altered $data.
130 function hook_markdown_render_editlink($data)
132 // Load help HTML into a string
133 $data['edit_link_plugin'][] = file_get_contents(PluginManager
::$PLUGINS_PATH .'/markdown/help.html');
135 // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
136 if (! in_array(NO_MD_TAG
, $data['tags'])) {
137 $data['tags'][NO_MD_TAG
] = 0;
145 * Remove HTML links auto generated by Shaarli core system.
146 * Keeps HREF attributes.
148 * @param string $description input description text.
150 * @return string $description without HTML links.
152 function reverse_text2clickable($description)
154 $descriptionLines = explode(PHP_EOL
, $description);
155 $descriptionOut = '';
156 $codeBlockOn = false;
159 foreach ($descriptionLines as $descriptionLine) {
160 // Detect line of code
161 $codeLineOn = preg_match('/^ /', $descriptionLine) > 0;
162 // Detect and toggle block of code
164 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
166 elseif (preg_match('/^```/', $descriptionLine) > 0) {
167 $codeBlockOn = false;
170 $hashtagTitle = ' title="Hashtag [^"]+"';
171 // Reverse `inline code` hashtags.
172 $descriptionLine = preg_replace(
173 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
178 // Reverse hashtag links if we're in a code block
.
179 $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : '';
180 $descriptionLine = preg_replace(
181 '!<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>!m',
186 $descriptionOut .= $descriptionLine;
187 if ($lineCount++ < count($descriptionLines) - 1) {
188 $descriptionOut .= PHP_EOL;
191 return $descriptionOut;
195 * Remove <br> tag to let markdown handle it.
197 * @param string $description input description text.
199 * @return string $description without <br> tags.
201 function reverse_nl2br($description)
203 return preg_replace('!<br */
?>!im
', '', $description);
207 * Remove HTML spaces ' 
;' auto generated by Shaarli core system.
209 * @param string $description input description text.
211 * @return string $description without HTML links.
213 function reverse_space2nbsp($description)
215 return preg_replace('/(^
| ) 
;/m
', '$1 ', $description);
219 * Remove dangerous HTML tags (tags, iframe, etc.).
220 * Doesn't affect
<code
> content (already escaped by Parsedown
).
222 * @param
string $description input description text
.
224 * @return string given
string escaped
.
226 function sanitize_html($description)
236 foreach ($escapeTags as $tag) {
237 $description = preg_replace_callback(
238 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
239 function ($match) { return escape($match
[0]); },
242 $description = preg_replace(
243 '#(<[^>]+)on[a-z]*="[^"]*"#is',
250 * Render shaare contents through Markdown parser.
251 * 1. Remove HTML generated by Shaarli core.
252 * 2. Reverse the escape function.
253 * 3. Generate markdown descriptions.
254 * 4. Sanitize sensible HTML tags for security.
255 * 5. Wrap description in 'markdown
' CSS class.
257 * @param string $description input description text.
259 * @return string HTML processed $description.
261 function process_markdown($description)
263 $parsedown = new Parsedown();
265 $processedDescription = $description;
266 $processedDescription = reverse_nl2br($processedDescription);
267 $processedDescription = reverse_space2nbsp($processedDescription);
268 $processedDescription = reverse_text2clickable($processedDescription);
269 $processedDescription = unescape($processedDescription);
270 $processedDescription = $parsedown
271 ->setMarkupEscaped(false)
272 ->setBreaksEnabled(true)
273 ->text($processedDescription);
274 $processedDescription = sanitize_html($processedDescription);
276 if(!empty($processedDescription)){
277 $processedDescription = '<div
class="markdown">'. $processedDescription . '</div
>';
280 return $processedDescription;