markdown.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * Plugin Markdown.
  4. *
  5. * Shaare's descriptions are parsed with Markdown.
  6. */
  7. /*
  8. * If this tag is used on a shaare, the description won't be processed by Parsedown.
  9. */
  10. define('NO_MD_TAG', 'nomarkdown');
  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. if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
  22. $value = stripNoMarkdownTag($value);
  23. continue;
  24. }
  25. $value['description'] = process_markdown($value['description']);
  26. }
  27. return $data;
  28. }
  29. /**
  30. * Parse feed linklist descriptions.
  31. *
  32. * @param array $data linklist data.
  33. *
  34. * @return mixed linklist data parsed in markdown (and converted to HTML).
  35. */
  36. function hook_markdown_render_feed($data)
  37. {
  38. foreach ($data['links'] as &$value) {
  39. if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
  40. $value = stripNoMarkdownTag($value);
  41. continue;
  42. }
  43. $value['description'] = process_markdown($value['description']);
  44. }
  45. return $data;
  46. }
  47. /**
  48. * Parse daily descriptions.
  49. *
  50. * @param array $data daily data.
  51. *
  52. * @return mixed daily data parsed in markdown (and converted to HTML).
  53. */
  54. function hook_markdown_render_daily($data)
  55. {
  56. // Manipulate columns data
  57. foreach ($data['cols'] as &$value) {
  58. foreach ($value as &$value2) {
  59. if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
  60. $value2 = stripNoMarkdownTag($value2);
  61. continue;
  62. }
  63. $value2['formatedDescription'] = process_markdown($value2['formatedDescription']);
  64. }
  65. }
  66. return $data;
  67. }
  68. /**
  69. * Check if noMarkdown is set in tags.
  70. *
  71. * @param string $tags tag list
  72. *
  73. * @return bool true if markdown should be disabled on this link.
  74. */
  75. function noMarkdownTag($tags)
  76. {
  77. return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags);
  78. }
  79. /**
  80. * Remove the no-markdown meta tag so it won't be displayed.
  81. *
  82. * @param array $link Link data.
  83. *
  84. * @return array Updated link without no markdown tag.
  85. */
  86. function stripNoMarkdownTag($link)
  87. {
  88. if (! empty($link['taglist'])) {
  89. $offset = array_search(NO_MD_TAG, $link['taglist']);
  90. if ($offset !== false) {
  91. unset($link['taglist'][$offset]);
  92. }
  93. }
  94. if (!empty($link['tags'])) {
  95. str_replace(NO_MD_TAG, '', $link['tags']);
  96. }
  97. return $link;
  98. }
  99. /**
  100. * When link list is displayed, include markdown CSS.
  101. *
  102. * @param array $data includes data.
  103. *
  104. * @return mixed - includes data with markdown CSS file added.
  105. */
  106. function hook_markdown_render_includes($data)
  107. {
  108. if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
  109. || $data['_PAGE_'] == Router::$PAGE_DAILY
  110. || $data['_PAGE_'] == Router::$PAGE_EDITLINK
  111. ) {
  112. $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
  113. }
  114. return $data;
  115. }
  116. /**
  117. * Hook render_editlink.
  118. * Adds an help link to markdown syntax.
  119. *
  120. * @param array $data data passed to plugin
  121. *
  122. * @return array altered $data.
  123. */
  124. function hook_markdown_render_editlink($data)
  125. {
  126. // Load help HTML into a string
  127. $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
  128. // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
  129. if (! in_array(NO_MD_TAG, $data['tags'])) {
  130. $data['tags'][NO_MD_TAG] = 0;
  131. }
  132. return $data;
  133. }
  134. /**
  135. * Remove HTML links auto generated by Shaarli core system.
  136. * Keeps HREF attributes.
  137. *
  138. * @param string $description input description text.
  139. *
  140. * @return string $description without HTML links.
  141. */
  142. function reverse_text2clickable($description)
  143. {
  144. $descriptionLines = explode(PHP_EOL, $description);
  145. $descriptionOut = '';
  146. $codeBlockOn = false;
  147. $lineCount = 0;
  148. foreach ($descriptionLines as $descriptionLine) {
  149. // Detect line of code: starting with 4 spaces,
  150. // except lists which can start with +/*/- or `2.` after spaces.
  151. $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
  152. // Detect and toggle block of code
  153. if (!$codeBlockOn) {
  154. $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
  155. }
  156. elseif (preg_match('/^```/', $descriptionLine) > 0) {
  157. $codeBlockOn = false;
  158. }
  159. $hashtagTitle = ' title="Hashtag [^"]+"';
  160. // Reverse `inline code` hashtags.
  161. $descriptionLine = preg_replace(
  162. '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m',
  163. '$1$2$3',
  164. $descriptionLine
  165. );
  166. // Reverse all links in code blocks, only non hashtag elsewhere.
  167. $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?';
  168. $descriptionLine = preg_replace(
  169. '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m',
  170. '$1',
  171. $descriptionLine
  172. );
  173. $descriptionOut .= $descriptionLine;
  174. if ($lineCount++ < count($descriptionLines) - 1) {
  175. $descriptionOut .= PHP_EOL;
  176. }
  177. }
  178. return $descriptionOut;
  179. }
  180. /**
  181. * Remove <br> tag to let markdown handle it.
  182. *
  183. * @param string $description input description text.
  184. *
  185. * @return string $description without <br> tags.
  186. */
  187. function reverse_nl2br($description)
  188. {
  189. return preg_replace('!<br */?>!im', '', $description);
  190. }
  191. /**
  192. * Remove HTML spaces '&nbsp;' auto generated by Shaarli core system.
  193. *
  194. * @param string $description input description text.
  195. *
  196. * @return string $description without HTML links.
  197. */
  198. function reverse_space2nbsp($description)
  199. {
  200. return preg_replace('/(^| )&nbsp;/m', '$1 ', $description);
  201. }
  202. /**
  203. * Remove dangerous HTML tags (tags, iframe, etc.).
  204. * Doesn't affect <code> content (already escaped by Parsedown).
  205. *
  206. * @param string $description input description text.
  207. *
  208. * @return string given string escaped.
  209. */
  210. function sanitize_html($description)
  211. {
  212. $escapeTags = array(
  213. 'script',
  214. 'style',
  215. 'link',
  216. 'iframe',
  217. 'frameset',
  218. 'frame',
  219. );
  220. foreach ($escapeTags as $tag) {
  221. $description = preg_replace_callback(
  222. '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
  223. function ($match) { return escape($match[0]); },
  224. $description);
  225. }
  226. $description = preg_replace(
  227. '#(<[^>]+)on[a-z]*="[^"]*"#is',
  228. '$1',
  229. $description);
  230. return $description;
  231. }
  232. /**
  233. * Render shaare contents through Markdown parser.
  234. * 1. Remove HTML generated by Shaarli core.
  235. * 2. Reverse the escape function.
  236. * 3. Generate markdown descriptions.
  237. * 4. Sanitize sensible HTML tags for security.
  238. * 5. Wrap description in 'markdown' CSS class.
  239. *
  240. * @param string $description input description text.
  241. *
  242. * @return string HTML processed $description.
  243. */
  244. function process_markdown($description)
  245. {
  246. $parsedown = new Parsedown();
  247. $processedDescription = $description;
  248. $processedDescription = reverse_nl2br($processedDescription);
  249. $processedDescription = reverse_space2nbsp($processedDescription);
  250. $processedDescription = reverse_text2clickable($processedDescription);
  251. $processedDescription = unescape($processedDescription);
  252. $processedDescription = $parsedown
  253. ->setMarkupEscaped(false)
  254. ->setBreaksEnabled(true)
  255. ->text($processedDescription);
  256. $processedDescription = sanitize_html($processedDescription);
  257. if(!empty($processedDescription)){
  258. $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
  259. }
  260. return $processedDescription;
  261. }