Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

markdown: Inject linkifiers helper.

Steve Howell388471003383
diff --git a/frontend_tests/node_tests/markdown_parse.js b/frontend_tests/node_tests/markdown_parse.js
index f4a55d2e9a0..e5b3c89a669 100644
--- a/frontend_tests/node_tests/markdown_parse.js
+++ b/frontend_tests/node_tests/markdown_parse.js
@@ -109,6 +109,14 @@ function get_realm_emoji_url(emoji_name) {
return realm_emoji_map.get(emoji_name);
}
+const regex = /#foo(\d+)(?!\w)/g;
+const linkifier_map = new Map();
+linkifier_map.set(regex, "http://foo.com/\\1");
+
+function get_linkifier_map() {
+ return linkifier_map;
+}
+
const helper_config = {
// user stuff
get_actual_name_from_user_id,
@@ -134,6 +142,9 @@ const helper_config = {
get_emoji_name,
get_emoticon_translations,
get_realm_emoji_url,
+
+ // linkifiers
+ get_linkifier_map,
};
function assert_parse(raw_content, expected_content) {
@@ -143,6 +154,7 @@ function assert_parse(raw_content, expected_content) {
function test(label, f) {
markdown.setup();
+ markdown.set_linkifier_regexes([regex]);
run_test(label, f);
}
@@ -200,3 +212,10 @@ test("emojis", () => {
'<p>Mars Attacks! <span aria-label="alien" class="emoji emoji-1f47d" role="img" title="alien">:alien:</span></p>',
);
});
+
+test("linkifiers", () => {
+ assert_parse(
+ "see #foo12345 for details",
+ '<p>see <a href="http://foo.com/12345" title="http://foo.com/12345">#foo12345</a> for details</p>',
+ );
+});
diff --git a/static/js/markdown.js b/static/js/markdown.js
index f23a9667b77..712b72531ed 100644
--- a/static/js/markdown.js
+++ b/static/js/markdown.js
@@ -6,7 +6,6 @@ import * as fenced_code from "../shared/js/fenced_code";
import marked from "../third/marked/lib/marked";
import * as blueslip from "./blueslip";
-import * as linkifiers from "./linkifiers";
// This contains zulip's frontend Markdown implementation; see
// docs/subsystems/markdown.md for docs on our Markdown syntax. The other
@@ -84,7 +83,7 @@ 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.
- for (const re of linkifiers.get_linkifier_map().keys()) {
+ for (const re of helpers.get_linkifier_map().keys()) {
const pattern = /[^\s"'(,:<]/.source + re.source + /(?!\w)/.source;
const regex = new RegExp(pattern);
if (regex.test(content)) {
@@ -284,7 +283,7 @@ function parse_with_options({raw_content, helper_config, options}) {
return {content, flags};
}
-export function add_topic_links(message) {
+function do_add_topic_links({message, get_linkifier_map}) {
if (message.type !== "stream") {
message.topic_links = [];
return;
@@ -292,7 +291,7 @@ export function add_topic_links(message) {
const topic = message.topic;
const links = [];
- for (const [pattern, url] of linkifiers.get_linkifier_map().entries()) {
+ for (const [pattern, url] of get_linkifier_map().entries()) {
let match;
while ((match = pattern.exec(topic)) !== null) {
let link_url = url;
@@ -376,8 +375,8 @@ function handleEmoji({emoji_name, get_realm_emoji_url, get_emoji_codepoint}) {
return alt_text;
}
-function handleLinkifier(pattern, matches) {
- let url = linkifiers.get_linkifier_map().get(pattern);
+function handleLinkifier({pattern, matches, get_linkifier_map}) {
+ let url = get_linkifier_map().get(pattern);
let current_group = 1;
@@ -569,9 +568,17 @@ export function parse({raw_content, helper_config}) {
});
}
+ function linkifierHandler(pattern, matches) {
+ return handleLinkifier({
+ pattern,
+ matches,
+ get_linkifier_map: helper_config.get_linkifier_map,
+ });
+ }
+
const options = {
emojiHandler,
- linkifierHandler: handleLinkifier,
+ linkifierHandler,
unicodeEmojiHandler,
streamHandler,
streamTopicHandler,
@@ -607,6 +614,10 @@ export function apply_markdown(message) {
message.is_me_message = is_status_message(raw_content);
}
+export function add_topic_links(message) {
+ return do_add_topic_links({message, get_linkifier_map: webapp_helpers.get_linkifier_map});
+}
+
export function parse_non_message(raw_content) {
// Occasionally we get markdown from the server that is not technically
// a message, but we want to convert it to HTML. Note that we parse
diff --git a/static/js/markdown_config.js b/static/js/markdown_config.js
index a3b4854d6b8..aa8bccecb46 100644
--- a/static/js/markdown_config.js
+++ b/static/js/markdown_config.js
@@ -1,5 +1,6 @@
import * as emoji from "./emoji";
import * as hash_util from "./hash_util";
+import * as linkifiers from "./linkifiers";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as user_groups from "./user_groups";
@@ -52,4 +53,7 @@ export const get_helpers = () => ({
get_emoji_codepoint: emoji.get_emoji_codepoint,
get_emoticon_translations: emoji.get_emoticon_translations,
get_realm_emoji_url: emoji.get_realm_emoji_url,
+
+ // linkifiers
+ get_linkifier_map: linkifiers.get_linkifier_map,
});