]> git.mxchange.org Git - friendica-addons.git/blob - markdown/markdown.php
faa1b3563f62089c8987a5e442450e57a64d7ae7
[friendica-addons.git] / markdown / markdown.php
1 <?php
2 /**
3  * Name: Markdown
4  * Description: Parse Markdown code when creating new items
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8 use Friendica\App;
9 use Friendica\Core\Hook;
10 use Friendica\Content\Text\Markdown;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\Session;
13 use Friendica\DI;
14
15 function markdown_install() {
16         Hook::register('post_local_start',      __FILE__, 'markdown_post_local_start');
17         Hook::register('addon_settings',        __FILE__, 'markdown_addon_settings');
18         Hook::register('addon_settings_post',   __FILE__, 'markdown_addon_settings_post');
19 }
20
21 function markdown_addon_settings(App $a, array &$data)
22 {
23         if (!Session::getLocalUser()) {
24                 return;
25         }
26
27         $enabled = intval(DI::pConfig()->get(Session::getLocalUser(), 'markdown', 'enabled'));
28
29         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
30         $html = Renderer::replaceMacros($t, [
31                 '$enabled' => ['enabled', DI::l10n()->t('Enable Markdown parsing'), $enabled, DI::l10n()->t('If enabled, adds Markdown support to the Compose Post form.')],
32         ]);
33
34         $data = [
35                 'addon' => 'markdown',
36                 'title' => DI::l10n()->t('Markdown Settings'),
37                 'html'  => $html,
38         ];
39 }
40
41 function markdown_addon_settings_post(App $a, array &$b)
42 {
43         if (!Session::getLocalUser() || empty($_POST['markdown-submit'])) {
44                 return;
45         }
46
47         DI::pConfig()->set(Session::getLocalUser(), 'markdown', 'enabled', intval($_POST['enabled']));
48 }
49
50 function markdown_post_local_start(App $a, &$request) {
51         if (empty($request['body']) || !DI::pConfig()->get(Session::getLocalUser(), 'markdown', 'enabled')) {
52                 return;
53         }
54
55         // Escape elements that shouldn't be parsed
56         $request['body'] = \Friendica\Content\Text\BBCode::performWithEscapedTags(
57                 $request['body'],
58                 ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
59                         'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'],
60                 function ($body) {
61                         // Escape mentions which username can contain Markdown-like characters
62                         // See https://github.com/friendica/friendica/issues/9486
63                         return \Friendica\Util\Strings::performWithEscapedBlocks($body, '/[@!][^@\s]+@[^\s]+\w/', function ($text) {
64                                 // Markdown accepts literal HTML but we do not in post body, so we need to escape left chevrons
65                                 // (right chevrons are used for quoting in Markdown)
66                                 // See https://github.com/friendica/friendica/issues/10634
67                                 $text = strtr($text, ['<' => '&lt;']);
68
69                                 return Markdown::toBBCode($text);
70                         });
71                 }
72         );
73 }