diff --git a/src/components/comment/CommentTextBlock/index.tsx b/src/components/comment/CommentTextBlock/index.tsx index 2dc3f0fd..4e8bacc5 100644 --- a/src/components/comment/CommentTextBlock/index.tsx +++ b/src/components/comment/CommentTextBlock/index.tsx @@ -1,13 +1,15 @@ import React, { FC } from 'react'; import { ICommentBlockProps } from '~/constants/comment'; import styles from './styles.module.scss'; +import classNames from 'classnames'; +import markdown from '~/styles/common/markdown.module.scss'; interface IProps extends ICommentBlockProps {} const CommentTextBlock: FC = ({ block }) => { return (
{ if (document.defaultView && document.defaultView.getComputedStyle) { @@ -112,12 +113,13 @@ export const findBlockType = (line: string): ValueOf }; export const splitCommentByBlocks = (text: string): ICommentBlock[] => - text - .split(/(https?:\/\/(?:www\.)(?:youtube\.com|youtu\.be)\/(?:watch)(?:\?v=)[\w\-\&\=]+)/) - .map(line => ({ - type: findBlockType(line), - content: line, - })); + pipe( + splitTextByYoutube, + splitTextOmitEmpty + )([text]).map(line => ({ + type: findBlockType(line), + content: line, + })); export const formatCommentText = (author: string, text: string): ICommentBlock[] => text ? splitCommentByBlocks(formatText(text)) : null; diff --git a/src/utils/splitText.ts b/src/utils/splitText.ts new file mode 100644 index 00000000..5cd7ac8a --- /dev/null +++ b/src/utils/splitText.ts @@ -0,0 +1,11 @@ +import { flatten } from 'ramda'; + +export const splitTextByYoutube = (strings: string[]): string[] => + flatten( + strings.map(str => + str.split(/(https?:\/\/(?:www\.)(?:youtube\.com|youtu\.be)\/(?:watch)(?:\?v=)[\w\-\&\=]+)/) + ) + ); + +export const splitTextOmitEmpty = (strings: string[]): string[] => + strings.filter(el => !!el.trim());