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