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.
This commit is contained in:
Steve Howell
2022-04-04 15:43:21 +00:00
committed by Tim Abbott
parent 093eba077a
commit 214ec099bb
2 changed files with 19 additions and 33 deletions

View File

@@ -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", "<p>boring</p>");
assert_parse("**bold**", "<p><strong>bold</strong></p>");
});
test("user mentions", () => {
run_test("user mentions", () => {
assert_parse("@**greg**", '<p><span class="user-mention" data-user-id="105">@greg</span></p>');
assert_parse("@**|105**", '<p><span class="user-mention" data-user-id="105">@greg</span></p>');
@@ -178,14 +173,14 @@ test("user mentions", () => {
);
});
test("user group mentions", () => {
run_test("user group mentions", () => {
assert_parse(
"@*Staff*",
'<p><span class="user-group-mention" data-user-group-id="201">@Staff</span></p>',
);
});
test("stream links", () => {
run_test("stream links", () => {
assert_parse(
"#**social**",
'<p><a class="stream" data-stream-id="301" href="/stream-301">#social</a></p>',
@@ -197,7 +192,7 @@ test("stream links", () => {
);
});
test("emojis", () => {
run_test("emojis", () => {
assert_parse(
"yup :)",
'<p>yup <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>',
@@ -212,14 +207,14 @@ test("emojis", () => {
);
});
test("linkifiers", () => {
run_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>',
);
});
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, [

View File

@@ -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) {