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