Skip to content
Merged
Changes from 3 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