]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/markdown/markdown.php
Markdown: fixes feed rendering with nomarkdown tag
[github/shaarli/Shaarli.git] / plugins / markdown / markdown.php
1 <?php
2
3 /**
4 * Plugin Markdown.
5 *
6 * Shaare's descriptions are parsed with Markdown.
7 */
8
9 /*
10 * If this tag is used on a shaare, the description won't be processed by Parsedown.
11 */
12 define('NO_MD_TAG', 'nomarkdown');
13
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 */
21 function hook_markdown_render_linklist($data)
22 {
23 foreach ($data['links'] as &$value) {
24 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
25 $value = stripNoMarkdownTag($value);
26 continue;
27 }
28 $value['description'] = process_markdown($value['description']);
29 }
30 return $data;
31 }
32
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 */
40 function hook_markdown_render_feed($data)
41 {
42 foreach ($data['links'] as &$value) {
43 if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
44 $value = stripNoMarkdownTag($value);
45 continue;
46 }
47 $value['description'] = process_markdown($value['description']);
48 }
49
50 return $data;
51 }
52
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 */
60 function hook_markdown_render_daily($data)
61 {
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);
67 continue;
68 }
69 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
70 }
71 }
72
73 return $data;
74 }
75
76 /**
77 * Check if noMarkdown is set in tags.
78 *
79 * @param string $tags tag list
80 *
81 * @return bool true if markdown should be disabled on this link.
82 */
83 function noMarkdownTag($tags)
84 {
85 return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags);
86 }
87
88 /**
89 * Remove the no-markdown meta tag so it won't be displayed.
90 *
91 * @param array $link Link data.
92 *
93 * @return array Updated link without no markdown tag.
94 */
95 function stripNoMarkdownTag($link)
96 {
97 if (! empty($link['taglist'])) {
98 $offset = array_search(NO_MD_TAG, $link['taglist']);
99 if ($offset !== false) {
100 unset($link['taglist'][$offset]);
101 }
102 }
103
104 if (!empty($link['tags'])) {
105 str_replace(NO_MD_TAG, '', $link['tags']);
106 }
107
108 return $link;
109 }
110
111 /**
112 * When link list is displayed, include markdown CSS.
113 *
114 * @param array $data includes data.
115 *
116 * @return mixed - includes data with markdown CSS file added.
117 */
118 function hook_markdown_render_includes($data)
119 {
120 if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
121 || $data['_PAGE_'] == Router::$PAGE_DAILY
122 || $data['_PAGE_'] == Router::$PAGE_EDITLINK
123 ) {
124
125 $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
126 }
127
128 return $data;
129 }
130
131 /**
132 * Hook render_editlink.
133 * Adds an help link to markdown syntax.
134 *
135 * @param array $data data passed to plugin
136 *
137 * @return array altered $data.
138 */
139 function hook_markdown_render_editlink($data)
140 {
141 // Load help HTML into a string
142 $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
143
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;
147 }
148
149 return $data;
150 }
151
152
153 /**
154 * Remove HTML links auto generated by Shaarli core system.
155 * Keeps HREF attributes.
156 *
157 * @param string $description input description text.
158 *
159 * @return string $description without HTML links.
160 */
161 function reverse_text2clickable($description)
162 {
163 $descriptionLines = explode(PHP_EOL, $description);
164 $descriptionOut = '';
165 $codeBlockOn = false;
166 $lineCount = 0;
167
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
173 if (!$codeBlockOn) {
174 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
175 }
176 elseif (preg_match('/^```/', $descriptionLine) > 0) {
177 $codeBlockOn = false;
178 }
179
180 $hashtagTitle = ' title="Hashtag [^"]+"';
181 // Reverse `inline code` hashtags.
182 $descriptionLine = preg_replace(
183 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
184 '$1$2$3',
185 $descriptionLine
186 );
187
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',
192 '$1',
193 $descriptionLine
194 );
195
196 $descriptionOut .= $descriptionLine;
197 if ($lineCount++ < count($descriptionLines) - 1) {
198 $descriptionOut .= PHP_EOL;
199 }
200 }
201 return $descriptionOut;
202 }
203
204 /**
205 * Remove <br> tag to let markdown handle it.
206 *
207 * @param string $description input description text.
208 *
209 * @return string $description without <br> tags.
210 */
211 function reverse_nl2br($description)
212 {
213 return preg_replace('!<br */?>!im', '', $description);
214 }
215
216 /**
217 * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
218 *
219 * @param string $description input description text.
220 *
221 * @return string $description without HTML links.
222 */
223 function reverse_space2nbsp($description)
224 {
225 return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
226 }
227
228 /**
229 * Remove dangerous HTML tags (tags, iframe, etc.).
230 * Doesn't affect <code> content (already escaped by Parsedown).
231 *
232 * @param string $description input description text.
233 *
234 * @return string given string escaped.
235 */
236 function sanitize_html($description)
237 {
238 $escapeTags = array(
239 'script',
240 'style',
241 'link',
242 'iframe',
243 'frameset',
244 'frame',
245 );
246 foreach ($escapeTags as $tag) {
247 $description = preg_replace_callback(
248 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
249 function ($match) { return escape($match[0]); },
250 $description);
251 }
252 $description = preg_replace(
253 '#(<[^>]+)on[a-z]*="[^"]*"#is',
254 '$1',
255 $description);
256 return $description;
257 }
258
259 /**
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.
266 *
267 * @param string $description input description text.
268 *
269 * @return string HTML processed $description.
270 */
271 function process_markdown($description)
272 {
273 $parsedown = new Parsedown();
274
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);
285
286 if(!empty($processedDescription)){
287 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
288 }
289
290 return $processedDescription;
291 }