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