3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Text;
24 use Friendica\Core\Logger;
26 use Friendica\Model\Contact;
29 * Friendica-specific usage of Markdown
34 * Converts a Markdown string into HTML. The hardwrap parameter maximizes
35 * compatibility with Diaspora in spite of the Markdown standard.
38 * @param bool $hardwrap Enables line breaks on \n without two trailing spaces
39 * @param string $baseuri Optional. Prepend anchor links with this URL
42 public static function convert($text, $hardwrap = true, $baseuri = null) {
43 DI::profiler()->startRecording('rendering');
45 $MarkdownParser = new MarkdownParser();
46 $MarkdownParser->code_class_prefix = 'language-';
47 $MarkdownParser->hard_wrap = $hardwrap;
48 $MarkdownParser->hashtag_protection = true;
49 $MarkdownParser->url_filter_func = function ($url) use ($baseuri) {
50 if (!empty($baseuri) && strpos($url, '#') === 0) {
51 $url = ltrim($baseuri, '/') . $url;
56 $text = self::convertDiasporaMentionsToHtml($text);
58 $html = $MarkdownParser->transform($text);
60 DI::profiler()->stopRecording();
66 * Replace Diaspora-style mentions in a text since they trip the Markdown parser autolinker.
71 private static function convertDiasporaMentionsToHtml(string $text)
73 return preg_replace_callback(
74 '/([@!]){(?:([^}]+?); ?)?([^} ]+)}/',
76 * Matching values for the callback
77 * [1] = mention type (@ or !)
78 * [2] = name (optional)
82 if ($matches[3] == '') {
86 $data = Contact::getByURL($matches[3]);
95 $name = $data['name'];
98 return $matches[1] . '<a href="' . $data['url'] . '">' . $name . '</a>';
105 * we don't want to support a bbcode specific markdown interpreter
106 * and the markdown library we have is pretty good, but provides HTML output.
107 * So we'll use that to convert to HTML, then convert the HTML back to bbcode,
108 * and then clean up a few Diaspora specific constructs.
110 public static function toBBCode($s): string
112 // @TODO Temporary until we find the source of the null value to finally set the correct type-hint
114 Logger::warning('Received null value');
122 DI::profiler()->startRecording('rendering');
124 // The parser cannot handle paragraphs correctly
125 $s = str_replace(['</p>', '<p>', '<p dir="ltr">'], ['<br>', '<br>', '<br>'], $s);
127 // Escaping hashtags that could be titles
128 $s = preg_replace('/^\#([^\s\#])/im', '\#$1', $s);
130 $s = self::convert($s);
132 $s = HTML::toBBCode($s);
134 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
135 $s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s);
137 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
138 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
139 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism' , '[youtube]$1[/youtube]', 'url', $s);
140 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/shorts\/(.*?)\].*?\[\/url\]/ism' , '[youtube]$1[/youtube]', 'url', $s);
141 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism' , '[vimeo]$2[/vimeo]' , 'url', $s);
142 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism' , '[vimeo]$1[/vimeo]' , 'url', $s);
144 // remove duplicate adjacent code tags
145 $s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
147 DI::profiler()->stopRecording();