2017-06-09 06:02:30 +08:00
|
|
|
import { performEmojiUnescape } from "pretty-text/emoji";
|
|
|
|
|
|
|
|
const rule = {
|
|
|
|
tag: "quote",
|
|
|
|
|
2017-11-21 05:28:03 +08:00
|
|
|
before(state, tagInfo) {
|
2017-07-22 04:09:27 +08:00
|
|
|
const attrs = tagInfo.attrs;
|
|
|
|
let options = state.md.options.discourse;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
let quoteInfo = attrs["_default"];
|
2017-11-21 05:28:03 +08:00
|
|
|
let username,
|
|
|
|
postNumber,
|
|
|
|
topicId,
|
|
|
|
avatarImg,
|
|
|
|
primaryGroupName,
|
|
|
|
full,
|
|
|
|
displayName;
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
if (quoteInfo) {
|
|
|
|
let split = quoteInfo.split(/\,\s*/);
|
|
|
|
username = split[0];
|
|
|
|
|
|
|
|
let i;
|
|
|
|
for (i = 1; i < split.length; i++) {
|
|
|
|
if (split[i].indexOf("post:") === 0) {
|
|
|
|
postNumber = parseInt(split[i].substr(5), 10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (split[i].indexOf("topic:") === 0) {
|
|
|
|
topicId = parseInt(split[i].substr(6), 10);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:27:28 +08:00
|
|
|
if (/full:\s*true/.test(split[i])) {
|
2017-06-09 06:02:30 +08:00
|
|
|
full = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:51:40 +08:00
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2017-11-21 05:28:03 +08:00
|
|
|
if (options.formatUsername) {
|
|
|
|
displayName = options.formatUsername(username);
|
|
|
|
} else {
|
|
|
|
displayName = username;
|
|
|
|
}
|
|
|
|
|
2017-11-03 21:51:40 +08:00
|
|
|
let token = state.push("bbcode_open", "aside", 1);
|
|
|
|
token.attrs = [];
|
|
|
|
|
|
|
|
if (primaryGroupName && primaryGroupName.length !== 0) {
|
|
|
|
token.attrs.push(["class", `quote group-${primaryGroupName}`]);
|
|
|
|
} else {
|
2018-04-03 01:50:15 +08:00
|
|
|
token.attrs.push(["class", "quote no-group"]);
|
2017-11-03 21:51:40 +08:00
|
|
|
}
|
2017-06-09 06:02:30 +08:00
|
|
|
|
2020-01-22 22:10:23 +08:00
|
|
|
if (username) {
|
|
|
|
token.attrs.push(["data-username", username]);
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:02:30 +08:00
|
|
|
if (postNumber) {
|
|
|
|
token.attrs.push(["data-post", postNumber]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (topicId) {
|
|
|
|
token.attrs.push(["data-topic", topicId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (full) {
|
|
|
|
token.attrs.push(["data-full", "true"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (username) {
|
|
|
|
let offTopicQuote =
|
|
|
|
options.topicId &&
|
|
|
|
postNumber &&
|
|
|
|
options.getTopicInfo &&
|
|
|
|
topicId !== options.topicId;
|
|
|
|
|
|
|
|
// on topic quote
|
|
|
|
token = state.push("quote_header_open", "div", 1);
|
|
|
|
token.attrs = [["class", "title"]];
|
|
|
|
|
|
|
|
token = state.push("quote_controls_open", "div", 1);
|
|
|
|
token.attrs = [["class", "quote-controls"]];
|
|
|
|
|
2020-05-07 22:34:36 +08:00
|
|
|
state.push("quote_controls_close", "div", -1);
|
2017-06-09 06:02:30 +08:00
|
|
|
|
|
|
|
if (avatarImg) {
|
|
|
|
token = state.push("html_inline", "", 0);
|
|
|
|
token.content = avatarImg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offTopicQuote) {
|
|
|
|
const topicInfo = options.getTopicInfo(topicId);
|
|
|
|
if (topicInfo) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let href = topicInfo.href;
|
2017-06-09 06:02:30 +08:00
|
|
|
if (postNumber > 0) {
|
|
|
|
href += "/" + postNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = topicInfo.title;
|
|
|
|
|
|
|
|
if (options.enableEmoji) {
|
|
|
|
title = performEmojiUnescape(topicInfo.title, {
|
|
|
|
getURL: options.getURL,
|
2019-05-21 22:56:51 +08:00
|
|
|
emojiSet: options.emojiSet,
|
2021-03-03 03:04:16 +08:00
|
|
|
emojiCDNUrl: options.emojiCDNUrl,
|
2019-12-04 00:32:33 +08:00
|
|
|
enableEmojiShortcuts: options.enableEmojiShortcuts,
|
|
|
|
inlineEmoji: options.inlineEmoji,
|
2022-02-09 19:18:59 +08:00
|
|
|
lazy: true,
|
2017-06-09 06:02:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
token = state.push("link_open", "a", 1);
|
|
|
|
token.attrs = [["href", href]];
|
|
|
|
token.block = false;
|
|
|
|
|
|
|
|
token = state.push("html_inline", "", 0);
|
|
|
|
token.content = title;
|
|
|
|
|
|
|
|
token = state.push("link_close", "a", -1);
|
|
|
|
token.block = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token = state.push("text", "", 0);
|
2017-11-21 05:28:03 +08:00
|
|
|
token.content = ` ${displayName}:`;
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:34:36 +08:00
|
|
|
state.push("quote_header_close", "div", -1);
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:34:36 +08:00
|
|
|
state.push("bbcode_open", "blockquote", 1);
|
2017-06-09 06:02:30 +08:00
|
|
|
},
|
|
|
|
|
2021-11-13 21:01:55 +08:00
|
|
|
after(state) {
|
2017-06-09 06:02:30 +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;
|
2021-03-03 03:04:16 +08:00
|
|
|
opts.emojiCDNUrl = siteSettings.external_emoji_url;
|
2019-05-21 22:56:51 +08:00
|
|
|
opts.enableEmojiShortcuts = siteSettings.enable_emoji_shortcuts;
|
2019-12-04 00:32:33 +08:00
|
|
|
opts.inlineEmoji = siteSettings.enable_inline_emoji_translation;
|
2017-06-09 06:02:30 +08:00
|
|
|
});
|
|
|
|
|
2018-03-14 01:07:51 +08:00
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
md.block.bbcode.ruler.push("quotes", rule);
|
2017-06-09 06:02:30 +08:00
|
|
|
});
|
2017-10-16 06:34:30 +08:00
|
|
|
|
2022-02-07 09:28:26 +08:00
|
|
|
helper.allowList(["img[class=avatar]", "img[loading=lazy]"]);
|
2020-10-28 10:22:06 +08:00
|
|
|
helper.allowList({
|
2017-11-03 21:51:40 +08:00
|
|
|
custom(tag, name, value) {
|
|
|
|
if (tag === "aside" && name === "class") {
|
2018-04-03 02:01:55 +08:00
|
|
|
return (
|
|
|
|
value === "quote no-group" || !!/^quote group\-(.+)$/.exec(value)
|
|
|
|
);
|
2017-11-03 21:51:40 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-06-09 06:02:30 +08:00
|
|
|
}
|