Files
zulip/help/add-a-custom-linkifier.md
Zixuan James Li 268f858f39 linkifier: Support URL templates for linkifiers.
This swaps out url_format_string from all of our APIs and replaces it
with url_template. Note that the documentation changes in the following
commits  will be squashed with this commit.

We change the "url_format" key to "url_template" for the
realm_linkifiers events in event_schema, along with updating
LinkifierDict. "url_template" is the name chosen to normalize
mixed usages of "url_format_string" and "url_format" throughout
the backend.

The markdown processor is updated to stop handling the format string
interpolation and delegate the task template expansion to the uri_template
library instead.

This change affects many test cases. We mostly just replace "%(name)s"
with "{name}", "url_format_string" with "url_template" to make sure that
they still pass. There are some test cases dedicated for testing "%"
escaping, which aren't relevant anymore and are subject to removal.
But for now we keep most of them as-is, and make sure that "%" is always
escaped since we do not use it for variable substitution any more.

Since url_format_string is not populated anymore, a migration is created
to remove this field entirely, and make url_template non-nullable since
we will always populate it. Note that it is possible to have
url_template being null after migration 0422 and before 0424, but
in practice, url_template will not be None after backfilling and the
backend now is always setting url_template.

With the removal of url_format_string, RealmFilter model will now be cleaned
with URL template checks, and the old checks for escapes are removed.

We also modified RealmFilter.clean to skip the validation when the
url_template is invalid. This avoids raising mulitple ValidationError's
when calling full_clean on a linkifier. But we might eventually want to
have a more centric approach to data validation instead of having
the same validation in both the clean method and the validator.

Fixes #23124.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2023-04-19 12:20:49 -07:00

2.0 KiB

Add a custom linkifier

{!admin-only.md!}

Linkifiers make it easy to refer to issues or tickets in third party issue trackers, like GitHub, Salesforce, Zendesk, and others. For instance, you can add a linkifier that automatically turns #2468 into a link to https://github.com/zulip/zulip/issues/2468.

Linkifiers use RFC 6570 compliant URL templates to represent the link to be generated from the pattern.

If the pattern appears in a topic, Zulip adds an Open () button to the right of the topic in the message recipient bar that links to the appropriate URL.

Add a custom linkifier

{start_tabs}

{settings_tab|linkifier-settings}

  1. Under Add a new linkifier, enter a Pattern and URL template.

  2. Click Add linkifier.

{end_tabs}

Understanding linkification patterns

This is best explained by example.

Hash followed by a number of any length.

  • Pattern: #(?P<id>[0-9]+)
  • URL template: https://github.com/zulip/zulip/issues/{id}
  • Original text: #2468
  • Automatically links to: https://github.com/zulip/zulip/issues/2468

String of hexadecimal digits between 7 and 40 characters long.

  • Pattern: (?P<id>[0-9a-f]{7,40})
  • URL template: https://github.com/zulip/zulip/commit/{id}
  • Original text: abdc123
  • Automatically links to: https://github.com/zulip/zulip/commit/abcd123

Generic GitHub org/repo#ID format:

  • Pattern: (?P<org>[a-zA-Z0-9_-]+)/(?P<repo>[a-zA-Z0-9_-]+)#(?P<id>[0-9]+)
  • URL template: https://github.com/{org}/{repo}/issues/{id}
  • Original text: zulip/zulip#2468
  • Automatically links to: https://github.com/zulip/zulip/issues/2468

Linkifier patterns are regular expressions, using the re2 regular expression engine.

If you have any trouble setting these up, please contact us with details on what you're trying to do, and we'll be happy to help you out.