3 namespace Shaarli\Formatter
;
5 use Shaarli\Config\ConfigManager
;
8 * Class BookmarkMarkdownFormatter
10 * Format bookmark description into Markdown format.
12 * @package Shaarli\Formatter
14 class BookmarkMarkdownFormatter
extends BookmarkDefaultFormatter
17 * When this tag is present in a bookmark, its description should not be processed with Markdown
19 const NO_MD_TAG
= 'nomarkdown';
21 /** @var \Parsedown instance */
24 /** @var bool used to escape HTML in Markdown or not.
25 * It MUST be set to true for shared instance as HTML content can
26 * introduce XSS vulnerabilities.
31 * @var array List of allowed protocols for links inside bookmark's description.
33 protected $allowedProtocols;
36 * LinkMarkdownFormatter constructor.
38 * @param ConfigManager $conf instance
40 public function __construct(ConfigManager
$conf)
42 parent
::__construct($conf);
43 $this->parsedown
= new \
Parsedown();
44 $this->escape
= $conf->get('security.markdown_escape', true);
45 $this->allowedProtocols
= $conf->get('security.allowed_protocols', []);
51 public function formatDescription($bookmark)
53 if (in_array(self
::NO_MD_TAG
, $bookmark->getTags())) {
54 return parent
::formatDescription($bookmark);
57 $processedDescription = $bookmark->getDescription();
58 $processedDescription = $this->filterProtocols($processedDescription);
59 $processedDescription = $this->formatHashTags($processedDescription);
60 $processedDescription = $this->reverseEscapedHtml($processedDescription);
61 $processedDescription = $this->parsedown
62 ->setMarkupEscaped($this->escape
)
63 ->setBreaksEnabled(true)
64 ->text($processedDescription);
65 $processedDescription = $this->sanitizeHtml($processedDescription);
67 if (!empty($processedDescription)) {
68 $processedDescription = '<div class="markdown">'. $processedDescription . '</div>';
71 return $processedDescription;
75 * Remove the NO markdown tag if it is present
79 protected function formatTagList($bookmark)
81 $out = parent
::formatTagList($bookmark);
82 if (($pos = array_search(self
::NO_MD_TAG
, $out)) !== false) {
84 return array_values($out);
90 * Replace not whitelisted protocols with http:// in given description.
91 * Also adds `index_url` to relative links if it's specified
93 * @param string $description input description text.
95 * @return string $description without malicious link.
97 protected function filterProtocols($description)
99 $allowedProtocols = $this->allowedProtocols
;
100 $indexUrl = ! empty($this->contextData
['index_url']) ? $this->contextData
['index_url'] : '';
102 return preg_replace_callback(
104 function ($match) use ($allowedProtocols, $indexUrl) {
105 $link = startsWith($match[1], '?') || startsWith($match[1], '/') ? $indexUrl : '';
106 $link .= whitelist_protocols($match[1], $allowedProtocols);
107 return ']('. $link.')';
114 * Replace hashtag in Markdown links format
115 * E.g. `#hashtag` becomes `[#hashtag](?addtag=hashtag)`
116 * It includes the index URL if specified.
118 * @param string $description
122 protected function formatHashTags($description)
124 $indexUrl = ! empty($this->contextData
['index_url']) ? $this->contextData
['index_url'] : '';
127 * To support unicode: http://stackoverflow.com/a/35498078/1484919
128 * \p{Pc} - to match underscore
129 * \p{N} - numeric character in any script
130 * \p{L} - letter from any language
131 * \p{Mn} - any non marking space (accents, umlauts, etc)
133 $regex = '/(^|\s)#([\p{Pc}\p{N}\p{L}\p{Mn}]+)/mui';
134 $replacement = '$1[#$2]('. $indexUrl .'?addtag=$2)';
136 $descriptionLines = explode(PHP_EOL
, $description);
137 $descriptionOut = '';
138 $codeBlockOn = false;
141 foreach ($descriptionLines as $descriptionLine) {
142 // Detect line of code: starting with 4 spaces,
143 // except lists which can start with +/*/- or `2.` after spaces.
144 $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0;
145 // Detect and toggle block of code
147 $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0;
148 } elseif (preg_match('/^```/', $descriptionLine) > 0) {
149 $codeBlockOn = false;
152 if (!$codeBlockOn && !$codeLineOn) {
153 $descriptionLine = preg_replace($regex, $replacement, $descriptionLine);
156 $descriptionOut .= $descriptionLine;
157 if ($lineCount++
< count($descriptionLines) - 1) {
158 $descriptionOut .= PHP_EOL
;
162 return $descriptionOut;
166 * Remove dangerous HTML tags (tags, iframe, etc.).
167 * Doesn't affect <code> content (already escaped by Parsedown).
169 * @param string $description input description text.
171 * @return string given string escaped.
173 protected function sanitizeHtml($description)
183 foreach ($escapeTags as $tag) {
184 $description = preg_replace_callback(
185 '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is',
187 return escape($match[0]);
192 $description = preg_replace(
193 '#(<[^>]+\s)on[a-z]*="?[^ "]*"?#is',
200 protected function reverseEscapedHtml($description)
202 return unescape($description);