markdown: Extract set_linkifier_regexes.

This is definitely better than having linkifiers
reach directly into marked.js, but there is
probably further improvement we can do here
to clean up how these regexes get set.

This introduces a circular dependency between
markdown.js and linkifiers.js, but we will
soon break it in the other direction.
This commit is contained in:
Steve Howell 2022-03-29 18:17:05 +00:00 committed by Tim Abbott
parent 2a240d3e19
commit b7f670f5a0
2 changed files with 9 additions and 7 deletions

View File

@ -1,6 +1,5 @@
import marked from "../third/marked/lib/marked";
import * as blueslip from "./blueslip";
import * as markdown from "./markdown";
const linkifier_map = new Map(); // regex -> url
@ -67,11 +66,8 @@ 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();
const marked_rules = [];
for (const linkifier of linkifiers) {
const [regex, final_url] = python_to_js_linkifier(linkifier.pattern, linkifier.url_format);
if (!regex) {
@ -80,10 +76,10 @@ export function update_linkifier_rules(linkifiers) {
}
linkifier_map.set(regex, final_url);
marked_rules.push(regex);
}
marked.InlineLexer.rules.zulip.linkifiers = marked_rules;
// Update our parser with our particular set of linkifiers.
markdown.set_linkifier_regexes(Array.from(linkifier_map.keys()));
}
export function initialize(linkifiers) {

View File

@ -449,6 +449,12 @@ function handleTex(tex, fullmatch) {
}
}
export function set_linkifier_regexes(regexes) {
// This needs to be called any time we modify our linkifier regexes,
// until we find a less clumsy way to handle this.
marked.InlineLexer.rules.zulip.linkifiers = regexes;
}
export function setup() {
// Once we focus on supporting other platforms such as mobile,
// we will export this function.