2021-02-10 16:46:29 +01:00
|
|
|
import ClipboardJS from "clipboard";
|
2021-02-28 21:16:38 +01:00
|
|
|
import {parseISO, isValid} from "date-fns";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-10 16:46:29 +01:00
|
|
|
import copy_code_button from "../templates/copy_code_button.hbs";
|
|
|
|
import view_code_in_playground from "../templates/view_code_in_playground.hbs";
|
2020-07-28 01:48:47 +02:00
|
|
|
|
2021-02-10 16:46:29 +01:00
|
|
|
import * as people from "./people";
|
2021-02-28 00:37:11 +01:00
|
|
|
import * as rtl from "./rtl";
|
2021-02-10 16:46:29 +01:00
|
|
|
import * as settings_config from "./settings_config";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 01:14:36 +01:00
|
|
|
import * as timerender from "./timerender";
|
2021-02-28 00:42:57 +01:00
|
|
|
import * as user_groups from "./user_groups";
|
2020-08-20 16:11:08 +02:00
|
|
|
|
2020-05-21 06:17:24 +02:00
|
|
|
/*
|
|
|
|
rendered_markdown
|
|
|
|
|
2020-08-11 01:47:44 +02:00
|
|
|
This module provides a single function 'update_elements' to
|
2020-05-21 06:17:24 +02:00
|
|
|
update any renamed users/streams/groups etc. and other
|
|
|
|
dynamic parts of our rendered messages.
|
|
|
|
|
2020-08-11 01:47:49 +02:00
|
|
|
Use this module wherever some Markdown rendered content
|
2020-05-21 06:17:24 +02:00
|
|
|
is being displayed.
|
|
|
|
*/
|
|
|
|
|
2020-05-21 00:53:14 +02:00
|
|
|
function get_user_id_for_mention_button(elem) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const user_id_string = $(elem).attr("data-user-id");
|
2020-08-11 01:47:49 +02:00
|
|
|
// Handle legacy Markdown that was rendered before we cut
|
2020-05-21 00:53:14 +02:00
|
|
|
// over to using data-user-id.
|
2020-07-15 01:29:15 +02:00
|
|
|
const email = $(elem).attr("data-user-email");
|
2020-05-21 00:53:14 +02:00
|
|
|
|
|
|
|
if (user_id_string === "*" || email === "*") {
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_id_string) {
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseInt(user_id_string, 10);
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (email) {
|
|
|
|
// Will return undefined if there's no match
|
|
|
|
const user = people.get_by_email(email);
|
|
|
|
if (user) {
|
|
|
|
return user.user_id;
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_group_id_for_mention_button(elem) {
|
2020-07-15 01:29:15 +02:00
|
|
|
const user_group_id = $(elem).attr("data-user-group-id");
|
2020-05-21 00:53:14 +02:00
|
|
|
|
|
|
|
if (user_group_id) {
|
2020-10-07 09:17:30 +02:00
|
|
|
return Number.parseInt(user_group_id, 10);
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 04:15:27 +02:00
|
|
|
// Helper function to update a mentioned user's name.
|
2021-02-10 16:46:29 +01:00
|
|
|
export function set_name_in_mention_element(element, name) {
|
2020-07-15 01:29:15 +02:00
|
|
|
if ($(element).hasClass("silent")) {
|
2020-05-21 04:15:27 +02:00
|
|
|
$(element).text(name);
|
|
|
|
} else {
|
|
|
|
$(element).text("@" + name);
|
|
|
|
}
|
2021-02-10 16:46:29 +01:00
|
|
|
}
|
2020-05-21 04:15:27 +02:00
|
|
|
|
2021-02-10 16:46:29 +01:00
|
|
|
export const update_elements = (content) => {
|
2020-05-21 00:53:14 +02:00
|
|
|
// Set the rtl class if the text has an rtl direction
|
2020-07-15 01:29:15 +02:00
|
|
|
if (rtl.get_direction(content.text()) === "rtl") {
|
|
|
|
content.addClass("rtl");
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find(".user-mention").each(function () {
|
2020-05-21 00:53:14 +02:00
|
|
|
const user_id = get_user_id_for_mention_button(this);
|
|
|
|
// We give special highlights to the mention buttons
|
|
|
|
// that refer to the current user.
|
|
|
|
if (user_id === "*" || people.is_my_user_id(user_id)) {
|
|
|
|
// Either a wildcard mention or us, so mark it.
|
2020-07-15 01:29:15 +02:00
|
|
|
$(this).addClass("user-mention-me");
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
if (user_id && user_id !== "*" && !$(this).find(".highlight").length) {
|
|
|
|
// If it's a mention of a specific user, edit the
|
|
|
|
// mention text to show the user's current name,
|
|
|
|
// assuming that you're not searching for text
|
|
|
|
// inside the highlight.
|
|
|
|
const person = people.get_by_user_id(user_id, true);
|
|
|
|
if (person !== undefined) {
|
|
|
|
// Note that person might be undefined in some
|
|
|
|
// unpleasant corner cases involving data import.
|
2021-02-10 16:46:29 +01:00
|
|
|
set_name_in_mention_element(this, person.full_name);
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find(".user-group-mention").each(function () {
|
2020-05-21 00:53:14 +02:00
|
|
|
const user_group_id = get_user_group_id_for_mention_button(this);
|
|
|
|
const user_group = user_groups.get_user_group_from_id(user_group_id, true);
|
|
|
|
if (user_group === undefined) {
|
|
|
|
// This is a user group the current user doesn't have
|
|
|
|
// data on. This can happen when user groups are
|
|
|
|
// deleted.
|
|
|
|
blueslip.info("Rendered unexpected user group " + user_group_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const my_user_id = people.my_current_user_id();
|
|
|
|
// Mark user group you're a member of.
|
|
|
|
if (user_groups.is_member_of(user_group_id, my_user_id)) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(this).addClass("user-mention-me");
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user_group_id && !$(this).find(".highlight").length) {
|
|
|
|
// Edit the mention to show the current name for the
|
|
|
|
// user group, if its not in search.
|
|
|
|
$(this).text("@" + user_group.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find("a.stream").each(function () {
|
2020-10-07 09:17:30 +02:00
|
|
|
const stream_id = Number.parseInt($(this).attr("data-stream-id"), 10);
|
2020-05-21 00:53:14 +02:00
|
|
|
if (stream_id && !$(this).find(".highlight").length) {
|
|
|
|
// Display the current name for stream if it is not
|
|
|
|
// being displayed in search highlight.
|
|
|
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
|
|
|
if (stream_name !== undefined) {
|
|
|
|
// If the stream has been deleted,
|
|
|
|
// stream_data.maybe_get_stream_name might return
|
|
|
|
// undefined. Otherwise, display the current stream name.
|
|
|
|
$(this).text("#" + stream_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find("a.stream-topic").each(function () {
|
2020-10-07 09:17:30 +02:00
|
|
|
const stream_id = Number.parseInt($(this).attr("data-stream-id"), 10);
|
2020-05-21 00:53:14 +02:00
|
|
|
if (stream_id && !$(this).find(".highlight").length) {
|
|
|
|
// Display the current name for stream if it is not
|
|
|
|
// being displayed in search highlight.
|
|
|
|
const text = $(this).text();
|
2020-07-15 01:29:15 +02:00
|
|
|
const topic = text.split(">", 2)[1];
|
2020-05-21 00:53:14 +02:00
|
|
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
|
|
|
if (stream_name !== undefined) {
|
|
|
|
// If the stream has been deleted,
|
|
|
|
// stream_data.maybe_get_stream_name might return
|
|
|
|
// undefined. Otherwise, display the current stream name.
|
2020-07-15 01:29:15 +02:00
|
|
|
$(this).text("#" + stream_name + " > " + topic);
|
2020-05-21 00:53:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find("time").each(function () {
|
2018-02-13 00:33:36 +01:00
|
|
|
// Populate each timestamp span with mentioned time
|
|
|
|
// in user's local timezone.
|
2020-07-15 01:29:15 +02:00
|
|
|
const time_str = $(this).attr("datetime");
|
2020-06-18 01:32:24 +02:00
|
|
|
if (time_str === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:20:46 +02:00
|
|
|
const timestamp = parseISO(time_str);
|
|
|
|
if (isValid(timestamp)) {
|
2018-02-13 00:33:36 +01:00
|
|
|
const text = $(this).text();
|
2020-07-15 23:10:20 +02:00
|
|
|
const rendered_time = timerender.render_markdown_timestamp(timestamp, text);
|
2018-02-13 00:33:36 +01:00
|
|
|
$(this).text(rendered_time.text);
|
2020-07-15 01:29:15 +02:00
|
|
|
$(this).attr("title", rendered_time.title);
|
2018-02-13 00:33:36 +01:00
|
|
|
} else {
|
2020-07-06 17:17:31 +02:00
|
|
|
// This shouldn't happen. If it does, we're very interested in debugging it.
|
2020-09-29 22:20:46 +02:00
|
|
|
blueslip.error(`Could not parse datetime supplied by backend: ${time_str}`);
|
2018-02-13 00:33:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find("span.timestamp-error").each(function () {
|
|
|
|
const time_str = $(this).text().replace("Invalid time format: ", "");
|
2020-07-16 22:40:18 +02:00
|
|
|
const text = i18n.t("Invalid time format: __timestamp__", {timestamp: time_str});
|
2020-07-06 17:07:44 +02:00
|
|
|
$(this).text(text);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
content.find("div.spoiler-header").each(function () {
|
2020-07-06 17:11:58 +02:00
|
|
|
// If a spoiler block has no header content, it should have a default header.
|
|
|
|
// We do this client side to allow for i18n by the client.
|
2020-07-22 03:39:41 +02:00
|
|
|
if ($(this).html().trim().length === 0) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$(this).append(`<p>${i18n.t("Spoiler")}</p>`);
|
2020-04-04 22:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the expand/collapse button to spoiler blocks
|
2020-07-15 00:34:28 +02:00
|
|
|
const toggle_button_html =
|
|
|
|
'<span class="spoiler-button" aria-expanded="false"><span class="spoiler-arrow"></span></span>';
|
2020-04-04 22:14:34 +02:00
|
|
|
$(this).prepend(toggle_button_html);
|
|
|
|
});
|
|
|
|
|
2020-09-06 09:00:45 +02:00
|
|
|
// Display the view-code-in-playground and the copy-to-clipboard button inside the div.codehilite element.
|
2020-08-20 16:11:08 +02:00
|
|
|
content.find("div.codehilite").each(function () {
|
2020-09-06 09:00:45 +02:00
|
|
|
const $codehilite = $(this);
|
|
|
|
const $pre = $codehilite.find("pre");
|
|
|
|
const fenced_code_lang = $codehilite.data("code-language");
|
|
|
|
if (fenced_code_lang !== undefined) {
|
|
|
|
const playground_info = settings_config.get_playground_info_for_languages(
|
|
|
|
fenced_code_lang,
|
|
|
|
);
|
|
|
|
if (playground_info !== undefined) {
|
|
|
|
// If a playground is configured for this language,
|
|
|
|
// offer to view the code in that playground. When
|
|
|
|
// there are multiple playgrounds, we display a
|
|
|
|
// popover listing the options.
|
|
|
|
let title = i18n.t("View in playground");
|
|
|
|
const view_in_playground_button = $(view_code_in_playground());
|
|
|
|
$pre.prepend(view_in_playground_button);
|
|
|
|
if (playground_info.length === 1) {
|
|
|
|
title = i18n.t(`View in ${playground_info[0].name}`);
|
|
|
|
} else {
|
|
|
|
view_in_playground_button.attr("aria-haspopup", "true");
|
|
|
|
}
|
|
|
|
view_in_playground_button.attr("title", title);
|
|
|
|
view_in_playground_button.attr("aria-label", title);
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 16:11:08 +02:00
|
|
|
const copy_button = $(copy_code_button());
|
2020-09-06 09:00:45 +02:00
|
|
|
$pre.prepend(copy_button);
|
2020-08-20 16:11:08 +02:00
|
|
|
new ClipboardJS(copy_button[0], {
|
|
|
|
text(copy_element) {
|
|
|
|
return $(copy_element).siblings("code").text();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 00:53:14 +02:00
|
|
|
// Display emoji (including realm emoji) as text if
|
|
|
|
// page_params.emojiset is 'text'.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (page_params.emojiset === "text") {
|
2020-05-21 00:53:14 +02:00
|
|
|
content.find(".emoji").replaceWith(function () {
|
|
|
|
const text = $(this).attr("title");
|
|
|
|
return ":" + text + ":";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|