forked from codesquad-members-2021/issue-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [#131]Feat: headerParser&emphasisParser * chore: markdown preview 제거
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import styled from 'styled-components'; | ||
|
||
const Markdown = ({ string }: { string: string }) => { | ||
const checkHeader = (str: string) => { | ||
const check = str.match(/^[\#]+ |^[\>] /gi); | ||
if (check) | ||
return { key: check[0], str: str.replace(/^[\#]+ |^[\>] /gi, '') }; | ||
else return { key: null, str: str }; | ||
}; | ||
|
||
const headerParser = (key: string | null, str: string | JSX.Element) => { | ||
switch (key) { | ||
case '# ': | ||
return <h1>{str}</h1>; | ||
case '## ': | ||
return <h2>{str}</h2>; | ||
case '### ': | ||
return <h3>{str}</h3>; | ||
case '#### ': | ||
return <h4>{str}</h4>; | ||
case '##### ': | ||
return <h5>{str}</h5>; | ||
case '###### ': | ||
return <h6>{str}</h6>; | ||
case '> ': | ||
return <QuotationStyle>{str}</QuotationStyle>; | ||
case null: | ||
return <span>{str}</span>; | ||
} | ||
}; | ||
|
||
const emphasisParser = (str: string) => { | ||
const arr = str.match(/(\*{2,5})(.*?)\1/g); | ||
let res = []; | ||
let temp; | ||
if (!arr) return str; | ||
else { | ||
temp = arr.map((str) => <b>{str.slice(2, -2)}</b>); | ||
} | ||
let sp = str.replace(/(\*{2,5})(.*?)\1/g, '\n').split('\n'); | ||
let j =0; | ||
for(let i =0; i<sp.length; i++) { | ||
if(!sp[i].length){ | ||
res.push(temp[j]) | ||
j++ | ||
} else res.push(sp[i]) | ||
} | ||
return <>{res.map((v) => v)}</>; | ||
}; | ||
|
||
return ( | ||
<> | ||
{string | ||
.split('\n') | ||
.map((v) => headerParser(checkHeader(v).key, emphasisParser(checkHeader(v).str)))} | ||
</> | ||
); | ||
}; | ||
|
||
export default Markdown; | ||
|
||
const QuotationStyle = styled.div` | ||
border-left: 4px solid #39a6a3; | ||
background-color: #deedf0; | ||
width: 100%; | ||
padding: 1rem; | ||
`; |
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