mirror of
https://github.com/zulip/zulip.git
synced 2025-11-02 21:13:36 +00:00
linkifiers: Avoid parallel data structure.
We can pretty easily work with a map in the two places that we ever relied on an array.
This commit is contained in:
@@ -2,8 +2,11 @@ import marked from "../third/marked/lib/marked";
|
|||||||
|
|
||||||
import * as blueslip from "./blueslip";
|
import * as blueslip from "./blueslip";
|
||||||
|
|
||||||
const linkifier_map = new Map();
|
const linkifier_map = new Map(); // regex -> url
|
||||||
export let linkifier_list = [];
|
|
||||||
|
export function get_linkifier_map() {
|
||||||
|
return linkifier_map;
|
||||||
|
}
|
||||||
|
|
||||||
function handleLinkifier(pattern, matches) {
|
function handleLinkifier(pattern, matches) {
|
||||||
let url = linkifier_map.get(pattern);
|
let url = linkifier_map.get(pattern);
|
||||||
@@ -80,7 +83,6 @@ function python_to_js_linkifier(pattern, url) {
|
|||||||
export function update_linkifier_rules(linkifiers) {
|
export function update_linkifier_rules(linkifiers) {
|
||||||
// Update the marked parser with our particular set of linkifiers
|
// Update the marked parser with our particular set of linkifiers
|
||||||
linkifier_map.clear();
|
linkifier_map.clear();
|
||||||
linkifier_list = [];
|
|
||||||
|
|
||||||
const marked_rules = [];
|
const marked_rules = [];
|
||||||
|
|
||||||
@@ -92,10 +94,6 @@ export function update_linkifier_rules(linkifiers) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
linkifier_map.set(regex, final_url);
|
linkifier_map.set(regex, final_url);
|
||||||
linkifier_list.push({
|
|
||||||
pattern: regex,
|
|
||||||
url_format: final_url,
|
|
||||||
});
|
|
||||||
marked_rules.push(regex);
|
marked_rules.push(regex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,13 +85,15 @@ function contains_problematic_linkifier(content) {
|
|||||||
// If a linkifier doesn't start with some specified characters
|
// If a linkifier doesn't start with some specified characters
|
||||||
// then don't render it locally. It is workaround for the fact that
|
// then don't render it locally. It is workaround for the fact that
|
||||||
// javascript regex doesn't support lookbehind.
|
// javascript regex doesn't support lookbehind.
|
||||||
const linkifier_list = linkifiers.linkifier_list;
|
for (const re of linkifiers.get_linkifier_map().keys()) {
|
||||||
const false_linkifier_match = linkifier_list.find((re) => {
|
const pattern = /[^\s"'(,:<]/.source + re.source + /(?!\w)/.source;
|
||||||
const pattern = /[^\s"'(,:<]/.source + re.pattern.source + /(?!\w)/.source;
|
|
||||||
const regex = new RegExp(pattern);
|
const regex = new RegExp(pattern);
|
||||||
return regex.test(content);
|
if (regex.test(content)) {
|
||||||
});
|
return true;
|
||||||
return false_linkifier_match !== undefined;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function contains_backend_only_syntax(content) {
|
export function contains_backend_only_syntax(content) {
|
||||||
@@ -286,11 +288,8 @@ export function add_topic_links(message) {
|
|||||||
}
|
}
|
||||||
const topic = message.topic;
|
const topic = message.topic;
|
||||||
const links = [];
|
const links = [];
|
||||||
const linkifier_list = linkifiers.linkifier_list;
|
|
||||||
|
|
||||||
for (const linkifier of linkifier_list) {
|
for (const [pattern, url] of linkifiers.get_linkifier_map().entries()) {
|
||||||
const pattern = linkifier.pattern;
|
|
||||||
const url = linkifier.url_format;
|
|
||||||
let match;
|
let match;
|
||||||
while ((match = pattern.exec(topic)) !== null) {
|
while ((match = pattern.exec(topic)) !== null) {
|
||||||
let link_url = url;
|
let link_url = url;
|
||||||
|
|||||||
Reference in New Issue
Block a user