4 * Description: Parse Markdown code when creating new items
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 use Friendica\Core\Hook;
10 use Friendica\Content\Text\Markdown;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\L10n;
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');
21 function markdown_addon_settings(App $a, &$s)
27 $enabled = intval(DI::pConfig()->get(local_user(), 'markdown', 'enabled'));
29 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
30 $s .= Renderer::replaceMacros($t, [
31 '$title' => L10n::t('Markdown'),
32 '$enabled' => ['enabled', L10n::t('Enable Markdown parsing'), $enabled, L10n::t('If enabled, self created items will additionally be parsed via Markdown.')],
33 '$submit' => L10n::t('Save Settings'),
37 function markdown_addon_settings_post(App $a, &$b)
39 if (!local_user() || empty($_POST['markdown-submit'])) {
43 DI::pConfig()->set(local_user(), 'markdown', 'enabled', intval($_POST['enabled']));
46 function markdown_post_local_start(App $a, &$request) {
47 if (empty($request['body']) || !DI::pConfig()->get(local_user(), 'markdown', 'enabled')) {
51 // Elements that shouldn't be parsed
52 $elements = ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
53 'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'];
54 foreach ($elements as $element) {
55 $request['body'] = preg_replace_callback("/\[" . $element . "(.*?)\](.*?)\[\/" . $element . "\]/ism",
56 function ($match) use ($element) {
57 return '[' . $element . '-b64' . base64_encode($match[1]) . ']' . base64_encode($match[2]) . '[/b64-' . $element . ']';
63 $request['body'] = Markdown::toBBCode($request['body']);
65 foreach (array_reverse($elements) as $element) {
66 $request['body'] = preg_replace_callback("/\[" . $element . "-b64(.*?)\](.*?)\[\/b64-" . $element . "\]/ism",
67 function ($match) use ($element) {
68 return '[' . $element . base64_decode($match[1]) . ']' . base64_decode($match[2]) . '[/' . $element . ']';