Skip to content

Commit

Permalink
Merge pull request #4507 from traPtitech/fix/cant_use_lookbehind_on_o…
Browse files Browse the repository at this point in the history
…ld_ios_safari

古いiOSで正規表現の後読みが使えない問題を修正
  • Loading branch information
nokhnaton authored Jan 28, 2025
2 parents 395e21b + 976f466 commit 46bb9c6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/lib/markdown/internalLinkEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

// URLの一部になっているときは置換しない (URLの正規表現は完全ではない)
const urlRegexStr = '(?:https?://)?(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]+(?:/[^/]+)*/?'
const urlStartRegex = new RegExp(`^${urlRegexStr}`)
const mentionRegex = new RegExp(
`(?<!${urlRegexStr}):?[@@]([^\\s@@.]{0,31}[^\\s@@:.])\\.?`,
`(${urlRegexStr})?:?[@@]([^\\s@@.]{0,31}[^\\s@@:.])\\.?`,
'g'
)
const userStartsRegex = /^[@]([a-zA-Z0-9_-]{1,32})/g
const channelRegex = new RegExp(
`(?<!${urlRegexStr})[##]([a-zA-Z0-9_/-]+)`,
'g'
)
const channelRegex = new RegExp(`(${urlRegexStr})?[##]([a-zA-Z0-9_/-]+)`, 'g')

const backQuote = '`'
const dollar = '$'
Expand Down Expand Up @@ -122,6 +120,8 @@ const replaceAll = (m: string, getters: Readonly<ReplaceGetters>) => {

const replaceMention = (m: string, getters: Readonly<UserAndGroupGetters>) => {
return m.replace(mentionRegex, s => {
const urlStart = s.match(urlStartRegex)
if (urlStart && urlStart.length !== 0) return s
// 始まりが:なものを除外
if (s.startsWith(':')) return s
// 終わりが.のものを除外
Expand Down Expand Up @@ -155,6 +155,8 @@ const replaceMention = (m: string, getters: Readonly<UserAndGroupGetters>) => {

const replaceChannel = (m: string, getter: Readonly<ChannelGetter>) => {
return m.replace(channelRegex, s => {
const urlStart = s.match(urlStartRegex)
if (urlStart && urlStart.length !== 0) return s
// .slice(1)は先頭の#を消すため
// 小文字化はgetter内で行う
const t = s.slice(1)
Expand Down

0 comments on commit 46bb9c6

Please sign in to comment.