]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/markdown/markdown.php
eb7bf23d12a530f976ed7e7b5484644fb8b85619
[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['taglist'] = stripNoMarkdownTag($value['taglist']);
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['tags'] = stripNoMarkdownTag($value['tags']);
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 continue;
67 }
68 $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
69 }
70 }
71
72 return $data;
73 }
74
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 */
82 function noMarkdownTag($tags)
83 {
84 return strpos($tags, NO_MD_TAG) !== false;
85 }
86
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 */
94 function stripNoMarkdownTag($tags)
95 {
96 unset($tags[array_search(NO_MD_TAG, $tags)]);
97 return array_values($tags);
98 }
99
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 */
107 function 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 */
128 function 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');
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
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 */
150 function reverse_text2clickable($description)
151 {
152 $descriptionLines = explode(PHP_EOL, $description);
153 $descriptionOut = '';
154 $codeBlockOn = false;
155 $lineCount = 0;
156
157 foreach ($descriptionLines as $descriptionLine) {
158 // Detect line of code
159 $codeLineOn = preg_match('/^ /', $descriptionLine) > 0;
160 // Detect and toggle block of code
161 if (!$codeBlockOn) {
162 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
163 }
164 elseif (preg_match('/^```/', $descriptionLine) > 0) {
165 $codeBlockOn = false;
166 }
167
168 $hashtagTitle = ' title="Hashtag [^"]+"';
169 // Reverse `inline code` hashtags.
170 $descriptionLine = preg_replace(
171 '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
172 '$1$2$3',
173 $descriptionLine
174 );
175
176 // Reverse hashtag links if we're in a code block.
177 $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : '';
178 $descriptionLine = preg_replace(
179 '!<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>!m',
180 '$1',
181 $descriptionLine
182 );
183
184 $descriptionOut .= $descriptionLine;
185 if ($lineCount++ < count($descriptionLines) - 1) {
186 $descriptionOut .= PHP_EOL;
187 }
188 }
189 return $descriptionOut;
190 }
191
192 /**
193 * Remove <br> tag to let markdown handle it.
194 *
195 * @param string $description input description text.
196 *
197 * @return string $description without <br> tags.
198 */
199 function reverse_nl2br($description)
200 {
201 return preg_replace('!<br */?>!im', '', $description);
202 }
203
204 /**
205 * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
206 *
207 * @param string $description input description text.
208 *
209 * @return string $description without HTML links.
210 */
211 function reverse_space2nbsp($description)
212 {
213 return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
214 }
215
216 /**
217 * Remove dangerous HTML tags (tags, iframe, etc.).
218 * Doesn't affect <code> content (already escaped by Parsedown).
219 *
220 * @param string $description input description text.
221 *
222 * @return string given string escaped.
223 */
224 function sanitize_html($description)
225 {
226 $escapeTags = array(
227 'script',
228 'style',
229 'link',
230 'iframe',
231 'frameset',
232 'frame',
233 );
234 foreach ($escapeTags as $tag) {
235 $description = preg_replace_callback(
236 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
237 function ($match) { return escape($match[0]); },
238 $description);
239 }
240 $description = preg_replace(
241 '#(<[^>]+)on[a-z]*="[^"]*"#is',
242 '$1',
243 $description);
244 return $description;
245 }
246
247 /**
248 * Render shaare contents through Markdown parser.
249 * 1. Remove HTML generated by Shaarli core.
250 * 2. Reverse the escape function.
251 * 3. Generate markdown descriptions.
252 * 4. Sanitize sensible HTML tags for security.
253 * 5. Wrap description in 'markdown' CSS class.
254 *
255 * @param string $description input description text.
256 *
257 * @return string HTML processed $description.
258 */
259 function process_markdown($description)
260 {
261 $parsedown = new Parsedown();
262
263 $processedDescription = $description;
264 $processedDescription = reverse_nl2br($processedDescription);
265 $processedDescription = reverse_space2nbsp($processedDescription);
266 $processedDescription = reverse_text2clickable($processedDescription);
267 $processedDescription = unescape($processedDescription);
268 $processedDescription = $parsedown
269 ->setMarkupEscaped(false)
270 ->setBreaksEnabled(true)
271 ->text($processedDescription);
272 $processedDescription = sanitize_html($processedDescription);
273
274 if(!empty($processedDescription)){
275 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
276 }
277
278 return $processedDescription;
279 }