f02bc2a017f208b878e08bd72be7b8ae1c7def76
[friendica-addons.git/.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\DI;
13
14 function markdown_install() {
15         Hook::register('post_local_start',      __FILE__, 'markdown_post_local_start');
16         Hook::register('addon_settings',        __FILE__, 'markdown_addon_settings');
17         Hook::register('addon_settings_post',   __FILE__, 'markdown_addon_settings_post');
18 }
19
20 function markdown_addon_settings(App $a, &$s)
21 {
22         if (!local_user()) {
23                 return;
24         }
25
26         $enabled = intval(DI::pConfig()->get(local_user(), 'markdown', 'enabled'));
27
28         $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
29         $s .= Renderer::replaceMacros($t, [
30                 '$title'   => DI::l10n()->t('Markdown'),
31                 '$enabled' => ['enabled', DI::l10n()->t('Enable Markdown parsing'), $enabled, DI::l10n()->t('If enabled, self created items will additionally be parsed via Markdown.')],
32                 '$submit'  => DI::l10n()->t('Save Settings'),
33         ]);
34 }
35
36 function markdown_addon_settings_post(App $a, &$b)
37 {
38         if (!local_user() || empty($_POST['markdown-submit'])) {
39                 return;
40         }
41
42         DI::pConfig()->set(local_user(), 'markdown', 'enabled', intval($_POST['enabled']));
43 }
44
45 function markdown_post_local_start(App $a, &$request) {
46         if (empty($request['body']) || !DI::pConfig()->get(local_user(), 'markdown', 'enabled')) {
47                 return;
48         }
49
50         // Elements that shouldn't be parsed
51         $elements = ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
52                 'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'];
53         foreach ($elements as $element) {
54                 $request['body'] = preg_replace_callback("/\[" . $element . "(.*?)\](.*?)\[\/" . $element . "\]/ism",
55                         function ($match) use ($element) {
56                                 return '[' . $element . '-b64' . base64_encode($match[1]) . ']' . base64_encode($match[2]) . '[/b64-' . $element . ']';
57                         },
58                         $request['body']
59                 );
60         }
61
62         $request['body'] = Markdown::toBBCode($request['body']);
63
64         foreach (array_reverse($elements) as $element) {
65                 $request['body'] = preg_replace_callback("/\[" . $element . "-b64(.*?)\](.*?)\[\/b64-" . $element . "\]/ism",
66                         function ($match) use ($element) {
67                                 return '[' . $element . base64_decode($match[1]) . ']' . base64_decode($match[2]) . '[/' . $element . ']';
68                         },
69                         $request['body']
70                 );
71         }
72 }