Skip to content

Commit

Permalink
メンションの前に:がつくとメンションできない問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
nokhnaton committed Oct 28, 2024
1 parent cffdf9d commit d432e7e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/lib/markdown/internalLinkEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* https://github.com/traPtitech/traQ/blob/master/utils/message/replacer.goと同様
*/

const mentionRegex = /:?[@@]([^\s@@]{0,31}[^\s@@:])/g
const mentionRegex = /:?[@@]([^\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 +114,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

0 comments on commit d432e7e

Please sign in to comment.