markdown.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Plugin Markdown.
  4. *
  5. * Shaare's descriptions are parsed with Markdown.
  6. */
  7. require_once 'Parsedown.php';
  8. /*
  9. * If this tag is used on a shaare, the description won't be processed by Parsedown.
  10. */
  11. define('NO_MD_TAG', 'nomarkdown');
  12. /**
  13. * Parse linklist descriptions.
  14. *
  15. * @param array $data linklist data.
  16. *
  17. * @return mixed linklist data parsed in markdown (and converted to HTML).
  18. */
  19. function hook_markdown_render_linklist($data)
  20. {
  21. foreach ($data['links'] as &$value) {
  22. if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
  23. $value['taglist'] = stripNoMarkdownTag($value['taglist']);
  24. continue;
  25. }
  26. $value['description'] = process_markdown($value['description']);
  27. }
  28. return $data;
  29. }
  30. /**
  31. * Parse feed linklist descriptions.
  32. *
  33. * @param array $data linklist data.
  34. *
  35. * @return mixed linklist data parsed in markdown (and converted to HTML).
  36. */
  37. function hook_markdown_render_feed($data)
  38. {
  39. foreach ($data['links'] as &$value) {
  40. if (!empty($value['tags']) && noMarkdownTag($value['tags'])) {
  41. $value['tags'] = stripNoMarkdownTag($value['tags']);
  42. continue;
  43. }
  44. $value['description'] = process_markdown($value['description']);
  45. }
  46. return $data;
  47. }
  48. /**
  49. * Parse daily descriptions.
  50. *
  51. * @param array $data daily data.
  52. *
  53. * @return mixed daily data parsed in markdown (and converted to HTML).
  54. */
  55. function hook_markdown_render_daily($data)
  56. {
  57. // Manipulate columns data
  58. foreach ($data['cols'] as &$value) {
  59. foreach ($value as &$value2) {
  60. if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) {
  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 strpos($tags, NO_MD_TAG) !== false;
  78. }
  79. /**
  80. * Remove the no-markdown meta tag so it won't be displayed.
  81. *
  82. * @param string $tags Tag list.
  83. *
  84. * @return string tag list without no markdown tag.
  85. */
  86. function stripNoMarkdownTag($tags)
  87. {
  88. unset($tags[array_search(NO_MD_TAG, $tags)]);
  89. return array_values($tags);
  90. }
  91. /**
  92. * When link list is displayed, include markdown CSS.
  93. *
  94. * @param array $data includes data.
  95. *
  96. * @return mixed - includes data with markdown CSS file added.
  97. */
  98. function hook_markdown_render_includes($data)
  99. {
  100. if ($data['_PAGE_'] == Router::$PAGE_LINKLIST
  101. || $data['_PAGE_'] == Router::$PAGE_DAILY
  102. || $data['_PAGE_'] == Router::$PAGE_EDITLINK
  103. ) {
  104. $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css';
  105. }
  106. return $data;
  107. }
  108. /**
  109. * Hook render_editlink.
  110. * Adds an help link to markdown syntax.
  111. *
  112. * @param array $data data passed to plugin
  113. *
  114. * @return array altered $data.
  115. */
  116. function hook_markdown_render_editlink($data)
  117. {
  118. // Load help HTML into a string
  119. $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html');
  120. // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion.
  121. if (! in_array(NO_MD_TAG, $data['tags'])) {
  122. $data['tags'][NO_MD_TAG] = 0;
  123. }
  124. return $data;
  125. }
  126. /**
  127. * Remove HTML links auto generated by Shaarli core system.
  128. * Keeps HREF attributes.
  129. *
  130. * @param string $description input description text.
  131. *
  132. * @return string $description without HTML links.
  133. */
  134. function reverse_text2clickable($description)
  135. {
  136. $descriptionLines = explode(PHP_EOL, $description);
  137. $descriptionOut = '';
  138. $codeBlockOn = false;
  139. $lineCount = 0;
  140. foreach ($descriptionLines as $descriptionLine) {
  141. // Detect line of code
  142. $codeLineOn = preg_match('/^ /', $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 hashtag links if we're in a code block.
  158. $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $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. }