]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/markdown/markdown.php
Fix an issue with links not being reversed in code blocks
[github/shaarli/Shaarli.git] / plugins / markdown / markdown.php
CommitLineData
1be4afac
A
1<?php
2
3/**
4 * Plugin Markdown.
5 *
6 * Shaare's descriptions are parsed with Markdown.
7 */
8
3ce20d9e
A
9/*
10 * If this tag is used on a shaare, the description won't be processed by Parsedown.
3ce20d9e 11 */
8c4e6018 12define('NO_MD_TAG', 'nomarkdown');
3ce20d9e 13
1be4afac
A
14/**
15 * Parse linklist descriptions.
16 *
17 * @param array $data linklist data.
18 *
19 * @return mixed linklist data parsed in markdown (and converted to HTML).
20 */
21function hook_markdown_render_linklist($data)
22{
23 foreach ($data['links'] as &$value) {
3ce20d9e 24 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
8c4e6018 25 $value['taglist'] = stripNoMarkdownTag($value['taglist']);
3ce20d9e
A
26 continue;
27 }
1be4afac
A
28 $value['description'] = process_markdown($value['description']);
29 }
1be4afac
A
30 return $data;
31}
32
635d38c2
A
33/**
34 * Parse feed linklist descriptions.
35 *
36 * @param array $data linklist data.
37 *
38 * @return mixed linklist data parsed in markdown (and converted to HTML).
39 */
40function hook_markdown_render_feed($data)
41{
42 foreach ($data['links'] as &$value) {
43 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
8c4e6018 44 $value['tags'] = stripNoMarkdownTag($value['tags']);
635d38c2
A
45 continue;
46 }
47 $value['description'] = process_markdown($value['description']);
48 }
49
50 return $data;
51}
52
1be4afac
A
53/**
54 * Parse daily descriptions.
55 *
56 * @param array $data daily data.
57 *
58 * @return mixed daily data parsed in markdown (and converted to HTML).
59 */
60function hook_markdown_render_daily($data)
61{
62 // Manipulate columns data
63 foreach ($data['cols'] as &$value) {
64 foreach ($value as &$value2) {
3ce20d9e
A
65 if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
66 continue;
67 }
1be4afac
A
68 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
69 }
70 }
71
72 return $data;
73}
74
3ce20d9e
A
75/**
76 * Check if noMarkdown is set in tags.
77 *
78 * @param string $tags tag list
79 *
80 * @return bool true if markdown should be disabled on this link.
81 */
82function noMarkdownTag($tags)
83{
84 return strpos($tags, NO_MD_TAG) !== false;
85}
86
8c4e6018
A
87/**
88 * Remove the no-markdown meta tag so it won't be displayed.
89 *
90 * @param string $tags Tag list.
91 *
92 * @return string tag list without no markdown tag.
93 */
94function stripNoMarkdownTag($tags)
95{
96 unset($tags[array_search(NO_MD_TAG, $tags)]);
97 return array_values($tags);
98}
99
1be4afac
A
100/**
101 * When link list is displayed, include markdown CSS.
102 *
103 * @param array $data includes data.
104 *
105 * @return mixed - includes data with markdown CSS file added.
106 */
107function hook_markdown_render_includes($data)
108{
109 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
110 || $data['_PAGE_'] == Router::$PAGE_DAILY
111 || $data['_PAGE_'] == Router::$PAGE_EDITLINK
112 ) {
113
114 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
115 }
116
117 return $data;
118}
119
120/**
121 * Hook render_editlink.
122 * Adds an help link to markdown syntax.
123 *
124 * @param array $data data passed to plugin
125 *
126 * @return array altered $data.
127 */
128function hook_markdown_render_editlink($data)
129{
130 // Load help HTML into a string
131 $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
3ce20d9e
A
132
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;
136 }
137
1be4afac
A
138 return $data;
139}
140
141
142/**
143 * Remove HTML links auto generated by Shaarli core system.
144 * Keeps HREF attributes.
145 *
146 * @param string $description input description text.
147 *
148 * @return string $description without HTML links.
149 */
150function reverse_text2clickable($description)
151{
9ccca401
A
152 $descriptionLines = explode(PHP_EOL, $description);
153 $descriptionOut = '';
154 $codeBlockOn = false;
155 $lineCount = 0;
156
157 foreach ($descriptionLines as $descriptionLine) {
c5941f31
A
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;
9ccca401
A
161 // Detect and toggle block of code
162 if (!$codeBlockOn) {
163 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
164 }
165 elseif (preg_match('/^```/', $descriptionLine) > 0) {
166 $codeBlockOn = false;
167 }
168
169 $hashtagTitle = ' title="Hashtag [^"]+"';
170 // Reverse `inline code` hashtags.
171 $descriptionLine = preg_replace(
172 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
173 '$1$2$3',
174 $descriptionLine
175 );
176
c5941f31
A
177 // Reverse all links in code blocks, only non hashtag elsewhere.
178 $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
9ccca401 179 $descriptionLine = preg_replace(
c5941f31 180 '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
9ccca401
A
181 '$1',
182 $descriptionLine
183 );
184
185 $descriptionOut .= $descriptionLine;
186 if ($lineCount++ < count($descriptionLines) - 1) {
187 $descriptionOut .= PHP_EOL;
188 }
189 }
190 return $descriptionOut;
1be4afac
A
191}
192
193/**
194 * Remove <br> tag to let markdown handle it.
195 *
196 * @param string $description input description text.
197 *
198 * @return string $description without <br> tags.
199 */
200function reverse_nl2br($description)
201{
202 return preg_replace('!<br */?>!im', '', $description);
203}
204
205/**
206 * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
207 *
208 * @param string $description input description text.
209 *
210 * @return string $description without HTML links.
211 */
212function reverse_space2nbsp($description)
213{
214 return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
215}
216
217/**
2925687e
A
218 * Remove dangerous HTML tags (tags, iframe, etc.).
219 * Doesn't affect <code> content (already escaped by Parsedown).
1be4afac
A
220 *
221 * @param string $description input description text.
222 *
2925687e 223 * @return string given string escaped.
1be4afac 224 */
2925687e 225function sanitize_html($description)
1be4afac 226{
2925687e
A
227 $escapeTags = array(
228 'script',
229 'style',
230 'link',
231 'iframe',
232 'frameset',
233 'frame',
234 );
235 foreach ($escapeTags as $tag) {
236 $description = preg_replace_callback(
237 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
238 function ($match) { return escape($match[0]); },
239 $description);
240 }
241 $description = preg_replace(
242 '#(<[^>]+)on[a-z]*="[^"]*"#is',
243 '$1',
244 $description);
245 return $description;
1be4afac
A
246}
247
248/**
249 * Render shaare contents through Markdown parser.
250 * 1. Remove HTML generated by Shaarli core.
2925687e
A
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.
1be4afac
A
255 *
256 * @param string $description input description text.
257 *
258 * @return string HTML processed $description.
259 */
260function process_markdown($description)
261{
262 $parsedown = new Parsedown();
263
264 $processedDescription = $description;
1be4afac
A
265 $processedDescription = reverse_nl2br($processedDescription);
266 $processedDescription = reverse_space2nbsp($processedDescription);
9ccca401 267 $processedDescription = reverse_text2clickable($processedDescription);
2925687e 268 $processedDescription = unescape($processedDescription);
1be4afac
A
269 $processedDescription = $parsedown
270 ->setMarkupEscaped(false)
271 ->setBreaksEnabled(true)
272 ->text($processedDescription);
2925687e 273 $processedDescription = sanitize_html($processedDescription);
841df2dd 274
275 if(!empty($processedDescription)){
276 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
277 }
1be4afac
A
278
279 return $processedDescription;
280}