markdown: Format spoilers for desktop notifications.

We hide the spoiler content in browser/desktop notifications.

Note: its not worth adding zjquery tests for this bit of code because
the tests do not operate on the actual data and are likely to get stale
if we change the syntax for spoilers.
This commit is contained in:
Rohitt Vashishtha 2020-07-15 05:38:01 +05:30 committed by Tim Abbott
parent 2f66c825a2
commit ea1c178305
4 changed files with 48 additions and 0 deletions

View File

@ -24,6 +24,8 @@ zrequire('muting');
zrequire('stream_data');
zrequire('people');
zrequire('ui');
zrequire('spoilers');
spoilers.hide_spoilers_in_notification = () => {};
zrequire('notifications');

View File

@ -0,0 +1,32 @@
set_global('$', global.make_zjquery());
zrequire('spoilers');
// This function is taken from rendered_markdown.js and slightly modified.
const $array = (array) => {
const each = (func) => {
array.forEach((elem, index) => {
func.call(this, index, elem);
});
};
return {each};
};
const get_spoiler_elem = (title) => {
const block = $.create(`block-${title}`);
const header = $.create(`header-${title}`);
const content = $.create(`content-${title}`);
header.text(title);
block.set_find_results('.spoiler-header', header);
block.set_find_results('.spoiler-content', content);
return block;
};
run_test('hide spoilers in notifications', () => {
const root = $.create('root element');
const spoiler_1 = get_spoiler_elem('this is the title');
const spoiler_2 = get_spoiler_elem('');
root.set_find_results('.spoiler-block', $array([spoiler_1, spoiler_2]));
spoilers.hide_spoilers_in_notification(root);
assert.equal(spoiler_1.find('.spoiler-header').text(), 'this is the title (…)');
assert.equal(spoiler_2.find('.spoiler-header').text(), '(…)');
});

View File

@ -299,6 +299,7 @@ function process_notification(notification) {
// Convert the content to plain text, replacing emoji with their alt text
content = $('<div/>').html(message.content);
ui.replace_emoji_with_text(content);
spoilers.hide_spoilers_in_notification(content);
content = content.text();
const topic = message.topic;

View File

@ -33,6 +33,19 @@ function expand_spoiler(spoiler) {
});
}
exports.hide_spoilers_in_notification = (content) => {
content.find('.spoiler-block').each((i, elem) => {
$(elem).find('.spoiler-content').remove();
let text = $(elem).find('.spoiler-header').text().trim();
if (text.length > 0) {
text = `${text} `;
}
text = `${text}(…)`;
$(elem).find('.spoiler-header').text(text);
});
return content;
};
exports.initialize = function () {
$("body").on("click", ".spoiler-header", function (e) {
const button = $(this).children('.spoiler-button');