diff --git a/include/items.php b/include/items.php index 88eec1dc4..a28e19a05 100644 --- a/include/items.php +++ b/include/items.php @@ -24,7 +24,8 @@ use Friendica\Util\ParseUrl; use Friendica\Util\Strings; use Friendica\Util\Temporal; -require_once 'mod/share.php'; +require_once __DIR__ . '/../mod/share.php'; + function add_page_info_data(array $data, $no_photos = false) { Hook::callAll('page_info_data', $data); diff --git a/src/BaseObject.php b/src/BaseObject.php index 4a6fa12d2..7e90478a9 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -4,7 +4,7 @@ */ namespace Friendica; -require_once 'boot.php'; +require_once __DIR__ . '/../boot.php'; use Friendica\Network\HTTPException\InternalServerErrorException; diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 2c324a433..2d1c681f4 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1268,24 +1268,7 @@ class BBCode extends BaseObject // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text if (!$for_plaintext) { - // Autolink feature (thanks to https://daringfireball.net/2010/07/improved_regex_for_matching_urls) - $autolink_regex = '@(?xi) -(?]+ # Run of non-space, non-()<> - | # or - \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels - | # or - [^\s`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars - )* -)@'; - $text = preg_replace($autolink_regex, '[url]$1[/url]', $text); + $text = preg_replace(Strings::autoLinkRegEx(), '[url]$1[/url]', $text); if ($simple_html == 7) { $text = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); $text = preg_replace_callback("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text); diff --git a/src/Content/Text/Markdown.php b/src/Content/Text/Markdown.php index e3e2cd3ac..a715de7e9 100644 --- a/src/Content/Text/Markdown.php +++ b/src/Content/Text/Markdown.php @@ -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('/ERROR: folder view/smarty3/ must be writable by webserver."; exit(); } diff --git a/src/Util/Strings.php b/src/Util/Strings.php index 55751d8d8..3edc9ba90 100644 --- a/src/Util/Strings.php +++ b/src/Util/Strings.php @@ -346,4 +346,30 @@ class Strings return $return; } + + /** + * Returns the regular expression string to match URLs in a given text + * + * @return string + * @see https://daringfireball.net/2010/07/improved_regex_for_matching_urls + */ + public static function autoLinkRegEx() + { + return '@(?xi) +(?]+ # Run of non-space, non-()<> + | # or + \(([^\s()<>]+|(\([^\s()<>]+\)))*\) # balanced parens, up to 2 levels + | # or + [^\s`!()\[\]{};:\'".,<>?«»“”‘’] # not a space or one of these punct chars + )* +)@'; + } } diff --git a/tests/datasets/content/text/markdown/bug-6633.html b/tests/datasets/content/text/markdown/bug-6633.html new file mode 100644 index 000000000..313194e02 --- /dev/null +++ b/tests/datasets/content/text/markdown/bug-6633.html @@ -0,0 +1,9 @@ +

Produção de cebola em sistema orgânico com uso de homeopatia

+ +

Bulbos de cebola em sistema orgânico na fase de colheita
+

+ +

https://mapadaagroecologia.org/locais/epagri-estacao-experimental-de-ituporanga-sc?locale=pt-BR

+ +

#agroecologia #ecologia #orgânico #agroecology #brazil

diff --git a/tests/datasets/content/text/markdown/bug-6633.md b/tests/datasets/content/text/markdown/bug-6633.md new file mode 100644 index 000000000..330d49167 --- /dev/null +++ b/tests/datasets/content/text/markdown/bug-6633.md @@ -0,0 +1,9 @@ +# Produção de cebola em sistema orgânico com uso de homeopatia + +![Bulbos de cebola em sistema orgânico na fase de colheita +](https://mapadaagroecologia.org/system/midias/imagems/000/000/097/original/Cebola_em_sistema_org%C3%A2nico.jpg?1549640469 "Bulbos de cebola em sistema orgânico na fase de colheita +") + +## https://mapadaagroecologia.org/locais/epagri-estacao-experimental-de-ituporanga-sc?locale=pt-BR + +#agroecologia #ecologia #orgânico #agroecology #brazil \ No newline at end of file diff --git a/tests/src/Content/Text/MarkdownTest.php b/tests/src/Content/Text/MarkdownTest.php new file mode 100644 index 000000000..e39b46b2c --- /dev/null +++ b/tests/src/Content/Text/MarkdownTest.php @@ -0,0 +1,52 @@ +setUpVfsDir(); + $this->mockApp($this->root); + } + + public function dataMarkdown() + { + $inputFiles = glob(__DIR__ . '/../../../datasets/content/text/markdown/*.md'); + + $data = []; + + foreach ($inputFiles as $file) { + $data[str_replace('.md', '', $file)] = [ + 'input' => file_get_contents($file), + 'expected' => file_get_contents(str_replace('.md', '.html', $file)) + ]; + } + + return $data; + } + + /** + * Test convert different input Markdown text into HTML + * @dataProvider dataMarkdown + * + * @param string $input The Markdown text to test + * @param string $expected The expected HTML output + * @throws \Exception + */ + public function testConvert($input, $expected) + { + $output = Markdown::convert($input); + + $this->assertEquals($expected, $output); + } +} \ No newline at end of file