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

メンションの前に:がつくとメンションできない問題を修正 #4399

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions src/lib/markdown/internalLinkEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* https://github.com/traPtitech/traQ/blob/master/utils/message/replacer.goと同様
*/

const mentionRegex = /:?[@@]([^\s@@]{0,31}[^\s@@:])/g
const mentionRegex =
/([@@]([^\s@@]{0,31}[^\s@@:]))|(:[@@]([^\s@@]{0,31}[^\s@@:]:))/g
const userStartsRegex = /^[@@]([a-zA-Z0-9_-]{1,32})/g
const channelRegex = /[##]([a-zA-Z0-9_/-]+)/g

Expand Down Expand Up @@ -114,21 +115,28 @@ const replaceAll = (m: string, getters: Readonly<ReplaceGetters>) => {

const replaceMention = (m: string, getters: Readonly<UserAndGroupGetters>) => {
return m.replace(mentionRegex, s => {
// 始まりが:なものを除外
if (s.startsWith(':')) {
const isStartsWithColon = s.startsWith(':')

// 始まりと終わりが:なものを除外
if (isStartsWithColon && s.endsWith(':')) {
return s
}
const sColonRemoved = isStartsWithColon ? s.slice(1) : s.slice(0)

// .slice(1)は先頭の@を消すため
// .slice(1)は先頭の@および:@を消すため
// 小文字化はgetter内で行う
const name = s.slice(1)
const name = sColonRemoved.slice(1)
const uid = getters.getUser(name)?.id
if (uid) {
return `!{"type":"user","raw":"${s}","id":"${uid}"}`
return `${
isStartsWithColon ? ':' : ''
}!{"type":"user","raw":"${sColonRemoved}","id":"${uid}"}`
}
const gid = getters.getGroup(name)?.id
if (gid) {
return `!{"type":"group","raw":"${s}","id":"${gid}"}`
return `${
isStartsWithColon ? ':' : ''
}!{"type":"group","raw":"${sColonRemoved}","id":"${gid}"}`
}

return s.replace(userStartsRegex, s => {
Expand Down
Loading