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: Support alternative_url_templates in reverse linkification.

Shubham Padia82a7b19a5528
diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts
index 53f1d46f1d3..bdd8f06c8ad 100644
--- a/web/src/compose_ui.ts
+++ b/web/src/compose_ui.ts
@@ -744,33 +744,39 @@ function expand_url_template_from_match(
function reverse_linkify_segment(segment: string): string | null {
const linkifier_map = linkifiers.get_linkifier_map();
- for (const [pattern, {url_template, group_number_to_name, reverse_template}] of linkifier_map) {
+ for (const [
+ pattern,
+ {url_template, group_number_to_name, reverse_template, alternative_url_templates},
+ ] of linkifier_map) {
if (!reverse_template) {
continue;
}
- const template_context = url_template.match(segment);
- if (!template_context) {
- continue;
- }
+ const all_templates = [url_template, ...alternative_url_templates];
+ for (const template of all_templates) {
+ const template_context = template.match(segment);
+ if (!template_context) {
+ continue;
+ }
- const reversed_text = expand_reverse_template(reverse_template, template_context);
- pattern.lastIndex = 0;
- const match = pattern.exec(reversed_text);
- if (!match) {
- continue;
- }
+ const reversed_text = expand_reverse_template(reverse_template, template_context);
+ pattern.lastIndex = 0;
+ const match = pattern.exec(reversed_text);
+ if (!match) {
+ continue;
+ }
- // Validate that expanding the captured groups round-trips to the original URL.
- const expanded_url = expand_url_template_from_match(
- match,
- url_template,
- group_number_to_name,
- );
- if (expanded_url !== segment) {
- continue;
+ // Validate that expanding the captured groups round-trips to the original URL.
+ const expanded_url = expand_url_template_from_match(
+ match,
+ template,
+ group_number_to_name,
+ );
+ if (expanded_url !== segment) {
+ continue;
+ }
+ return reversed_text;
}
- return reversed_text;
}
return null;
}
diff --git a/web/src/linkifiers.ts b/web/src/linkifiers.ts
index dc0cfa83ac0..c6f785a77cf 100644
--- a/web/src/linkifiers.ts
+++ b/web/src/linkifiers.ts
@@ -10,6 +10,7 @@ type LinkifierMap = Map<
url_template: Template;
group_number_to_name: Record<number, string>;
reverse_template: string | null;
+ alternative_url_templates: Template[];
}
>;
const linkifier_map: LinkifierMap = new Map();
@@ -23,7 +24,8 @@ export function get_linkifier_map(): LinkifierMap {
export function python_to_js_linkifier(
pattern: string,
url: string,
-): [RegExp | null, Template, Record<number, string>] {
+ alternative_urls: string[] = [],
+): [RegExp | null, Template, Record<number, string>, Template[]] {
// Converts a python named-group regex to a javascript-compatible numbered
// group regex... with a regex!
const named_group_re = /\(?P<([^>]+?)>/g;
@@ -86,17 +88,22 @@ export function python_to_js_linkifier(
}
}
const url_template = new Template(url);
- return [final_regex, url_template, group_number_to_name];
+ const alternative_url_templates = alternative_urls.map(
+ (template_string) => new Template(template_string),
+ );
+ return [final_regex, url_template, group_number_to_name, alternative_url_templates];
}
export function update_linkifier_rules(linkifiers: Linkifier[]): void {
linkifier_map.clear();
for (const linkifier of linkifiers) {
- const [regex, url_template, group_number_to_name] = python_to_js_linkifier(
- linkifier.pattern,
- linkifier.url_template,
- );
+ const [regex, url_template, group_number_to_name, alternative_url_templates] =
+ python_to_js_linkifier(
+ linkifier.pattern,
+ linkifier.url_template,
+ linkifier.alternative_url_templates ?? [],
+ );
if (!regex) {
// Skip any linkifiers that could not be converted
continue;
@@ -106,6 +113,7 @@ export function update_linkifier_rules(linkifiers: Linkifier[]): void {
url_template,
group_number_to_name,
reverse_template: linkifier.reverse_template ?? null,
+ alternative_url_templates,
});
}
}
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index 08914fdaa75..f04741c458e 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -411,6 +411,7 @@ export const realm_linkifier_schema = z.object({
id: z.number(),
example_input: z.optional(z.nullable(z.string())),
reverse_template: z.optional(z.nullable(z.string())),
+ alternative_url_templates: z.optional(z.array(z.string())),
});
export const realm_report_message_types = z.object({
diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjs
index f9212008402..581bfa4f63e 100644
--- a/web/tests/compose_paste.test.cjs
+++ b/web/tests/compose_paste.test.cjs
@@ -145,6 +145,7 @@ run_test("paste_handler reverse linkify", ({override, override_rewire}) => {
pattern: "#D(?P<id>[0-9]{2,8})",
url_template: "https://github.com/zulip/zulip-desktop/pull/{id}",
reverse_template: "#D{id}",
+ alternative_url_templates: ["https://github.com/zulip/zulip-desktop/issues/{id}"],
},
]);
@@ -219,6 +220,13 @@ run_test("paste_handler reverse linkify", ({override, override_rewire}) => {
expected: "#D1359 dummy text.",
expected_undo_texts: ["https://github.com/zulip/zulip-desktop/pull/1359 dummy text."],
},
+ {
+ // Reverse linkify should work for alternative URL templates.
+ paste_html: "",
+ paste_text: "https://github.com/zulip/zulip-desktop/issues/42",
+ expected: "#D42",
+ expected_undo_texts: ["https://github.com/zulip/zulip-desktop/issues/42"],
+ },
];
for (const test_case of test_cases) {
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index 68061cb4a0a..346349578a3 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -393,6 +393,59 @@ run_test("reverse_linkify_text", () => {
compose_ui.reverse_linkify_text("https://github.com/zulip/zulip-flutter/pull/123"),
"zulip-flutter#123",
);
+
+ // Alternative URL templates: match URLs against both primary and alternatives.
+ linkifiers.update_linkifier_rules([
+ {
+ id: 7,
+ pattern: "#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/zulip/issues/{id}",
+ reverse_template: "#{id}",
+ alternative_url_templates: ["https://github.com/zulip/zulip/pull/{id}"],
+ },
+ ]);
+ // Primary URL template still works.
+ assert.equal(
+ compose_ui.reverse_linkify_text("https://github.com/zulip/zulip/issues/123"),
+ "#123",
+ );
+ // Alternative URL template also works.
+ assert.equal(
+ compose_ui.reverse_linkify_text("https://github.com/zulip/zulip/pull/456"),
+ "#456",
+ );
+ // Non-matching URL returns null.
+ assert.equal(compose_ui.reverse_linkify_text("https://github.com/zulip/zulip/wiki/789"), null);
+
+ // Multiple alternative URL templates.
+ linkifiers.update_linkifier_rules([
+ {
+ id: 8,
+ pattern: "#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/zulip/issues/{id}",
+ reverse_template: "#{id}",
+ alternative_url_templates: [
+ "https://github.com/zulip/zulip/pull/{id}",
+ "https://github.com/zulip/zulip/discussions/{id}",
+ ],
+ },
+ ]);
+ assert.equal(
+ compose_ui.reverse_linkify_text("https://github.com/zulip/zulip/discussions/42"),
+ "#42",
+ );
+
+ // Alternative templates with no reverse_template are ignored.
+ linkifiers.update_linkifier_rules([
+ {
+ id: 9,
+ pattern: "#(?P<id>\\d+)",
+ url_template: "https://github.com/zulip/zulip/issues/{id}",
+ reverse_template: null,
+ alternative_url_templates: ["https://github.com/zulip/zulip/pull/{id}"],
+ },
+ ]);
+ assert.equal(compose_ui.reverse_linkify_text("https://github.com/zulip/zulip/pull/123"), null);
});
run_test("quote_message", ({override, override_rewire}) => {