Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

linkifiers: Clarify behaviour in case of overlap for reverse linkifier.

Shubham Padia1da127257cf2
diff --git a/starlight_help/src/content/docs/add-a-custom-linkifier.mdx b/starlight_help/src/content/docs/add-a-custom-linkifier.mdx
index 7340f88b1cb..bab5f20610d 100644
--- a/starlight_help/src/content/docs/add-a-custom-linkifier.mdx
+++ b/starlight_help/src/content/docs/add-a-custom-linkifier.mdx
@@ -200,30 +200,46 @@ This example pattern allows linking to Google searches.
### Overlapping patterns
-In this example, a general linkifier is configured to make GitHub
-repository references like `zulip-desktop#123` link to issues in that
-repository within the `zulip` GitHub organization. A more specific
-linkifier overrides that linkifier for a specific repository of
-interest (`django/django`) that is in a different organization.
+This example shows how specific linkifiers can be combined with more general
+ones to offer succinct linked text for the most commonly referenced links. For
+example, if you could have:
+
+* A specific linkifier for the `zulip/zulip` GitHub repository, so that `#123`
+ links to an issue or pull request in that repository.
+* A general linkifier for all GitHub repositories.
+
+You would set that up with:
* Specific linkifier (ordered before the general linkifier)
- * Pattern: `django#(?P<id>[0-9]+)`
- * URL template: `https://github.com/django/django/pull/{id}`
+ * Example text to linkify: `#123`
+ * Pattern: `#(?P<id>[0-9]+)`
+ * URL template: `https://github.com/zulip/zulip/pull/{id}`
+ * Auto-convert pasted URLs matching the template to: `#{id}`
* General linkifier
- * Pattern: `(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)`
- * URL template: `https://github.com/zulip/{repo}/pull/{id}`
-* Example matching both linkifiers; specific linkifier takes precedence:
- * Original text: `django#123`
- * Automatically links to: `https://github.com/django/django/pull/123`
-* Example matching only the general linkifier:
- * Original text: `zulip-desktop#123`
- * Automatically links to: `https://github.com/zulip/zulip-desktop/pull/123`
+ * Example text to linkify: `org/repo#123`
+ * Pattern: `(?P<org>[a-zA-Z0-9_-]+)/(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)`
+ * URL template: `https://github.com/{org}/{repo}/pull/{id}`
+ * Auto-convert pasted URLs matching the template to: `{org}/{repo}#{id}`
+
+With the specific linkifier ordered first, when both linkifiers match, the
+specific linkifier will take precedence:
+
+* Original text: `#123`
+* Automatically links to: `https://github.com/zulip/zulip/pull/123`
+* Pasting `https://github.com/zulip/zulip/pull/123` auto-converts to: `#123`
+
+When only the general linkifier matches:
+
+* Original text: `django/django#123`
+* Automatically links to: `https://github.com/django/django/pull/123`
+* Pasting `https://github.com/django/django/pull/123` auto-converts to: `django/django#123`
<ZulipTip>
- This set of patterns has overlapping regular expressions. Note that
- the general linkifier pattern would match `lorem#123` too. The specific
- linkifier will only get prioritized over the general linkifier if it is
- ordered before the more general pattern. This can be customized by
- dragging and dropping existing linkifiers into the desired order. New
- linkifiers will automatically be ordered last.
+ This set of patterns has overlapping regular expressions and URL
+ templates. The specific linkifier will only get prioritized over
+ the general linkifier if it is ordered before the more general
+ pattern. The same priority order applies when auto-converting
+ pasted URLs. This can be customized by dragging and dropping
+ existing linkifiers into the desired order. New linkifiers will
+ automatically be ordered last.
</ZulipTip>
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index da471a8ca0a..c00784e185a 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -327,6 +327,46 @@ run_test("reverse_linkify_text", () => {
]);
assert.equal(compose_ui.reverse_linkify_text("https://tracker.example.com/issue/a%2Fb"), null);
assert.equal(compose_ui.reverse_linkify_text("https://tracker.example.com/issue/a/b"), "#a/b");
+
+ // When reverse linkifier URL templates overlap, the first matching
+ // linkifier should determine the shortened text.
+ linkifiers.update_linkifier_rules([
+ {
+ id: 5,
+ pattern: "flutter#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/zulip-flutter/pull/{id}",
+ reverse_template: "flutter#{id}",
+ },
+ {
+ id: 6,
+ pattern: "(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/{repo}/pull/{id}",
+ reverse_template: "{repo}#{id}",
+ },
+ ]);
+ assert.equal(
+ compose_ui.reverse_linkify_text("https://github.com/zulip/zulip-flutter/pull/123"),
+ "flutter#123",
+ );
+
+ linkifiers.update_linkifier_rules([
+ {
+ id: 6,
+ pattern: "(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/{repo}/pull/{id}",
+ reverse_template: "{repo}#{id}",
+ },
+ {
+ id: 5,
+ pattern: "flutter#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/zulip-flutter/pull/{id}",
+ reverse_template: "flutter#{id}",
+ },
+ ]);
+ assert.equal(
+ compose_ui.reverse_linkify_text("https://github.com/zulip/zulip-flutter/pull/123"),
+ "zulip-flutter#123",
+ );
});
run_test("quote_message", ({override, override_rewire}) => {