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:
Steve Howell
2022-03-29 15:14:44 +00:00
committed by Tim Abbott
parent 71c12e313c
commit efdf2c8fe3
2 changed files with 14 additions and 17 deletions

View File

@@ -2,8 +2,11 @@ import marked from "../third/marked/lib/marked";
import * as blueslip from "./blueslip";
const linkifier_map = new Map();
export let linkifier_list = [];
const linkifier_map = new Map(); // regex -> url
export function get_linkifier_map() {
return linkifier_map;
}
function handleLinkifier(pattern, matches) {
let url = linkifier_map.get(pattern);
@@ -80,7 +83,6 @@ function python_to_js_linkifier(pattern, url) {
export function update_linkifier_rules(linkifiers) {
// Update the marked parser with our particular set of linkifiers
linkifier_map.clear();
linkifier_list = [];
const marked_rules = [];
@@ -92,10 +94,6 @@ export function update_linkifier_rules(linkifiers) {
}
linkifier_map.set(regex, final_url);
linkifier_list.push({
pattern: regex,
url_format: final_url,
});
marked_rules.push(regex);
}

View File

@@ -85,13 +85,15 @@ function contains_problematic_linkifier(content) {
// If a linkifier doesn't start with some specified characters
// then don't render it locally. It is workaround for the fact that
// javascript regex doesn't support lookbehind.
const linkifier_list = linkifiers.linkifier_list;
const false_linkifier_match = linkifier_list.find((re) => {
const pattern = /[^\s"'(,:<]/.source + re.pattern.source + /(?!\w)/.source;
for (const re of linkifiers.get_linkifier_map().keys()) {
const pattern = /[^\s"'(,:<]/.source + re.source + /(?!\w)/.source;
const regex = new RegExp(pattern);
return regex.test(content);
});
return false_linkifier_match !== undefined;
if (regex.test(content)) {
return true;
}
}
return false;
}
export function contains_backend_only_syntax(content) {
@@ -286,11 +288,8 @@ export function add_topic_links(message) {
}
const topic = message.topic;
const links = [];
const linkifier_list = linkifiers.linkifier_list;
for (const linkifier of linkifier_list) {
const pattern = linkifier.pattern;
const url = linkifier.url_format;
for (const [pattern, url] of linkifiers.get_linkifier_map().entries()) {
let match;
while ((match = pattern.exec(topic)) !== null) {
let link_url = url;