]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
ShaarliParsedown: add PHPDoc/comments 1696/head
authorArthurHoaro <arthur@hoa.ro>
Thu, 4 Feb 2021 09:53:23 +0000 (10:53 +0100)
committerArthurHoaro <arthur@hoa.ro>
Thu, 4 Feb 2021 09:53:23 +0000 (10:53 +0100)
application/formatter/Parsedown/ShaarliParsedown.php
application/formatter/Parsedown/ShaarliParsedownExtra.php
application/formatter/Parsedown/ShaarliParsedownTrait.php

index d577bdfa8a2fcf5759a5f1caee6fcf2f4daf955d..8eb48fda91b0450428f6fb90a4d7fd98ed025444 100644 (file)
@@ -4,6 +4,11 @@ declare(strict_types=1);
 
 namespace Shaarli\Formatter\Parsedown;
 
+/**
+ * Parsedown extension for Shaarli.
+ *
+ * Extension for both Parsedown and ParsedownExtra centralized in ShaarliParsedownTrait.
+ */
 class ShaarliParsedown extends \Parsedown
 {
     use ShaarliParsedownTrait;
index 92ad26ca70fdc44caabe2e3e26dd0ce3dcc7aa4c..15a35da479765c86b77edb3eae7c770c42773409 100644 (file)
@@ -4,6 +4,11 @@ declare(strict_types=1);
 
 namespace Shaarli\Formatter\Parsedown;
 
+/**
+ * ParsedownExtra extension for Shaarli.
+ *
+ * Extension for both Parsedown and ParsedownExtra centralized in ShaarliParsedownTrait.
+ */
 class ShaarliParsedownExtra extends \ParsedownExtra
 {
     use ShaarliParsedownTrait;
index e6f4dabb8ae8d751947e3d1d5a4f55213cbcdfc2..ed7b1747453f84c4c1fd87c15eb7f19a81fa3598 100644 (file)
@@ -6,24 +6,48 @@ namespace Shaarli\Formatter\Parsedown;
 
 use Shaarli\Formatter\BookmarkDefaultFormatter as Formatter;
 
+/**
+ * Trait used for Parsedown and ParsedownExtra extension.
+ *
+ * Extended:
+ *   - Format links properly in search context
+ */
 trait ShaarliParsedownTrait
 {
+    /**
+     * @inheritDoc
+     */
     protected function inlineLink($excerpt)
     {
         return $this->shaarliFormatLink(parent::inlineLink($excerpt), true);
     }
 
+    /**
+     * @inheritDoc
+     */
     protected function inlineUrl($excerpt)
     {
         return $this->shaarliFormatLink(parent::inlineUrl($excerpt), false);
     }
 
+    /**
+     * Properly format markdown link:
+     *   - remove highlight tags from HREF attribute
+     *   - (optional) add highlight tags to link caption
+     *
+     * @param array|null $link     Parsedown formatted link array.
+     *                             It can be empty.
+     * @param bool       $fullWrap Add highlight tags the whole link caption
+     *
+     * @return array|null
+     */
     protected function shaarliFormatLink(?array $link, bool $fullWrap): ?array
     {
+        // If open and clean search tokens are found in the link, process.
         if (
             is_array($link)
-            && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_OPEN) !== false
-            && strpos($link['element']['attributes']['href'], Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false
+            && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_OPEN) !== false
+            && strpos($link['element']['attributes']['href'] ?? '', Formatter::SEARCH_HIGHLIGHT_CLOSE) !== false
         ) {
             $link['element']['attributes']['href'] = $this->shaarliRemoveSearchTokens(
                 $link['element']['attributes']['href']
@@ -40,6 +64,13 @@ trait ShaarliParsedownTrait
         return $link;
     }
 
+    /**
+     * Remove open and close tags from provided string.
+     *
+     * @param string $entry input
+     *
+     * @return string Striped input
+     */
     protected function shaarliRemoveSearchTokens(string $entry): string
     {
         $entry = str_replace(Formatter::SEARCH_HIGHLIGHT_OPEN, '', $entry);