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
2018-06-15 17:03:24 +02:00

43 lines
1.1 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 = "";
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 = [
post.get("username"),
`post:${post.get("post_number")}`,
`topic:${post.get("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`;
}
};