HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
육개짱 프론트엔드
육개짱 프론트엔드
/
🍞
Toast UI Editor
🍞

Toast UI Editor

기본 사용법

import { Editor } from "@toast-ui/react-editor" import "@toast-ui/editor/dist/i18n/ko-kr" import "@toast-ui/editor/dist/toastui-editor.css" const Home = () => { return ( <> <Editor previewStyle="vertical" // horizontal로 하면 깃허브처럼 priview 따로 있음 height="600px" initialEditType="markdown" // 모드 설정(기본 마크다운) initialValue="hello" // placeholder language="ko-KR" // 한국어 제공 /> </> ) } export default Home
notion image
notion image
 

데이터 형식

notion image

마크다운

notion image

HTML

notion image

문제점

Viewer에서 sanitizing과 html형식으로 보여줄때 문제

보안이슈로 sanitizing을 적용하거나 value로 html을 넣으면 Viewer 에서 보여질때 주석은 그대로 보여지고 체크박스는 제대로 렌더링이 안된다.. → 일단 sanitizing을 안하는 방향이 나아보임
  • sanitizing
    • notion image
  • not sanitizing
    • notion image
       

이미지를 첨부했을때 문자열이 너무 길게 보여진다

notion image
마크다운 모드에서 이미지를 첨부하면 자동으로 이미지 파일을 base64로 변환한후 string으로 보여주는데 이게 상당히 보기 힘들다.. 그래서 벨로그 같은 서비스 같이 url로 이쁘게 바꿔서 에디터에 표시해야 하는데 이때 AWS S3가 필요하다. 다행히 toast ui editor v3에서 addImageBlobHook 이라는 훅을 지원해줘서 구현이 가능하다.
addImageBlobHook(blob, callback){ }
blob : 이미지의 Blob 형식 데이터
callback : 이미지 업로드 후 실행될 함수
서버에 보내는 이미지 데이터 형식은 formData다.
 
흐름은 다음과 같다.
notion image
tanstack-query를 사용하여 이미지 url이 올때까지 loading처리를 해두면 될 것 같고, 다른 사람이 올린 포스팅기준으로는 이미지 url이 전달되는데 시간이 오래걸리지 않는 것으로 보아 문제도 없어 보인다.
 
참고자료
https://velog.io/@io_/Toast-UI-Editor-이미지-업로드
https://teawon.github.io/next/toast-ui-editor-image-uplaod/
https://iamiet.tistory.com/entry/toast-ui-editor-v3-이미지-업로드-최적화커스터마이징하기

서버에서 받아온 마크다운 데이터를 어떻게 렌더링?

  • 마크다운 문법
  • markdown parser를 이용(markdown-it)
  • DOMPurify 모듈을 사용해 sanitizing(악성 스크립트 대처)
 
참고자료
https://velog.io/@suxxzzy/toast-ui-editor-api-사용-마크다운-서버-전송-마크다운-화면-렌더링

저장(임시)기능(선택)

  • HTML을 markdown으로 변경해서 렌더링하면 테이블과 설정한 내용이 깨지는 버그 발생하므로 내용을 저장할 땐 markdown으로 저장하기
  • 로컬스토리지 이용
https://bloodstrawberry.tistory.com/1271

서버에 보낼 데이터 형식

이유는 모르겠지만 일단 Markdown형식이 아닌 HTML형식의 문자열
참고자료
https://congsong.tistory.com/68