]> git.mxchange.org Git - friendica.git/commitdiff
Add new Content\Text\MarkdownParser
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 10 Mar 2019 04:28:50 +0000 (23:28 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 10 Mar 2019 04:28:50 +0000 (23:28 -0500)
- Add autolinker to MarkdownParser->doAutoLinks()
- Set hashtag_protection and url_filter_func properties of MarkdownParser

src/Content/Text/Markdown.php
src/Content/Text/MarkdownParser.php [new file with mode: 0644]

index e3e2cd3ac957750e56f04205179611a3038aac72..a715de7e97e1debba48f30ebe558864bd42962b9 100644 (file)
@@ -9,7 +9,6 @@ namespace Friendica\Content\Text;
 use Friendica\BaseObject;
 use Friendica\Core\System;
 use Friendica\Model\Contact;
-use Michelf\MarkdownExtra;
 
 /**
  * Friendica-specific usage of Markdown
@@ -31,11 +30,18 @@ class Markdown extends BaseObject
        public static function convert($text, $hardwrap = true) {
                $stamp1 = microtime(true);
 
-               $MarkdownParser = new MarkdownExtra();
-               $MarkdownParser->hard_wrap = $hardwrap;
-               $MarkdownParser->code_class_prefix = 'language-';
+               $MarkdownParser = new MarkdownParser();
+               $MarkdownParser->code_class_prefix  = 'language-';
+               $MarkdownParser->hard_wrap          = $hardwrap;
+               $MarkdownParser->hashtag_protection = true;
+               $MarkdownParser->url_filter_func    = function ($url) {
+                       if (strpos($url, '#') === 0) {
+                               $url = ltrim($_SERVER['REQUEST_URI'], '/') . $url;
+                       }
+                       return  $url;
+               };
+
                $html = $MarkdownParser->transform($text);
-               $html = preg_replace('/<a(.*?)href="#/is', '<a$1href="' . ltrim($_SERVER['REQUEST_URI'], '/') . '#', $html);
 
                self::getApp()->getProfiler()->saveTimestamp($stamp1, "parser", System::callstack());
 
diff --git a/src/Content/Text/MarkdownParser.php b/src/Content/Text/MarkdownParser.php
new file mode 100644 (file)
index 0000000..fa1d166
--- /dev/null
@@ -0,0 +1,18 @@
+<?php\r
+namespace Friendica\Content\Text;\r
+\r
+use Friendica\Util\Strings;\r
+use Michelf\MarkdownExtra;\r
+\r
+class MarkdownParser extends MarkdownExtra\r
+{\r
+       protected function doAutoLinks($text)\r
+       {\r
+               $text = parent::doAutoLinks($text);\r
+\r
+               $text = preg_replace_callback(Strings::autoLinkRegEx(),\r
+                       array($this, '_doAutoLinks_url_callback'), $text);\r
+\r
+               return $text;\r
+       }\r
+}
\ No newline at end of file