Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handles various Discord markdowns #114

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/classes/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const CODEBLOCK_REPLACER = [/```.*?```/gs, 'コードブロック '] as const;

const SPOILER_REPLACER = [/\|\|.*?\|\|/g, ' '] as const;

const MARKDOWN2_REPLACER = [/([*_~`])\1(.+?)\1\1/gs, '$2 '] as const;
const MARKDOWN1_REPLACER = [/([_`])(.+?)\1/gs, '$2 '] as const;
Comment on lines +21 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/([*_~`]{2}|[_`])(.+?)\1/gs

とすれば一つにまとめられるのでは?

const ITALIC_REPLACER = [/\*(.+?)\*/g, '$1 '] as const;
// eslint-disable-next-line no-irregular-whitespace
const QUOTE_REPLACER = [/^> (.*[^\s ])$/g, '$1 '] as const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これだと末尾に空白文字のある行だけが置換されません。
例:

> a 
> b

(> aの行の末尾にだけスペースがある)
だと、> bの行はマッチしますが、> aの行はマッチしません。


const GUILD_EMOJI_REPLACER = (dict: Collection<string, string>) =>
[
/<:(.+?):(\d{18})>/g,
Expand Down Expand Up @@ -115,7 +121,11 @@ export default class Preprocessor {
replaced = replaced
.replace(...URL_REPLACER)
.replace(...CODEBLOCK_REPLACER)
.replace(...SPOILER_REPLACER);
.replace(...SPOILER_REPLACER)
.replace(...MARKDOWN2_REPLACER)
.replace(...MARKDOWN1_REPLACER)
.replace(...ITALIC_REPLACER)
.replace(...QUOTE_REPLACER);
if (this.room.guildSettings?.readEmojis === true) {
replaced = replaced.replace(...this.#guildEmojiReplacer);
}
Expand Down