mirror of
https://github.com/zulip/zulip.git
synced 2025-11-16 11:52:01 +00:00
markdown: Use options, not rules, for linkifier regexes.
This avoids the need to set a global from linkifiers.js.
This commit is contained in:
@@ -7,9 +7,11 @@ const {run_test} = require("../zjsunit/test");
|
|||||||
const blueslip = require("../zjsunit/zblueslip");
|
const blueslip = require("../zjsunit/zblueslip");
|
||||||
|
|
||||||
const linkifiers = zrequire("linkifiers");
|
const linkifiers = zrequire("linkifiers");
|
||||||
const marked = zrequire("../third/marked/lib/marked");
|
const markdown = zrequire("markdown");
|
||||||
|
const markdown_config = zrequire("markdown_config");
|
||||||
|
|
||||||
linkifiers.initialize([]);
|
linkifiers.initialize([]);
|
||||||
|
markdown.initialize(markdown_config.get_helpers());
|
||||||
|
|
||||||
run_test("python_to_js_linkifier", () => {
|
run_test("python_to_js_linkifier", () => {
|
||||||
// The only way to reach python_to_js_linkifier is indirectly, hence the call
|
// The only way to reach python_to_js_linkifier is indirectly, hence the call
|
||||||
@@ -26,7 +28,7 @@ run_test("python_to_js_linkifier", () => {
|
|||||||
id: 20,
|
id: 20,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
let actual_value = marked.InlineLexer.rules.zulip.linkifiers;
|
let actual_value = markdown.get_linkifier_regexes();
|
||||||
let expected_value = [/\/aa\/g(?!\w)/gim, /\/aa\/g(?!\w)/g];
|
let expected_value = [/\/aa\/g(?!\w)/gim, /\/aa\/g(?!\w)/g];
|
||||||
assert.deepEqual(actual_value, expected_value);
|
assert.deepEqual(actual_value, expected_value);
|
||||||
// Test case with multiple replacements.
|
// Test case with multiple replacements.
|
||||||
@@ -37,7 +39,7 @@ run_test("python_to_js_linkifier", () => {
|
|||||||
id: 30,
|
id: 30,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
actual_value = marked.InlineLexer.rules.zulip.linkifiers;
|
actual_value = markdown.get_linkifier_regexes();
|
||||||
expected_value = [/#cf(\d+)([A-Z][\dA-Z]*)(?!\w)/g];
|
expected_value = [/#cf(\d+)([A-Z][\dA-Z]*)(?!\w)/g];
|
||||||
assert.deepEqual(actual_value, expected_value);
|
assert.deepEqual(actual_value, expected_value);
|
||||||
// Test incorrect syntax.
|
// Test incorrect syntax.
|
||||||
@@ -52,7 +54,7 @@ run_test("python_to_js_linkifier", () => {
|
|||||||
id: 40,
|
id: 40,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
actual_value = marked.InlineLexer.rules.zulip.linkifiers;
|
actual_value = markdown.get_linkifier_regexes();
|
||||||
expected_value = [];
|
expected_value = [];
|
||||||
assert.deepEqual(actual_value, expected_value);
|
assert.deepEqual(actual_value, expected_value);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -154,7 +154,6 @@ function assert_parse(raw_content, expected_content) {
|
|||||||
|
|
||||||
function test(label, f) {
|
function test(label, f) {
|
||||||
markdown.setup();
|
markdown.setup();
|
||||||
markdown.set_linkifier_regexes([regex]);
|
|
||||||
run_test(label, f);
|
run_test(label, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import * as blueslip from "./blueslip";
|
import * as blueslip from "./blueslip";
|
||||||
import * as markdown from "./markdown";
|
|
||||||
|
|
||||||
const linkifier_map = new Map(); // regex -> url
|
const linkifier_map = new Map(); // regex -> url
|
||||||
|
|
||||||
@@ -77,9 +76,6 @@ export function update_linkifier_rules(linkifiers) {
|
|||||||
|
|
||||||
linkifier_map.set(regex, final_url);
|
linkifier_map.set(regex, final_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update our parser with our particular set of linkifiers.
|
|
||||||
markdown.set_linkifier_regexes(Array.from(linkifier_map.keys()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function initialize(linkifiers) {
|
export function initialize(linkifiers) {
|
||||||
|
|||||||
@@ -451,10 +451,8 @@ function handleTex(tex, fullmatch) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function set_linkifier_regexes(regexes) {
|
export function get_linkifier_regexes() {
|
||||||
// This needs to be called any time we modify our linkifier regexes,
|
return Array.from(helpers.get_linkifier_map().keys());
|
||||||
// until we find a less clumsy way to handle this.
|
|
||||||
marked.InlineLexer.rules.zulip.linkifiers = regexes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setup() {
|
export function setup() {
|
||||||
@@ -577,8 +575,9 @@ export function parse({raw_content, helper_config}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
emojiHandler,
|
get_linkifier_regexes,
|
||||||
linkifierHandler,
|
linkifierHandler,
|
||||||
|
emojiHandler,
|
||||||
unicodeEmojiHandler,
|
unicodeEmojiHandler,
|
||||||
streamHandler,
|
streamHandler,
|
||||||
streamTopicHandler,
|
streamTopicHandler,
|
||||||
|
|||||||
@@ -484,7 +484,6 @@ var inline = {
|
|||||||
stream: noop,
|
stream: noop,
|
||||||
tex: noop,
|
tex: noop,
|
||||||
timestamp: noop,
|
timestamp: noop,
|
||||||
linkifiers: [],
|
|
||||||
text: /^[\s\S]+?(?=[\\<!\[_*`$]| {2,}\n|$)/
|
text: /^[\s\S]+?(?=[\\<!\[_*`$]| {2,}\n|$)/
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -550,7 +549,6 @@ inline.zulip = merge({}, inline.breaks, {
|
|||||||
stream: /^#\*\*([^\*]+)\*\*/,
|
stream: /^#\*\*([^\*]+)\*\*/,
|
||||||
tex: /^(\$\$([^\n_$](\\\$|[^\n$])*)\$\$(?!\$))\B/,
|
tex: /^(\$\$([^\n_$](\\\$|[^\n$])*)\$\$(?!\$))\B/,
|
||||||
timestamp: /^<time:([^>]+)>/,
|
timestamp: /^<time:([^>]+)>/,
|
||||||
linkifiers: [],
|
|
||||||
text: replace(inline.breaks.text)
|
text: replace(inline.breaks.text)
|
||||||
('|', '|(\ud83c[\udd00-\udfff]|\ud83d[\udc00-\ude4f]|' +
|
('|', '|(\ud83c[\udd00-\udfff]|\ud83d[\udc00-\ude4f]|' +
|
||||||
'\ud83d[\ude80-\udeff]|\ud83e[\udd00-\uddff]|' +
|
'\ud83d[\ude80-\udeff]|\ud83e[\udd00-\uddff]|' +
|
||||||
@@ -647,8 +645,10 @@ InlineLexer.prototype.output = function(src) {
|
|||||||
|
|
||||||
// linkifier (Zulip)
|
// linkifier (Zulip)
|
||||||
var self = this;
|
var self = this;
|
||||||
this.rules.linkifiers.forEach(function (linkifier) {
|
|
||||||
var ret = self.inlineReplacement(linkifier, src, function(regex, groups, match) {
|
const regexes = this.options.get_linkifier_regexes ? this.options.get_linkifier_regexes() : [];
|
||||||
|
regexes.forEach(function (regex) {
|
||||||
|
var ret = self.inlineReplacement(regex, src, function(regex, groups, match) {
|
||||||
// Insert the created URL
|
// Insert the created URL
|
||||||
href = self.linkifier(regex, groups, match);
|
href = self.linkifier(regex, groups, match);
|
||||||
if (href !== undefined) {
|
if (href !== undefined) {
|
||||||
|
|||||||
Reference in New Issue
Block a user