Skip to content

light9639/React-Markdown-TS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“‹ React-markdown์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“  ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.

:octocat: ๋ฐ”๋กœ๊ฐ€๊ธฐ : https://light9639.github.io/React-Markdown-TS/

light9639 github io_React-Markdown-TS_

โœจ React-markdown์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“  ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. โœจ

๐ŸŽ‰ React ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  • React ์ƒ์„ฑ
npm create-react-app my-app
# or
yarn create react-app my-app
  • vite๋ฅผ ์ด์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด
npm create vite@latest
# or
yarn create vite
  • ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰ ํ›„ ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ๋งŒ๋“  ํ›„ React ์„ ํƒ, Typescirpt-SWC ์„ ํƒํ•˜๋ฉด ์ƒ์„ฑ ์™„๋ฃŒ.

๐Ÿ›บ 'react-markdown', 'react-syntax-highlighter', 'remark-gfm' ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

  • ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ ค๋ฉด react-markdown ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์„ ์น˜ํ•ด์„œ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
$ npm install react-markdown react-syntax-highlighter remark-gfm
# or
$ yarn add react-markdown react-syntax-highlighter remark-gfm

โœ’๏ธ App.tsx, App.css ์ˆ˜์ • ๋ฐ ์ž‘์„ฑ

โšก App.tsx

  • ReactMarkdown์œผ๋กœ ๋งˆํฌ๋‹ค์šด์„ ๊ฐ์‹ธ์ฃผ๋ฉด ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•์ด react์— ์ ์šฉ๋œ๋‹ค.
  • react-syntax-highlighter๋ฅผ ์ด์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฌธ์„œ์—์„œ๋Š” prism ์†์„ฑ์„ ์ด์šฉํ–ˆ๋‹ค.
  • ReactMarkdown๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆํฌ๋‹ค์šด ๋ฌธ๋ฒ•์ด ์ œํ•œ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— link, table, checklist ๋“ฑ์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ remark-gfm ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ๊ฐ™์ด ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.
import React, { useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
import ReactMarkdown from "react-markdown";
import SyntaxHighlighter from "react-syntax-highlighter";
import prism from 'react-syntax-highlighter/dist/esm/styles/prism/prism';
import remarkGfm from 'remark-gfm';

// ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์Šคํƒ€์ผ๋“ค
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';

const startingText = `# Hello

https://example.org

\`\`\`sh
# Code block
\`\`\`

![Placeholder image](https://unsplash.it/600/400)

~~strike~~ this

## TODO

* [ ]  This
* [ ]  That
* [x]  The other

|Fahrenheit|Celsius|Kelvin|
|---:|---:|---:|
|-459.67|-273.15|0|
|-40|-40|233.15|
|32|0|273.15|
|212|100|373.15|
`;

export default function App(): JSX.Element {
  const [input, setInput] = useState<string>(startingText);

  return (
    <div className="App">
      <textarea
        autoFocus
        className='textarea'
        value={input}
        onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => { setInput(e.target.value) }}
      ></textarea>
      <ReactMarkdown
        remarkPlugins={[remarkGfm]}
        children={input}
        className='markdown'
        components={{
          code({ node, inline, className, children, ...props }) {
            const match = /language-(\w+)/.exec(className || '')
            return !inline && match ? (
              <SyntaxHighlighter
                children={String(children).replace(/\n$/, '')}
                style={prism}
                language={match[1]}
                PreTag="div"
                {...props}
              />
            ) : (
              <code className={className} {...props}>
                {children}
              </code>
            )
          },
        }}
      />
    </div>
  )
}

โšก App.css

  • ๋ฐ˜์‘ํ˜• CSS๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋„“์ด๊ฐ€ 1024px ์ด์ƒ์ผ ๋•Œ๋Š” ๊ฐ๊ฐ ์ ˆ๋ฐ˜์”ฉ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜์ง€๋งŒ 1024px ์ดํ•˜์ผ ๊ฒฝ์šฐ์—๋Š” ์„ธ๋กœ ์ •๋ ฌ์ด ๋˜๋„๋ก ํ•œ๋‹ค.
.App {
  display: flex;
}

.textarea {
  width: 50%;
  height: 100vh;
  padding: 20px;
  font-size: 2rem;
  outline: none;
}

.markdown {
  width: 50%;
  height: 100vh;
  padding: 20px;
  outline: none;
}

@media screen and (max-width: 1024px) {
  .App {
    flex-direction: column;
  }

  .textarea {
    max-width: 95%;
    width: 100%;
    margin: 0 auto;
    height: 50vh;
  }

  .markdown {
    max-width: 95%;
    width: 100%;
    margin: 0 auto;
    height: 50vh;
  }
}

๐Ÿ“Ž ์ถœ์ฒ˜

About

๐Ÿ“‹ React-Markdown์„ ์ด์šฉํ•œ ์˜ˆ์ œ ํŒŒ์ผ.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published