discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js.es6

174 lines
4.5 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { performEmojiUnescape } from "pretty-text/emoji";
const rule = {
2018-06-15 23:03:24 +08:00
tag: "quote",
before(state, tagInfo) {
2017-07-22 04:09:27 +08:00
const attrs = tagInfo.attrs;
let options = state.md.options.discourse;
2018-06-15 23:03:24 +08:00
let quoteInfo = attrs["_default"];
let username,
postNumber,
topicId,
avatarImg,
primaryGroupName,
full,
displayName;
if (quoteInfo) {
let split = quoteInfo.split(/\,\s*/);
username = split[0];
let i;
2018-06-15 23:03:24 +08:00
for (i = 1; i < split.length; i++) {
if (split[i].indexOf("post:") === 0) {
2018-06-15 23:03:24 +08:00
postNumber = parseInt(split[i].substr(5), 10);
continue;
}
if (split[i].indexOf("topic:") === 0) {
2018-06-15 23:03:24 +08:00
topicId = parseInt(split[i].substr(6), 10);
continue;
}
if (/full:\s*true/.test(split[i])) {
full = true;
continue;
}
}
}
if (options.lookupAvatarByPostNumber) {
// client-side, we can retrieve the avatar from the post
avatarImg = options.lookupAvatarByPostNumber(postNumber, topicId);
} else if (options.lookupAvatar) {
// server-side, we need to lookup the avatar from the username
avatarImg = options.lookupAvatar(username);
}
if (options.lookupPrimaryUserGroupByPostNumber) {
// client-side, we can retrieve the primary user group from the post
2018-06-15 23:03:24 +08:00
primaryGroupName = options.lookupPrimaryUserGroupByPostNumber(
postNumber,
topicId
);
} else if (options.lookupPrimaryUserGroup) {
// server-side, we need to lookup the primary user group from the username
primaryGroupName = options.lookupPrimaryUserGroup(username);
}
if (options.formatUsername) {
displayName = options.formatUsername(username);
} else {
displayName = username;
}
2018-06-15 23:03:24 +08:00
let token = state.push("bbcode_open", "aside", 1);
token.attrs = [];
if (primaryGroupName && primaryGroupName.length !== 0) {
2018-06-15 23:03:24 +08:00
token.attrs.push(["class", `quote group-${primaryGroupName}`]);
} else {
2018-06-15 23:03:24 +08:00
token.attrs.push(["class", "quote no-group"]);
}
if (postNumber) {
2018-06-15 23:03:24 +08:00
token.attrs.push(["data-post", postNumber]);
}
if (topicId) {
2018-06-15 23:03:24 +08:00
token.attrs.push(["data-topic", topicId]);
}
if (full) {
2018-06-15 23:03:24 +08:00
token.attrs.push(["data-full", "true"]);
}
if (username) {
2018-06-15 23:03:24 +08:00
let offTopicQuote =
options.topicId &&
postNumber &&
options.getTopicInfo &&
topicId !== options.topicId;
// on topic quote
2018-06-15 23:03:24 +08:00
token = state.push("quote_header_open", "div", 1);
token.attrs = [["class", "title"]];
2018-06-15 23:03:24 +08:00
token = state.push("quote_controls_open", "div", 1);
token.attrs = [["class", "quote-controls"]];
2018-06-15 23:03:24 +08:00
token = state.push("quote_controls_close", "div", -1);
if (avatarImg) {
2018-06-15 23:03:24 +08:00
token = state.push("html_inline", "", 0);
token.content = avatarImg;
}
if (offTopicQuote) {
const topicInfo = options.getTopicInfo(topicId);
if (topicInfo) {
var href = topicInfo.href;
2018-06-15 23:03:24 +08:00
if (postNumber > 0) {
href += "/" + postNumber;
}
let title = topicInfo.title;
if (options.enableEmoji) {
title = performEmojiUnescape(topicInfo.title, {
2018-06-15 23:03:24 +08:00
getURL: options.getURL,
emojiSet: options.emojiSet
});
}
2018-06-15 23:03:24 +08:00
token = state.push("link_open", "a", 1);
token.attrs = [["href", href]];
token.block = false;
2018-06-15 23:03:24 +08:00
token = state.push("html_inline", "", 0);
token.content = title;
2018-06-15 23:03:24 +08:00
token = state.push("link_close", "a", -1);
token.block = false;
}
} else {
2018-06-15 23:03:24 +08:00
token = state.push("text", "", 0);
token.content = ` ${displayName}:`;
}
2018-06-15 23:03:24 +08:00
token = state.push("quote_header_close", "div", -1);
}
2018-06-15 23:03:24 +08:00
token = state.push("bbcode_open", "blockquote", 1);
},
after: function(state) {
2018-06-15 23:03:24 +08:00
state.push("bbcode_close", "blockquote", -1);
state.push("bbcode_close", "aside", -1);
}
};
export function setup(helper) {
helper.registerOptions((opts, siteSettings) => {
opts.enableEmoji = siteSettings.enable_emoji;
opts.emojiSet = siteSettings.emoji_set;
});
helper.registerPlugin(md => {
2018-06-15 23:03:24 +08:00
md.block.bbcode.ruler.push("quotes", rule);
});
2018-06-15 23:03:24 +08:00
helper.whiteList(["img[class=avatar]"]);
helper.whiteList({
custom(tag, name, value) {
2018-06-15 23:03:24 +08:00
if (tag === "aside" && name === "class") {
return (
value === "quote no-group" || !!/^quote group\-(.+)$/.exec(value)
);
}
}
});
}