Skip to content

Commit

Permalink
feat: Keep spoiler & Split into cutContent
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina committed Aug 27, 2023
1 parent 17f1537 commit e084f40
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ const sendWebhook = async (
console.log(await followupRes.text());
};

const cutContent = (content: string): string => {
const spoilerMarks = [];
const spoilerSpans: [number, number][] = [];
const chars = [...content];
let isInCodeBlock = false;
for (let i = 0; i < chars.length - 1; ++i) {
if (chars[i] === "|" && chars[i + 1] === "|" && !isInCodeBlock) {
const start = spoilerMarks.pop();
if (start !== undefined) {
spoilerSpans.push([start, i + 2]);
} else {
spoilerMarks.push(i);
}
++i;
}
if (chars[i] === '`' && chars[i + 1] === '`' && chars[i + 2] === '`' && chars[i + 3] === '\n') {
isInCodeBlock = !isInCodeBlock;
if (isInCodeBlock) {
spoilerMarks.pop();
}
i += 3;
}
}

const PREVIEW_LENGTH = 20;
const isCuttingSpoiler = spoilerSpans.some(([start, end]) => start <= PREVIEW_LENGTH && PREVIEW_LENGTH < end);

let cut = "";
cut += content.substring(0, PREVIEW_LENGTH);
if (isCuttingSpoiler) {
cut += "||";
}
if (PREVIEW_LENGTH <= content.length) {
cut += "...";
}
return cut;
};

export const makeCommands = (options: WebhookOptions): InteractionHandlers => [
[
{
Expand Down Expand Up @@ -128,11 +166,7 @@ export const makeCommands = (options: WebhookOptions): InteractionHandlers => [

let previewContent = "";
if (message.content.length !== 0) {
const PREVIEW_LENGTH = 20;
previewContent += message.content.substring(0, PREVIEW_LENGTH);
if (PREVIEW_LENGTH <= message.content.length) {
previewContent += "...";
}
previewContent += cutContent(message.content);
}

void sendWebhook({
Expand Down

0 comments on commit e084f40

Please sign in to comment.