From 1d7ecd6dc242432eaadc7d13893dc01a5e95c0f7 Mon Sep 17 00:00:00 2001
From: kpaxman <kpaxman@uwaterloo.ca>
Date: Mon, 19 Apr 2021 17:10:22 -0400
Subject: [PATCH] Add basic anchor matcher

---
 src/Plugin/Linkit/Matcher/AnchorMatcher.php | 39 +++++++++++++++++++++
 tests/src/Kernel/LinkitAutocompleteTest.php | 15 ++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 src/Plugin/Linkit/Matcher/AnchorMatcher.php

diff --git a/src/Plugin/Linkit/Matcher/AnchorMatcher.php b/src/Plugin/Linkit/Matcher/AnchorMatcher.php
new file mode 100644
index 00000000..0f8230d9
--- /dev/null
+++ b/src/Plugin/Linkit/Matcher/AnchorMatcher.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\linkit\Plugin\Linkit\Matcher;
+
+use Drupal\Component\Utility\Html;
+use Drupal\linkit\MatcherBase;
+use Drupal\linkit\Suggestion\DescriptionSuggestion;
+use Drupal\linkit\Suggestion\SuggestionCollection;
+
+/**
+ * Provides specific linkit matchers for anchors. Does not check anchor exists.
+ *
+ * @Matcher(
+ *   id = "anchor_basic",
+ *   label = @Translation("Anchor (basic)"),
+ * )
+ */
+class AnchorMatcher extends MatcherBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function execute($string) {
+    $suggestions = new SuggestionCollection();
+
+    // Check for a # and tell people it's an anchor.
+    if (substr($string, 0, 1) == '#' && strlen($string) > 1) {
+      $suggestion = new DescriptionSuggestion();
+      $suggestion->setLabel($this->t('Anchor @anchor', ['@anchor' => $string]))
+        ->setPath(Html::escape($string))
+        ->setGroup($this->t('Anchor'))
+        ->setDescription($this->t('Links to an anchor named @anchor on the current page', ['@anchor' => $string]));
+
+      $suggestions->addSuggestion($suggestion);
+    }
+    return $suggestions;
+  }
+
+}
diff --git a/tests/src/Kernel/LinkitAutocompleteTest.php b/tests/src/Kernel/LinkitAutocompleteTest.php
index 5eae2841..5e1e7909 100644
--- a/tests/src/Kernel/LinkitAutocompleteTest.php
+++ b/tests/src/Kernel/LinkitAutocompleteTest.php
@@ -114,6 +114,21 @@ class LinkitAutocompleteTest extends LinkitKernelTestBase {
     $this->assertSame('mailto:' . $email, $data[0]['path'], 'Autocomplete returned email suggestion with an mailto href.');
   }
 
+  /**
+   * Tests the autocomplete with an anchor (basic).
+   */
+  public function testAutocompletionAnchorBasic() {
+    /** @var \Drupal\linkit\MatcherInterface $plugin */
+    $plugin = $this->matcherManager->createInstance('anchor_basic');
+    $this->linkitProfile->addMatcher($plugin->getConfiguration());
+    $this->linkitProfile->save();
+
+    $anchor = '#example';
+    $data = $this->getAutocompleteResult($anchor);
+    $this->assertSame((string) new FormattableMarkup('Anchor @anchor', ['@anchor' => $anchor]), $data[0]['label'], 'Autocomplete returned anchor suggestion.');
+    $this->assertSame($anchor, $data[0]['path'], 'Autocomplete returned anchor suggestion as entered.');
+  }
+
   /**
    * Tests autocompletion in general.
    */
-- 
GitLab