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

@@ -1,12 +1,10 @@
"use strict";
import {isValid} from "date-fns";
import katex from "katex";
import _ from "lodash";
const {isValid} = require("date-fns");
const katex = require("katex");
const _ = require("lodash");
const emoji = require("../shared/js/emoji");
const fenced_code = require("../shared/js/fenced_code");
const marked = require("../third/marked/lib/marked");
import * as emoji from "../shared/js/emoji";
import * as fenced_code from "../shared/js/fenced_code";
import marked from "../third/marked/lib/marked";
// This contains zulip's frontend Markdown implementation; see
// 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*/,
];
exports.translate_emoticons_to_names = (text) => {
export function translate_emoticons_to_names(text) {
// Translates emoticons in a string to their colon syntax.
let translated = text;
let replacement_text;
@@ -76,9 +74,9 @@ exports.translate_emoticons_to_names = (text) => {
}
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
// backend Markdown processor can correctly handle.
// 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 markedup !== undefined || false_filter_match !== undefined;
};
}
exports.apply_markdown = function (message) {
export function apply_markdown(message) {
message_store.init_booleans(message);
const options = {
@@ -201,10 +199,10 @@ exports.apply_markdown = function (message) {
};
// Our python-markdown processor appends two \n\n to input
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") {
message.topic_links = [];
return;
@@ -239,11 +237,11 @@ exports.add_topic_links = function (message) {
}
message.topic_links = links;
};
}
exports.is_status_message = function (raw_content) {
export function is_status_message(raw_content) {
return raw_content.startsWith("/me ");
};
}
function make_emoji_span(codepoint, title, alt_text) {
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];
}
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
realm_filter_map.clear();
realm_filter_list = [];
@@ -445,9 +443,9 @@ exports.update_realm_filter_rules = function (realm_filters) {
}
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;
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
// requirement should be that they have the setting on.
return exports.translate_emoticons_to_names(src);
return translate_emoticons_to_names(src);
}
// 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_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
// 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,
preprocessors: [preprocess_code_blocks, preprocess_translate_emoticons],
});
};
window.markdown = exports;
}