4 * @file src/Content/Text/Markdown.php
7 namespace Friendica\Content\Text;
9 use Friendica\BaseObject;
10 use Friendica\Core\System;
11 use Friendica\Model\Contact;
12 use Michelf\MarkdownExtra;
15 * Friendica-specific usage of Markdown
17 * @author Hypolite Petovan <hypolite@mrpetovan.com>
19 class Markdown extends BaseObject
22 * Converts a Markdown string into HTML. The hardwrap parameter maximizes
23 * compatibility with Diaspora in spite of the Markdown standard.
25 * @brief Converts a Markdown string into HTML
27 * @param bool $hardwrap
31 public static function convert($text, $hardwrap = true) {
32 $stamp1 = microtime(true);
34 $MarkdownParser = new MarkdownExtra();
35 $MarkdownParser->hard_wrap = $hardwrap;
36 $MarkdownParser->code_class_prefix = 'language-';
37 $html = $MarkdownParser->transform($text);
38 $html = preg_replace('/<a(.*?)href="#/is', '<a$1href="' . ltrim($_SERVER['REQUEST_URI'], '/') . '#', $html);
40 self::getApp()->getProfiler()->saveTimestamp($stamp1, "parser", System::callstack());
46 * @brief Callback function to replace a Diaspora style mention in a mention for Friendica
48 * @param array $match Matching values for the callback
49 * [1] = mention type (@ or !)
50 * [2] = name (optional)
52 * @return string Replaced mention
53 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
54 * @throws \ImagickException
56 private static function diasporaMention2BBCodeCallback($match)
58 if ($match[3] == '') {
62 $data = Contact::getDetailsByAddr($match[3]);
71 $name = $data['name'];
74 return $match[1] . '[url=' . $data['url'] . ']' . $name . '[/url]';
78 * we don't want to support a bbcode specific markdown interpreter
79 * and the markdown library we have is pretty good, but provides HTML output.
80 * So we'll use that to convert to HTML, then convert the HTML back to bbcode,
81 * and then clean up a few Diaspora specific constructs.
83 public static function toBBCode($s)
85 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
87 // The parser cannot handle paragraphs correctly
88 $s = str_replace(['</p>', '<p>', '<p dir="ltr">'], ['<br>', '<br>', '<br>'], $s);
90 // Escaping hashtags that could be titles
91 $s = preg_replace('/^\#([^\s\#])/im', '\#$1', $s);
93 $s = self::convert($s);
95 $regexp = "/([@!])\{(?:([^\}]+?); ?)?([^\} ]+)\}/";
96 $s = preg_replace_callback($regexp, ['self', 'diasporaMention2BBCodeCallback'], $s);
98 $s = HTML::toBBCode($s);
100 // protect the recycle symbol from turning into a tag, but without unescaping angles and naked ampersands
101 $s = str_replace('♲', html_entity_decode('♲', ENT_QUOTES, 'UTF-8'), $s);
103 // Convert everything that looks like a link to a link
104 $s = preg_replace('/([^\]=]|^)(https?\:\/\/)([a-zA-Z0-9:\/\-?&;.=_~#%$!+,@]+(?<!,))/ism', '$1[url=$2$3]$2$3[/url]', $s);
106 //$s = preg_replace("/([^\]\=]|^)(https?\:\/\/)(vimeo|youtu|www\.youtube|soundcloud)([a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2$3$4]$2$3$4[/url]',$s);
107 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism', '[youtube]$2[/youtube]', 'url', $s);
108 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/www.youtube.com\/watch\?v\=(.*?)\].*?\[\/url\]/ism' , '[youtube]$1[/youtube]', 'url', $s);
109 $s = BBCode::pregReplaceInTag('/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism' , '[vimeo]$2[/vimeo]' , 'url', $s);
110 $s = BBCode::pregReplaceInTag('/\[url\=https?:\/\/vimeo.com\/([0-9]+)\](.*?)\[\/url\]/ism' , '[vimeo]$1[/vimeo]' , 'url', $s);
112 // remove duplicate adjacent code tags
113 $s = preg_replace('/(\[code\])+(.*?)(\[\/code\])+/ism', '[code]$2[/code]', $s);
115 // Don't show link to full picture (until it is fixed)
116 $s = BBCode::scaleExternalImages($s, false);