This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/quote.js.es6
Bianca Nenciu 7b7e1717f2
FIX: Quoting a quote preserves the original post information (#8746)
Let's say post #2 quotes post number #1. If a user decides to quote the
quote in post #2, it should keep the information of post #1
("user_1, post: 1, topic: X"), instead of replacing with current post
info ("user_2, post: 2, topic: X").
2020-01-22 16:10:23 +02:00

44 lines
1.2 KiB
JavaScript

export default {
REGEXP: /\[quote=([^\]]*)\]((?:[\s\S](?!\[quote=[^\]]*\]))*?)\[\/quote\]/im,
// Build the BBCode quote around the selected text
build(post, contents, opts) {
if (!post) {
return "";
}
if (!contents) contents = "";
if (!opts) opts = {};
const sansQuotes = contents.replace(this.REGEXP, "").trim();
if (sansQuotes.length === 0) {
return "";
}
// Strip the HTML from cooked
const stripped = $("<div/>")
.html(post.get("cooked"))
.text();
// Let's remove any non-word characters as a kind of hash.
// Yes it's not accurate but it should work almost every time we need it to.
// It would be unlikely that the user would quote another post that matches in exactly this way.
const sameContent =
stripped.replace(/\W/g, "") === contents.replace(/\W/g, "");
const params = [
opts.username || post.username,
`post:${opts.post || post.post_number}`,
`topic:${opts.topic || post.topic_id}`
];
opts = opts || {};
if (opts["full"] || sameContent) params.push("full:true");
return `[quote="${params.join(", ")}"]\n${
opts["raw"] ? contents : sansQuotes
}\n[/quote]\n\n`;
}
};