diff --git a/src/lib/markdown/internalLinkEmbedder.ts b/src/lib/markdown/internalLinkEmbedder.ts index da2724194..210f75a75 100644 --- a/src/lib/markdown/internalLinkEmbedder.ts +++ b/src/lib/markdown/internalLinkEmbedder.ts @@ -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 @@ -114,21 +115,28 @@ const replaceAll = (m: string, getters: Readonly) => { const replaceMention = (m: string, getters: Readonly) => { 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 => {