Skip to content

Commit

Permalink
メールアドレスの中も置換されないように
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Dec 29, 2024
1 parent 99455db commit 8d3ebc4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lib/markdown/internalLinkEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// URLの一部になっているときは置換しない (URLの正規表現は完全ではない)
const urlRegexStr = '(?:https?://)?(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]+(?:/[^/]+)*/?'
const mentionRegex = new RegExp(
`(?<!${urlRegexStr}):?[@@]([^\\s@@]{0,31}[^\\s@@:])`,
`(?<!${urlRegexStr}):?[@@]([^\\s@@.]{0,31}[^\\s@@:.])\\.?`,
'g'
)
const userStartsRegex = /^[@@]([a-zA-Z0-9_-]{1,32})/g
Expand Down Expand Up @@ -123,9 +123,9 @@ const replaceAll = (m: string, getters: Readonly<ReplaceGetters>) => {
const replaceMention = (m: string, getters: Readonly<UserAndGroupGetters>) => {
return m.replace(mentionRegex, s => {
// 始まりが:なものを除外
if (s.startsWith(':')) {
return s
}
if (s.startsWith(':')) return s
// 終わりが.のものを除外
if (s.endsWith('.')) return s

// .slice(1)は先頭の@を消すため
// 小文字化はgetter内で行う
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/lib/markdown/internalLinkEmbedder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const users = {
'dfdff0c9-5de0-46ee-9721-2525e8bb3d47': {
name: 'very_long_long_long_long_lo_name',
id: 'dfdff0c9-5de0-46ee-9721-2525e8bb3d47'
},
'dfdff0c9-5de0-46ee-9721-2525e8bb3d48': {
name: 'trap',
id: 'dfdff0c9-5de0-46ee-9721-2525e8bb3d48'
}
}
const groups = {
Expand Down Expand Up @@ -122,7 +126,9 @@ const specs: Spec[] = [
[
'example.com/users/@takashi_trap/hello',
'example.com/users/@takashi_trap/hello'
]
],
['[email protected]', '[email protected]'],
['@ab', '@ab']
]

describe('internalLinkEmbedder', () => {
Expand Down

0 comments on commit 8d3ebc4

Please sign in to comment.