markdown.php 7.5 KB

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