IT translation update blackout addon THX Sylke Vicious
[friendica-addons.git/.git] / markdown / markdown.php
index 854bc68..4f12e71 100644 (file)
@@ -7,11 +7,8 @@
  */
 use Friendica\App;
 use Friendica\Core\Hook;
-use Friendica\Core\Logger;
 use Friendica\Content\Text\Markdown;
 use Friendica\Core\Renderer;
-use Friendica\Core\PConfig;
-use Friendica\Core\L10n;
 use Friendica\DI;
 
 function markdown_install() {
@@ -30,9 +27,9 @@ function markdown_addon_settings(App $a, &$s)
 
        $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
        $s .= Renderer::replaceMacros($t, [
-               '$title'   => L10n::t('Markdown'),
-               '$enabled' => ['enabled', L10n::t('Enable Markdown parsing'), $enabled, L10n::t('If enabled, self created items will additionally be parsed via Markdown.')],
-               '$submit'  => L10n::t('Save Settings'),
+               '$title'   => DI::l10n()->t('Markdown'),
+               '$enabled' => ['enabled', DI::l10n()->t('Enable Markdown parsing'), $enabled, DI::l10n()->t('If enabled, self created items will additionally be parsed via Markdown.')],
+               '$submit'  => DI::l10n()->t('Save Settings'),
        ]);
 }
 
@@ -50,26 +47,17 @@ function markdown_post_local_start(App $a, &$request) {
                return;
        }
 
-       // Elements that shouldn't be parsed
-       $elements = ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
-               'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'];
-       foreach ($elements as $element) {
-               $request['body'] = preg_replace_callback("/\[" . $element . "(.*?)\](.*?)\[\/" . $element . "\]/ism",
-                       function ($match) use ($element) {
-                               return '[' . $element . '-b64' . base64_encode($match[1]) . ']' . base64_encode($match[2]) . '[/b64-' . $element . ']';
-                       },
-                       $request['body']
-               );
-       }
-
-       $request['body'] = Markdown::toBBCode($request['body']);
-
-       foreach (array_reverse($elements) as $element) {
-               $request['body'] = preg_replace_callback("/\[" . $element . "-b64(.*?)\](.*?)\[\/b64-" . $element . "\]/ism",
-                       function ($match) use ($element) {
-                               return '[' . $element . base64_decode($match[1]) . ']' . base64_decode($match[2]) . '[/' . $element . ']';
-                       },
-                       $request['body']
-               );
-       }
+       // Escape elements that shouldn't be parsed
+       $request['body'] = \Friendica\Content\Text\BBCode::performWithEscapedTags(
+               $request['body'],
+               ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
+                       'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'],
+               function ($body) {
+                       // Escape mentions which username can contain Markdown-like characters
+                       // See https://github.com/friendica/friendica/issues/9486
+                       return \Friendica\Util\Strings::performWithEscapedBlocks($body, '/[@!][^@\s]+@[^\s]+\w/', function ($text) {
+                               return Markdown::toBBCode($text);
+                       });
+               }
+       );
 }