From 1be4afacf98e0124258199ec416dc1c4b4948305 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 17 Nov 2015 21:01:11 +0100 Subject: PLUGIN Markdown Parse link description in Markdown (HTML) before rendering. * hard remove of Shaarli's HTML before parsing. * Using Parsedown PHP lib. * Includes basic markdown CSS. * Style: removed 400px height max limit for shaares. * Unit tests. --- plugins/markdown/markdown.php | 158 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 plugins/markdown/markdown.php (limited to 'plugins/markdown/markdown.php') diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php new file mode 100644 index 00000000..3630ef14 --- /dev/null +++ b/plugins/markdown/markdown.php @@ -0,0 +1,158 @@ +[^ ]+!m', '$1', $description); +} + +/** + * Remove
tag to let markdown handle it. + * + * @param string $description input description text. + * + * @return string $description without
tags. + */ +function reverse_nl2br($description) +{ + return preg_replace('!
!im', '', $description); +} + +/** + * Remove HTML spaces ' ' auto generated by Shaarli core system. + * + * @param string $description input description text. + * + * @return string $description without HTML links. + */ +function reverse_space2nbsp($description) +{ + return preg_replace('/(^| ) /m', '$1 ', $description); +} + +/** + * Remove '>' at start of line auto generated by Shaarli core system + * to allow markdown blockquotes. + * + * @param string $description input description text. + * + * @return string $description without HTML links. + */ +function reset_quote_tags($description) +{ + return preg_replace('/^( *)> /m', '$1> ', $description); +} + +/** + * Render shaare contents through Markdown parser. + * 1. Remove HTML generated by Shaarli core. + * 2. Generate markdown descriptions. + * 3. Wrap description in 'markdown' CSS class. + * + * @param string $description input description text. + * + * @return string HTML processed $description. + */ +function process_markdown($description) +{ + $parsedown = new Parsedown(); + + $processedDescription = $description; + $processedDescription = reverse_text2clickable($processedDescription); + $processedDescription = reverse_nl2br($processedDescription); + $processedDescription = reverse_space2nbsp($processedDescription); + $processedDescription = reset_quote_tags($processedDescription); + $processedDescription = $parsedown + ->setMarkupEscaped(false) + ->setBreaksEnabled(true) + ->text($processedDescription); + $processedDescription = '
'. $processedDescription . '
'; + + return $processedDescription; +} -- cgit v1.2.3