From 214ec099bb141c671f6df9622174c444550bfdb3 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 4 Apr 2022 15:43:21 +0000 Subject: [PATCH] markdown: Eliminate setup() call. It has always been pretty arbitrary what we did inside of setup() vs. parse(), and we want to avoid unpredictable results from other platforms neglecting to call setup(). On my machine you can parse a simple message in about 25 microseconds, based on a trial of a million messages with the content of "**bold**". Whatever portion of that time is related to setup-related things like compiling regexes should be negligible from the user's perspective, since we never run parse() in a loop. --- frontend_tests/node_tests/markdown_parse.js | 19 +++++------- static/js/markdown.js | 33 ++++++++------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/frontend_tests/node_tests/markdown_parse.js b/frontend_tests/node_tests/markdown_parse.js index 4de2c4eda5..1e58540928 100644 --- a/frontend_tests/node_tests/markdown_parse.js +++ b/frontend_tests/node_tests/markdown_parse.js @@ -152,17 +152,12 @@ function assert_parse(raw_content, expected_content) { assert.equal(content, expected_content); } -function test(label, f) { - markdown.setup(); - run_test(label, f); -} - -test("basics", () => { +run_test("basics", () => { assert_parse("boring", "

boring

"); assert_parse("**bold**", "

bold

"); }); -test("user mentions", () => { +run_test("user mentions", () => { assert_parse("@**greg**", '

@greg

'); assert_parse("@**|105**", '

@greg

'); @@ -178,14 +173,14 @@ test("user mentions", () => { ); }); -test("user group mentions", () => { +run_test("user group mentions", () => { assert_parse( "@*Staff*", '

@Staff

', ); }); -test("stream links", () => { +run_test("stream links", () => { assert_parse( "#**social**", '

#social

', @@ -197,7 +192,7 @@ test("stream links", () => { ); }); -test("emojis", () => { +run_test("emojis", () => { assert_parse( "yup :)", '

yup :smile:

', @@ -212,14 +207,14 @@ test("emojis", () => { ); }); -test("linkifiers", () => { +run_test("linkifiers", () => { assert_parse( "see #foo12345 for details", '

see #foo12345 for details

', ); }); -test("topic links", () => { +run_test("topic links", () => { const topic = "progress on #foo101 and #foo102"; const topic_links = markdown.get_topic_links({topic, get_linkifier_map}); assert.deepEqual(topic_links, [ diff --git a/static/js/markdown.js b/static/js/markdown.js index f390282da4..e2de62d9c0 100644 --- a/static/js/markdown.js +++ b/static/js/markdown.js @@ -453,10 +453,7 @@ export function get_linkifier_regexes() { return Array.from(helpers.get_linkifier_map().keys()); } -export function setup() { - // Once we focus on supporting other platforms such as mobile, - // we will export this function. - +export function parse({raw_content, helper_config}) { function disable_markdown_regex(rules, name) { rules[name] = { exec() { @@ -486,7 +483,7 @@ export function setup() { } function preprocess_translate_emoticons(src) { - if (!helpers.should_translate_emoticons()) { + if (!helper_config.should_translate_emoticons()) { return src; } @@ -517,21 +514,6 @@ export function setup() { // HTML into the output. This generated HTML is safe to not escape fenced_code.set_stash_func((html) => marked.stashHtml(html, true)); - marked.setOptions({ - gfm: true, - tables: true, - breaks: true, - pedantic: false, - sanitize: true, - smartLists: true, - smartypants: false, - zulip: true, - renderer: r, - preprocessors: [preprocess_code_blocks, preprocess_translate_emoticons], - }); -} - -export function parse({raw_content, helper_config}) { function streamHandler(stream_name) { return handleStream({ stream_name, @@ -581,6 +563,16 @@ export function parse({raw_content, helper_config}) { streamTopicHandler, texHandler: handleTex, timestampHandler: handleTimestamp, + gfm: true, + tables: true, + breaks: true, + pedantic: false, + sanitize: true, + smartLists: true, + smartypants: false, + zulip: true, + renderer: r, + preprocessors: [preprocess_code_blocks, preprocess_translate_emoticons], }; return parse_with_options({raw_content, helper_config, options}); @@ -598,7 +590,6 @@ export function initialize(helper_config) { // other platforms should call setup(). webapp_helpers = helper_config; helpers = helper_config; - setup(); } export function apply_markdown(message) {