diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-05-10 23:18:04 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-06-06 21:04:43 +0200 |
commit | 9ccca40189652e529732683abcdf54fcf775c9ec (patch) | |
tree | 9deda85d287dcba664bbba2f0bf9228e6118fbad /plugins/markdown/markdown.php | |
parent | bb9ca54838e2f877635197541e8439171c83d5dc (diff) | |
download | Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.tar.gz Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.tar.zst Shaarli-9ccca40189652e529732683abcdf54fcf775c9ec.zip |
Hashtag system
* Hashtag are auto-linked with a filter search
* Supports unicode
* Compatible with markdown (excluded in code blocks)
Diffstat (limited to 'plugins/markdown/markdown.php')
-rw-r--r-- | plugins/markdown/markdown.php | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php index 5f56ecc2..6b1c1d44 100644 --- a/plugins/markdown/markdown.php +++ b/plugins/markdown/markdown.php | |||
@@ -151,7 +151,44 @@ function hook_markdown_render_editlink($data) | |||
151 | */ | 151 | */ |
152 | function reverse_text2clickable($description) | 152 | function reverse_text2clickable($description) |
153 | { | 153 | { |
154 | return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description); | 154 | $descriptionLines = explode(PHP_EOL, $description); |
155 | $descriptionOut = ''; | ||
156 | $codeBlockOn = false; | ||
157 | $lineCount = 0; | ||
158 | |||
159 | foreach ($descriptionLines as $descriptionLine) { | ||
160 | // Detect line of code | ||
161 | $codeLineOn = preg_match('/^ /', $descriptionLine) > 0; | ||
162 | // Detect and toggle block of code | ||
163 | if (!$codeBlockOn) { | ||
164 | $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0; | ||
165 | } | ||
166 | elseif (preg_match('/^```/', $descriptionLine) > 0) { | ||
167 | $codeBlockOn = false; | ||
168 | } | ||
169 | |||
170 | $hashtagTitle = ' title="Hashtag [^"]+"'; | ||
171 | // Reverse `inline code` hashtags. | ||
172 | $descriptionLine = preg_replace( | ||
173 | '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m', | ||
174 | '$1$2$3', | ||
175 | $descriptionLine | ||
176 | ); | ||
177 | |||
178 | // Reverse hashtag links if we're in a code block. | ||
179 | $hashtagFilter = ($codeBlockOn || $codeLineOn) ? $hashtagTitle : ''; | ||
180 | $descriptionLine = preg_replace( | ||
181 | '!<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>!m', | ||
182 | '$1', | ||
183 | $descriptionLine | ||
184 | ); | ||
185 | |||
186 | $descriptionOut .= $descriptionLine; | ||
187 | if ($lineCount++ < count($descriptionLines) - 1) { | ||
188 | $descriptionOut .= PHP_EOL; | ||
189 | } | ||
190 | } | ||
191 | return $descriptionOut; | ||
155 | } | 192 | } |
156 | 193 | ||
157 | /** | 194 | /** |
@@ -226,9 +263,9 @@ function process_markdown($description) | |||
226 | $parsedown = new Parsedown(); | 263 | $parsedown = new Parsedown(); |
227 | 264 | ||
228 | $processedDescription = $description; | 265 | $processedDescription = $description; |
229 | $processedDescription = reverse_text2clickable($processedDescription); | ||
230 | $processedDescription = reverse_nl2br($processedDescription); | 266 | $processedDescription = reverse_nl2br($processedDescription); |
231 | $processedDescription = reverse_space2nbsp($processedDescription); | 267 | $processedDescription = reverse_space2nbsp($processedDescription); |
268 | $processedDescription = reverse_text2clickable($processedDescription); | ||
232 | $processedDescription = unescape($processedDescription); | 269 | $processedDescription = unescape($processedDescription); |
233 | $processedDescription = $parsedown | 270 | $processedDescription = $parsedown |
234 | ->setMarkupEscaped(false) | 271 | ->setMarkupEscaped(false) |