mobile sharing: Move fenced_code.js to shared/js.

We also take fenced_code out of the global namespace,
since it only requires katex and underscore.

And we fix the exports to be ES6 style.
This commit is contained in:
Steve Howell 2020-07-23 20:41:45 +00:00 committed by Anders Kaseorg
parent c50dbf8297
commit cc31403112
11 changed files with 25 additions and 26 deletions

View File

@ -147,7 +147,6 @@
"favicon": false, "favicon": false,
"feature_flags": false, "feature_flags": false,
"feedback_widget": false, "feedback_widget": false,
"fenced_code": false,
"flatpickr": false, "flatpickr": false,
"floating_recipient_bar": false, "floating_recipient_bar": false,
"gear_menu": false, "gear_menu": false,
@ -162,7 +161,6 @@
"input_pill": false, "input_pill": false,
"invite": false, "invite": false,
"jQuery": false, "jQuery": false,
"katex": false,
"keydown_util": false, "keydown_util": false,
"lightbox": false, "lightbox": false,
"list_cursor": false, "list_cursor": false,

View File

@ -23,7 +23,6 @@ zrequire("compose_ui");
zrequire("compose"); zrequire("compose");
zrequire("compose_state"); zrequire("compose_state");
zrequire("compose_actions"); zrequire("compose_actions");
zrequire("fenced_code");
zrequire("stream_data"); zrequire("stream_data");
set_global("document", "document-stub"); set_global("document", "document-stub");

View File

@ -1,4 +1,4 @@
zrequire("fenced_code"); const fenced_code = zrequire("fenced_code", "shared/js/fenced_code");
run_test("get_unused_fence", () => { run_test("get_unused_fence", () => {
assert.equal(fenced_code.get_unused_fence("```js\nsomething\n```"), "`".repeat(4)); assert.equal(fenced_code.get_unused_fence("```js\nsomething\n```"), "`".repeat(4));

View File

@ -1,12 +1,11 @@
zrequire("hash_util"); zrequire("hash_util");
set_global("katex", zrequire("katex", "katex/dist/katex.min.js"));
set_global("marked", zrequire("marked", "third/marked/lib/marked")); set_global("marked", zrequire("marked", "third/marked/lib/marked"));
const emoji_codes = zrequire("emoji_codes", "generated/emoji/emoji_codes.json"); const emoji_codes = zrequire("emoji_codes", "generated/emoji/emoji_codes.json");
const fenced_code = zrequire("fenced_code", "shared/js/fenced_code");
const markdown_config = zrequire("markdown_config"); const markdown_config = zrequire("markdown_config");
zrequire("emoji"); zrequire("emoji");
zrequire("fenced_code");
zrequire("markdown"); zrequire("markdown");
zrequire("message_store"); zrequire("message_store");
zrequire("people"); zrequire("people");

View File

@ -5,27 +5,28 @@
There may be a better way. There may be a better way.
*/ */
const rewiremock = require("rewiremock/node");
const markdown_config = zrequire("markdown_config"); const markdown_config = zrequire("markdown_config");
set_global("marked", zrequire("marked", "third/marked/lib/marked")); set_global("marked", zrequire("marked", "third/marked/lib/marked"));
set_global("page_params", {}); set_global("page_params", {});
zrequire("emoji"); zrequire("emoji");
zrequire("fenced_code");
zrequire("hash_util"); zrequire("hash_util");
zrequire("message_store"); zrequire("message_store");
zrequire("people"); zrequire("people");
zrequire("stream_data"); zrequire("stream_data");
zrequire("user_groups"); zrequire("user_groups");
set_global("katex", { const markdown = rewiremock.proxy(() => zrequire("markdown"), {
renderToString: () => { "katex/dist/katex.min.js": {
throw new Error("some-exception"); renderToString: () => {
throw new Error("some-exception");
},
}, },
}); });
const markdown = zrequire("markdown");
markdown.initialize([], markdown_config.get_helpers()); markdown.initialize([], markdown_config.get_helpers());
run_test("katex_throws_unexpected_exceptions", () => { run_test("katex_throws_unexpected_exceptions", () => {

View File

@ -97,7 +97,6 @@ zrequire("user_groups");
zrequire("unread"); zrequire("unread");
zrequire("bot_data"); zrequire("bot_data");
set_global("marked", zrequire("marked", "third/marked/lib/marked")); set_global("marked", zrequire("marked", "third/marked/lib/marked"));
zrequire("fenced_code");
zrequire("markdown"); zrequire("markdown");
zrequire("upload"); zrequire("upload");
zrequire("compose"); zrequire("compose");

View File

@ -77,7 +77,6 @@ import "../narrow.js";
import "../reload_state.js"; import "../reload_state.js";
import "../reload.js"; import "../reload.js";
import "../compose_fade.js"; import "../compose_fade.js";
import "../fenced_code.js";
import "../markdown.js"; import "../markdown.js";
import "../local_message.js"; import "../local_message.js";
import "../echo.js"; import "../echo.js";

View File

@ -1,4 +1,5 @@
const autosize = require("autosize"); const autosize = require("autosize");
const fenced_code = require("../shared/js/fenced_code");
exports.blur_compose_inputs = function () { exports.blur_compose_inputs = function () {
$(".message_comp").find("input, textarea, button, #private_message_recipient").trigger("blur"); $(".message_comp").find("input, textarea, button, #private_message_recipient").trigger("blur");

View File

@ -44,7 +44,6 @@ declare let emoji_picker: any;
declare let favicon: any; declare let favicon: any;
declare let feature_flags: any; declare let feature_flags: any;
declare let feedback_widget: any; declare let feedback_widget: any;
declare let fenced_code: any;
declare let floating_recipient_bar: any; declare let floating_recipient_bar: any;
declare let gear_menu: any; declare let gear_menu: any;
declare let hashchange: any; declare let hashchange: any;

View File

@ -1,3 +1,6 @@
const katex = require("katex/dist/katex.min.js");
const fenced_code = require("../shared/js/fenced_code");
// 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
// main piece in rendering markdown client-side is // main piece in rendering markdown client-side is

View File

@ -1,3 +1,6 @@
import _ from "underscore";
import katex from "katex/dist/katex.min.js";
// Parsing routine that can be dropped in to message parsing // Parsing routine that can be dropped in to message parsing
// and formats code blocks // and formats code blocks
// //
@ -27,7 +30,7 @@ let stash_func = function (text) {
return text; return text;
}; };
exports.wrap_code = function (code) { export function wrap_code(code) {
// Trim trailing \n until there's just one left // Trim trailing \n until there's just one left
// This mirrors how pygments handles code input // This mirrors how pygments handles code input
return ( return (
@ -35,7 +38,7 @@ exports.wrap_code = function (code) {
_.escape(code.replace(/^\n+|\n+$/g, "")) + _.escape(code.replace(/^\n+|\n+$/g, "")) +
"\n</code></pre></div>\n" "\n</code></pre></div>\n"
); );
}; }
function wrap_quote(text) { function wrap_quote(text) {
const paragraphs = text.split("\n\n"); const paragraphs = text.split("\n\n");
@ -80,11 +83,11 @@ function wrap_spoiler(header, text, stash_func) {
return output.join("\n\n"); return output.join("\n\n");
} }
exports.set_stash_func = function (stash_handler) { export function set_stash_func(stash_handler) {
stash_func = stash_handler; stash_func = stash_handler;
}; }
exports.process_fenced_code = function (content) { export function process_fenced_code(content) {
const input = content.split("\n"); const input = content.split("\n");
const output = []; const output = [];
const handler_stack = []; const handler_stack = [];
@ -166,7 +169,7 @@ exports.process_fenced_code = function (content) {
}, },
done() { done() {
const text = exports.wrap_code(lines.join("\n")); const text = wrap_code(lines.join("\n"));
// insert safe HTML that is passed through the parsing // insert safe HTML that is passed through the parsing
const placeholder = stash_func(text, true); const placeholder = stash_func(text, true);
output_lines.push(""); output_lines.push("");
@ -222,10 +225,10 @@ exports.process_fenced_code = function (content) {
} }
return output.join("\n"); return output.join("\n");
}; }
const fence_length_re = /^ {0,3}(`{3,})/gm; const fence_length_re = /^ {0,3}(`{3,})/gm;
exports.get_unused_fence = (content) => { export function get_unused_fence(content) {
// we only return ``` fences, not ~~~. // we only return ``` fences, not ~~~.
let length = 3; let length = 3;
let match; let match;
@ -234,6 +237,4 @@ exports.get_unused_fence = (content) => {
length = Math.max(length, match[1].length + 1); length = Math.max(length, match[1].length + 1);
} }
return "`".repeat(length); return "`".repeat(length);
}; }
window.fenced_code = exports;