diff --git a/front/src/api.ts b/front/src/api.ts index c1557b9..58f6eaa 100644 --- a/front/src/api.ts +++ b/front/src/api.ts @@ -250,6 +250,17 @@ export interface PostCommentResponse { export const postComment = async (commentData: PostCommentRequest): Promise => { try { const identity = await get_id_token(); + const hashtopic: string[] = []; + const regex = /#\S+/g; + const matches = commentData.content.match(regex); + if (matches) { + matches.forEach(tag => { + const cleanTag = tag.substring(1); + if (!hashtopic.includes(cleanTag)) { + hashtopic.push(cleanTag); + } + }); + } const response = await fetch('/api/comment', { method: 'POST', headers: { @@ -257,6 +268,7 @@ export const postComment = async (commentData: PostCommentRequest): Promise { + return (tree: any) => { + const transformNode = (node: any, inLink = false) => { + if (node.type === 'link') inLink = true; + + if (node.children) { + node.children = node.children.flatMap((child: any) => { + if (child.type === 'text' && !inLink) { + const parts = child.value.split(/(#\S+)/g); + return parts.map((part: string) => { + if (part.match(/^#\S+$/)) { + return { + type: 'link', + url: 'tag:' + part, + children: [{ type: 'text', value: part }] + }; + } + if (part === "") return []; + return { type: 'text', value: part }; + }).flat(); + } + return transformNode(child, inLink); + }); + } + return node; + }; + transformNode(tree); + }; +}; + interface CommentSectionProps { postId: number; } @@ -188,7 +225,21 @@ const CommentSection: React.FC = ({ postId }) => {
{comment.nickname}
- {comment.content} +
+ { + if (props.href && props.href.startsWith('tag:')) { + return {props.children}; + } + return ; + }, + }} + > + {comment.content} + +