-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FE] 팀 채팅 내 링크를 하이퍼링크로 씌우는 기능 구현 (#923)
* refactor: URL 판정 정규 표현식 재사용을 위해 상수 파일로 분리 * feat: ParsedThreadContent 관련 타입 추가 - 스레드에서 평범하게 입력된 메시지가 string 타입이라면, ParsedThreadContent는 이 메시지를 쪼개고 분류하여 파싱한 배열 형태의 데이터 타입이다. 현재는 일반 텍스트와 링크 두 가지만 구분한다. * feat: string 형태의 메시지를 받아 분류별로 파싱한 메시지를 반환하는 parseThreadContent 함수 구현 * test: parseThreadContent 함수에 대한 테스트 코드 작성 * feat: Thread 컴포넌트의 메시지에 있는 링크에 대해 자동으로 하이퍼링크를 생성하는 기능 구현 * test: Thread 컴포넌트에서 링크가 포함된 스토리를 추가 * feat: NoticeThread 컴포넌트의 메시지에 있는 링크에 대해 자동으로 하이퍼링크를 생성하는 기능 구현 * test: NoticeThread 컴포넌트에서 링크가 포함된 스토리를 추가 * refactor: for문을 지양하고, forEach문과 else문으로 변경 * refactor: 매직 넘버로 간주될 수 있는 정규표현식을 상수로써 분리 - 현재 쓰이는 곳은 이 함수 하나뿐이므로 해당 파일에 선언, 쓰이는 곳이 두 곳 이상이 될 경우 상수 파일로 분리할 예정
- Loading branch information
Showing
11 changed files
with
268 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export const linkTableHeaderValues = ['링크명', '이름', '날짜', '']; | ||
|
||
export const URL_REGEX = | ||
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { URL_REGEX } from '~/constants/link'; | ||
import { generateHttpsUrl } from '~/utils/generateHttpsUrl'; | ||
import type { ParsedThreadContent } from '~/types/feed'; | ||
|
||
const LINK_SEPARATOR_REGEX = /\S+(?=https?:\/\/)|https?:\/\/\S+|\s+|\S+/g; | ||
|
||
export const parseThreadContent = (rawContent: string) => { | ||
const splittedThreadContent = rawContent.match(LINK_SEPARATOR_REGEX) ?? []; | ||
const generatedThreadContent: ParsedThreadContent = []; | ||
let textContent = ''; | ||
|
||
splittedThreadContent.forEach((currentContent) => { | ||
const isLink = URL_REGEX.test(currentContent); | ||
|
||
if (isLink && textContent !== '') { | ||
generatedThreadContent.push({ | ||
type: 'text', | ||
text: textContent, | ||
}); | ||
|
||
textContent = ''; | ||
} | ||
|
||
if (isLink) { | ||
generatedThreadContent.push({ | ||
type: 'link', | ||
text: currentContent, | ||
link: generateHttpsUrl(currentContent), | ||
}); | ||
} else { | ||
textContent += currentContent; | ||
} | ||
}); | ||
|
||
if (textContent !== '') { | ||
generatedThreadContent.push({ | ||
type: 'text', | ||
text: textContent, | ||
}); | ||
} | ||
|
||
return generatedThreadContent; | ||
}; |
Oops, something went wrong.