js: Convert static/js/markdown.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 15:49:36 -08:00 committed by Tim Abbott
parent ea01e6da5e
commit 10a4d2bcbd
15 changed files with 45 additions and 35 deletions

View File

@ -167,7 +167,6 @@
"localStorage": false, "localStorage": false,
"local_message": false, "local_message": false,
"location": false, "location": false,
"markdown": false,
"message_edit": false, "message_edit": false,
"message_events": false, "message_events": false,
"message_fetch": false, "message_fetch": false,

View File

@ -63,7 +63,8 @@ const transmit = set_global("transmit", {});
const channel = {__esModule: true}; const channel = {__esModule: true};
rewiremock("../../static/js/channel").with(channel); rewiremock("../../static/js/channel").with(channel);
const stream_edit = set_global("stream_edit", {}); const stream_edit = set_global("stream_edit", {});
const markdown = set_global("markdown", {}); const markdown = {__esModule: true};
rewiremock("../../static/js/markdown").with(markdown);
const loading = {__esModule: true}; const loading = {__esModule: true};
rewiremock("../../static/js/loading").with(loading); rewiremock("../../static/js/loading").with(loading);
const page_params = set_global("page_params", {}); const page_params = set_global("page_params", {});

View File

@ -30,7 +30,8 @@ set_global("current_msg_list", {});
const emoji_picker = set_global("emoji_picker", {}); const emoji_picker = set_global("emoji_picker", {});
set_global("home_msg_list", {}); set_global("home_msg_list", {});
const hotspots = set_global("hotspots", {}); const hotspots = set_global("hotspots", {});
const markdown = set_global("markdown", {}); const markdown = {__esModule: true};
rewiremock("../../static/js/markdown").with(markdown);
const message_edit = set_global("message_edit", {}); const message_edit = set_global("message_edit", {});
const message_events = set_global("message_events", {}); const message_events = set_global("message_events", {});
const message_list = set_global("message_list", {}); const message_list = set_global("message_list", {});

View File

@ -2,6 +2,8 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const rewiremock = require("rewiremock/node");
const {stub_templates} = require("../zjsunit/handlebars"); const {stub_templates} = require("../zjsunit/handlebars");
const {set_global, zrequire, with_overrides} = require("../zjsunit/namespace"); const {set_global, zrequire, with_overrides} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
@ -31,13 +33,15 @@ set_global("stream_data", {
return "#FFFFFF"; return "#FFFFFF";
}, },
}); });
set_global("markdown", { rewiremock("../../static/js/markdown").with({
apply_markdown: noop, apply_markdown: noop,
}); });
set_global("page_params", { set_global("page_params", {
twenty_four_hour_time: false, twenty_four_hour_time: false,
}); });
rewiremock.enable();
const {localstorage} = zrequire("localstorage"); const {localstorage} = zrequire("localstorage");
const drafts = zrequire("drafts"); const drafts = zrequire("drafts");
const timerender = zrequire("timerender"); const timerender = zrequire("timerender");
@ -301,3 +305,4 @@ run_test("format_drafts", (override) => {
drafts.launch(); drafts.launch();
timerender.render_now = stub_render_now; timerender.render_now = stub_render_now;
}); });
rewiremock.disable();

View File

@ -3,12 +3,14 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const MockDate = require("mockdate"); const MockDate = require("mockdate");
const rewiremock = require("rewiremock/node");
const {set_global, zrequire} = require("../zjsunit/namespace"); const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const local_message = set_global("local_message", {}); const local_message = set_global("local_message", {});
const markdown = set_global("markdown", {}); const markdown = {__esModule: true};
rewiremock("../../static/js/markdown").with(markdown);
const page_params = set_global("page_params", {}); const page_params = set_global("page_params", {});
const fake_now = 555; const fake_now = 555;
@ -44,6 +46,8 @@ set_global("message_list", {});
set_global("current_msg_list", ""); set_global("current_msg_list", "");
rewiremock.enable();
const echo = zrequire("echo"); const echo = zrequire("echo");
const people = zrequire("people"); const people = zrequire("people");
@ -259,3 +263,4 @@ run_test("insert_local_message PM", (override) => {
}); });
MockDate.reset(); MockDate.reset();
rewiremock.disable();

View File

@ -120,7 +120,6 @@ zrequire("presence");
zrequire("search_pill_widget"); zrequire("search_pill_widget");
zrequire("unread"); zrequire("unread");
zrequire("bot_data"); zrequire("bot_data");
zrequire("markdown");
const upload = zrequire("upload"); const upload = zrequire("upload");
const compose = zrequire("compose"); const compose = zrequire("compose");
zrequire("composebox_typeahead"); zrequire("composebox_typeahead");

View File

@ -24,7 +24,6 @@ import "../widgetize";
import "../message_list"; import "../message_list";
import "../narrow"; import "../narrow";
import "../reload"; import "../reload";
import "../markdown";
import "../local_message"; import "../local_message";
import "../sent_messages"; import "../sent_messages";
import "../compose_state"; import "../compose_state";

View File

@ -16,6 +16,7 @@ const compose_pm_pill = require("./compose_pm_pill");
const drafts = require("./drafts"); const drafts = require("./drafts");
const echo = require("./echo"); const echo = require("./echo");
const loading = require("./loading"); const loading = require("./loading");
const markdown = require("./markdown");
const peer_data = require("./peer_data"); const peer_data = require("./peer_data");
const people = require("./people"); const people = require("./people");
const rendered_markdown = require("./rendered_markdown"); const rendered_markdown = require("./rendered_markdown");

View File

@ -5,6 +5,7 @@ import render_draft_table_body from "../templates/draft_table_body.hbs";
import * as compose_fade from "./compose_fade"; import * as compose_fade from "./compose_fade";
import {localstorage} from "./localstorage"; import {localstorage} from "./localstorage";
import * as markdown from "./markdown";
import * as people from "./people"; import * as people from "./people";
import * as util from "./util"; import * as util from "./util";

View File

@ -1,4 +1,5 @@
import * as alert_words from "./alert_words"; import * as alert_words from "./alert_words";
import * as markdown from "./markdown";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";
import * as people from "./people"; import * as people from "./people";
import * as pm_list from "./pm_list"; import * as pm_list from "./pm_list";

View File

@ -41,7 +41,6 @@ declare let lightbox: any;
declare let list_util: any; declare let list_util: any;
declare let list_widget: any; declare let list_widget: any;
declare let local_message: any; declare let local_message: any;
declare let markdown: any;
declare let message_edit: any; declare let message_edit: any;
declare let message_events: any; declare let message_events: any;
declare let message_fetch: any; declare let message_fetch: any;

View File

@ -1,12 +1,10 @@
"use strict"; import {isValid} from "date-fns";
import katex from "katex";
import _ from "lodash";
const {isValid} = require("date-fns"); import * as emoji from "../shared/js/emoji";
const katex = require("katex"); import * as fenced_code from "../shared/js/fenced_code";
const _ = require("lodash"); import marked from "../third/marked/lib/marked";
const emoji = require("../shared/js/emoji");
const fenced_code = require("../shared/js/fenced_code");
const marked = require("../third/marked/lib/marked");
// This contains zulip's frontend Markdown implementation; see // This contains zulip's frontend Markdown implementation; see
// docs/subsystems/markdown.md for docs on our Markdown syntax. The other // docs/subsystems/markdown.md for docs on our Markdown syntax. The other
@ -38,7 +36,7 @@ const backend_only_markdown_re = [
/\S*(?:twitter|youtube).com\/\S*/, /\S*(?:twitter|youtube).com\/\S*/,
]; ];
exports.translate_emoticons_to_names = (text) => { export function translate_emoticons_to_names(text) {
// Translates emoticons in a string to their colon syntax. // Translates emoticons in a string to their colon syntax.
let translated = text; let translated = text;
let replacement_text; let replacement_text;
@ -76,9 +74,9 @@ exports.translate_emoticons_to_names = (text) => {
} }
return translated; return translated;
}; }
exports.contains_backend_only_syntax = function (content) { export function contains_backend_only_syntax(content) {
// Try to guess whether or not a message contains syntax that only the // Try to guess whether or not a message contains syntax that only the
// backend Markdown processor can correctly handle. // backend Markdown processor can correctly handle.
// If it doesn't, we can immediately render it client-side for local echo. // If it doesn't, we can immediately render it client-side for local echo.
@ -93,9 +91,9 @@ exports.contains_backend_only_syntax = function (content) {
return regex.test(content); return regex.test(content);
}); });
return markedup !== undefined || false_filter_match !== undefined; return markedup !== undefined || false_filter_match !== undefined;
}; }
exports.apply_markdown = function (message) { export function apply_markdown(message) {
message_store.init_booleans(message); message_store.init_booleans(message);
const options = { const options = {
@ -201,10 +199,10 @@ exports.apply_markdown = function (message) {
}; };
// Our python-markdown processor appends two \n\n to input // Our python-markdown processor appends two \n\n to input
message.content = marked(message.raw_content + "\n\n", options).trim(); message.content = marked(message.raw_content + "\n\n", options).trim();
message.is_me_message = exports.is_status_message(message.raw_content); message.is_me_message = is_status_message(message.raw_content);
}; }
exports.add_topic_links = function (message) { export function add_topic_links(message) {
if (message.type !== "stream") { if (message.type !== "stream") {
message.topic_links = []; message.topic_links = [];
return; return;
@ -239,11 +237,11 @@ exports.add_topic_links = function (message) {
} }
message.topic_links = links; message.topic_links = links;
}; }
exports.is_status_message = function (raw_content) { export function is_status_message(raw_content) {
return raw_content.startsWith("/me "); return raw_content.startsWith("/me ");
}; }
function make_emoji_span(codepoint, title, alt_text) { function make_emoji_span(codepoint, title, alt_text) {
return `<span aria-label="${_.escape(title)}" class="emoji emoji-${_.escape( return `<span aria-label="${_.escape(title)}" class="emoji emoji-${_.escape(
@ -425,7 +423,7 @@ function python_to_js_filter(pattern, url) {
return [final_regex, url]; return [final_regex, url];
} }
exports.update_realm_filter_rules = function (realm_filters) { export function update_realm_filter_rules(realm_filters) {
// Update the marked parser with our particular set of realm filters // Update the marked parser with our particular set of realm filters
realm_filter_map.clear(); realm_filter_map.clear();
realm_filter_list = []; realm_filter_list = [];
@ -445,9 +443,9 @@ exports.update_realm_filter_rules = function (realm_filters) {
} }
marked.InlineLexer.rules.zulip.realm_filters = marked_rules; marked.InlineLexer.rules.zulip.realm_filters = marked_rules;
}; }
exports.initialize = function (realm_filters, helper_config) { export function initialize(realm_filters, helper_config) {
helpers = helper_config; helpers = helper_config;
function disable_markdown_regex(rules, name) { function disable_markdown_regex(rules, name) {
@ -485,7 +483,7 @@ exports.initialize = function (realm_filters, helper_config) {
// In this scenario, the message has to be from the user, so the only // In this scenario, the message has to be from the user, so the only
// requirement should be that they have the setting on. // requirement should be that they have the setting on.
return exports.translate_emoticons_to_names(src); return translate_emoticons_to_names(src);
} }
// Disable lheadings // Disable lheadings
@ -506,7 +504,7 @@ exports.initialize = function (realm_filters, helper_config) {
// Disable autolink as (a) it is not used in our backend and (b) it interferes with @mentions // Disable autolink as (a) it is not used in our backend and (b) it interferes with @mentions
disable_markdown_regex(marked.InlineLexer.rules.zulip, "autolink"); disable_markdown_regex(marked.InlineLexer.rules.zulip, "autolink");
exports.update_realm_filter_rules(realm_filters); update_realm_filter_rules(realm_filters);
// Tell our fenced code preprocessor how to insert arbitrary // Tell our fenced code preprocessor how to insert arbitrary
// HTML into the output. This generated HTML is safe to not escape // HTML into the output. This generated HTML is safe to not escape
@ -531,6 +529,4 @@ exports.initialize = function (realm_filters, helper_config) {
renderer: r, renderer: r,
preprocessors: [preprocess_code_blocks, preprocess_translate_emoticons], preprocessors: [preprocess_code_blocks, preprocess_translate_emoticons],
}); });
}; }
window.markdown = exports;

View File

@ -8,6 +8,7 @@ const render_topic_edit_form = require("../templates/topic_edit_form.hbs");
const channel = require("./channel"); const channel = require("./channel");
const echo = require("./echo"); const echo = require("./echo");
const loading = require("./loading"); const loading = require("./loading");
const markdown = require("./markdown");
const message_viewport = require("./message_viewport"); const message_viewport = require("./message_viewport");
const rows = require("./rows"); const rows = require("./rows");

View File

@ -2,6 +2,7 @@ import * as emoji from "../shared/js/emoji";
import * as alert_words from "./alert_words"; import * as alert_words from "./alert_words";
import * as compose_fade from "./compose_fade"; import * as compose_fade from "./compose_fade";
import * as markdown from "./markdown";
import * as muting_ui from "./muting_ui"; import * as muting_ui from "./muting_ui";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";
import * as peer_data from "./peer_data"; import * as peer_data from "./peer_data";

View File

@ -15,6 +15,7 @@ const drafts = require("./drafts");
const echo = require("./echo"); const echo = require("./echo");
const emojisets = require("./emojisets"); const emojisets = require("./emojisets");
const invite = require("./invite"); const invite = require("./invite");
const markdown = require("./markdown");
const markdown_config = require("./markdown_config"); const markdown_config = require("./markdown_config");
const message_viewport = require("./message_viewport"); const message_viewport = require("./message_viewport");
const muting = require("./muting"); const muting = require("./muting");