New Addon Markdown
authorMichael <heluecht@pirati.ca>
Thu, 26 Dec 2019 08:14:04 +0000 (08:14 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 26 Dec 2019 08:14:04 +0000 (08:14 +0000)
markdown/README [new file with mode: 0644]
markdown/markdown.php [new file with mode: 0644]
markdown/templates/settings.tpl [new file with mode: 0644]

diff --git a/markdown/README b/markdown/README
new file mode 100644 (file)
index 0000000..da3cd53
--- /dev/null
@@ -0,0 +1,5 @@
+Markdown
+========
+
+The Markdown addon parses user input for new items and comments via the Markdown parser.
+This enables users to use the Markdown syntax additionally to the BBCode syntax.
diff --git a/markdown/markdown.php b/markdown/markdown.php
new file mode 100644 (file)
index 0000000..a5358d0
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Name: Markdown
+ * Description: Parse Markdown code when creating new items
+ * Version: 0.1
+ * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
+ */
+use Friendica\App;
+use Friendica\Core\Hook;
+use Friendica\Core\Logger;
+use Friendica\Content\Text\Markdown;
+use Friendica\Core\Renderer;
+use Friendica\Core\PConfig;
+use Friendica\Core\L10n;
+
+function markdown_install() {
+       Hook::register('post_local_start',      __FILE__, 'markdown_post_local_start');
+       Hook::register('addon_settings',        __FILE__, 'markdown_addon_settings');
+       Hook::register('addon_settings_post',   __FILE__, 'markdown_addon_settings_post');
+}
+
+function markdown_addon_settings(App $a, &$s)
+{
+       if (!local_user()) {
+               return;
+       }
+
+       $enabled = intval(PConfig::get(local_user(), 'markdown', 'enabled'));
+
+       $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
+       $s .= Renderer::replaceMacros($t, [
+               '$title'   => L10n::t('Markdown'),
+               '$enabled' => ['enabled', L10n::t('Enable Markdown parsing'), $enabled, L10n::t('If enabled, self created items will additionally be parsed via Markdown.')],
+               '$submit'  => L10n::t('Save Settings'),
+       ]);
+}
+
+function markdown_addon_settings_post(App $a, &$b)
+{
+       if (!local_user() || empty($_POST['markdown-submit'])) {
+               return;
+       }
+
+       PConfig::set(local_user(), 'markdown', 'enabled', intval($_POST['enabled']));
+}
+
+function markdown_post_local_start(App $a, &$request) {
+       if (empty($request['body']) || !PConfig::get(local_user(), 'markdown', 'enabled')) {
+               return;
+       }
+
+       $request['body'] = Markdown::toBBCode($request['body']);
+}
diff --git a/markdown/templates/settings.tpl b/markdown/templates/settings.tpl
new file mode 100644 (file)
index 0000000..3d01351
--- /dev/null
@@ -0,0 +1,15 @@
+<span id="settings_markdown_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose('settings_markdown_expanded'); openClose('settings_markdown_inflated');">
+       <h3>{{$title}}</h3>
+</span>
+<div id="settings_markdown_expanded" class="settings-block" style="display: none;">
+       <span class="fakelink" onclick="openClose('settings_markdown_expanded'); openClose('settings_markdown_inflated');">
+               <h3>{{$title}}</h3>
+       </span>
+
+       <div id="markdown-wrapper">
+               {{include file="field_checkbox.tpl" field=$enabled}}
+       </div>
+       <div class="settings-submit-wrapper" >
+               <input type="submit" id="markdown-submit" name="markdown-submit" class="settings-submit" value="{{$submit}}" />
+       </div>
+</div>