๊ณต์ ๋ฌธ์๋ค์ด๋
velog
๊ฐ์ ๊ฒฝ์ฐ์ ์ฐ์ธก ์๋จ์ ์ ๋ชฉ์ ๊ธฐ์ค์ผ๋ก ์ด๋ํ ์ ์๋ ๋ค๋น๊ฒ์ด์ ๋ฐ๋ฅผ ์ ๊ณตํด ์ค๋๋ค.
ํด๋น ๋ค๋น๊ฒ์ด์ ๋ฐ๋ฅผ ์ ์ํ ๋ฐฉ๋ฒ์ ๋ํ ๊ธฐ๋ก์ ๋๋ค.
<h1>
~ <h6>
๋ฅผ ์์ฑํ ๋ id
๋ก ์ ๋ชฉ์ ๋ฃ์ด์ค๋ค.#
, ##
, ... ######
์ดํ์ ๋์ค๋ ๋ฌธ์์ด(์ ๋ชฉ)์ ์ฐพ์๋ธ๋ค.<h1>
~ <h6>
์ id ๋ฃ๋ ์ฝ๋<ReactMarkdown
className="py-4 px-8 prose dark:prose-invert whitespace-normal max-w-full"
remarkPlugins={[remarkGfm]}
components={{
h1: ({ node, ...props }) => (
<h1 id={props.children[0]?.toString()} {...props} />
),
h2: ({ node, ...props }) => (
<h2 id={props.children[0]?.toString()} {...props} />
),
h3: ({ node, ...props }) => (
<h3 id={props.children[0]?.toString()} {...props} />
),
h4: ({ node, ...props }) => (
<h4 id={props.children[0]?.toString()} {...props} />
),
h5: ({ node, ...props }) => (
<h5 id={props.children[0]?.toString()} {...props} />
),
h6: ({ node, ...props }) => (
<h6 id={props.children[0]?.toString()} {...props} />
),
// ... ๋๋จธ์ง ์์ฑ๋ค์ ์๋ต
}}
>
{markdown}
</ReactMarkdown>
ํ๋ฐฉ ํ์์ ์ด์ฉํด์ #{1,6}
์ดํ์ ์ค๋ ํ
์คํธ๋ง ์ถ์ถํ์ต๋๋ค.
#
์ ๋ฐฉ์ ์ค๋ ํ์ดํธ ์คํ์ด์ค๋ ๋ฌด์ํ๊ณ , ํ
์คํธ๊ฐ ์ค๋ ๊ฒฝ์ฐ ์ ๋ชฉ์ผ๋ก ์ธ์ํ์ง ์๋๋ก ๋ง๋ค์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ #
๊ฐ์์ ๋ฐ๋ผ์ ๋ค๋ฅธ ์คํ์ผ์ ๋ถ์ฌํ๊ธฐ ์ํด์ ๊ฐ๊ฐ์ ๊ตฌ๋ถํ๊ธฐ ์ํด ๊ทธ๋ฃน ์ฐ์ฐ์(()
)๋ก 1 ~ 6๊น์ง ๋ถ๋ฆฌํด์ ์์ฑํ์ต๋๋ค.
// 2022/05/04 - ๋ฌธ์ฅ์์ ์ ๋ชฉ์ธ ๋จ์ด๋ง ๊ณจ๋ผ์ ๋จ์ด์ ํฌ๊ธฐ( h1 ~ h6 ) ๊ฐ์ฒด๋ก ๋ฐํ - by 1-blue
export const getTitleList = (sentence: string) => {
const titleListArray = [
...sentence.matchAll(
/((?<=^[\s]*#{1}\s).+)|((?<=^[\s]*#{2}\s).+)|((?<=^[\s]*#{3}\s).+)|((?<=^[\s]*#{4}\s).+)|((?<=^[\s]*#{5}\s).+)|((?<=^[\s]*#{6}\s).+)/gm
),
];
const titleList: { text: string; size: number }[] = [];
titleListArray.map((title) => {
// #, ##, ... ###### ์ค ์ด๋๊ฒ์ธ์ง ํ๋จํด์ ๊ทธ ํฌ๊ธฐ๋ฅผ 0(h1) ~ 5(h6)๋ก ๋ฐํํจ
const size = title.slice(1).findIndex((v) => v?.length > 0);
titleList.push({ text: title[0], size });
});
return titleList;
};
// ๋ฐํ ํํ
[
{
title: "์ฒซ ๋ฒ์งธ ์ ๋ชฉ",
size: 0
},
{
title: "๋ ๋ฒ์งธ ์ ๋ชฉ",
size: 1
},
// ...
]
์๋ ์ ๊ท ํํ์์ ๋ํ ๋ด์ฉ์ ๊ฐ๋จํ ๊ฒ๋ง ์๊ณ ์์๊ณ , ์ค์ ๋ก ์ฌ์ฉํ ๋๋ ๊ตฌ๊ธ์ ๊ฒ์ํด์ ์์ฑ๋ ์ ๊ท ํํ์์ ๊ทธ๋๋ก ๊ฐ์ ธ์์ ์ฌ์ฉํ์์ต๋๋ค. ( email
, password
๋ฑ )
์ด๋ฒ์ ์ฌ์ฉํ๋ฉด์ ์ด๋ค ํค์๋๋ก ๊ฒ์ํด์ ์ฐพ์์ผ ํ ์ง๋ถํฐ ํ๋ค์๊ณ , ์ค์ ๋ก ์ฌ์ฉํ๋ ค๊ณ ํ ์คํธํด ๋ณด๋ฉด์ ์ํ์ฐฉ์ค๋ฅผ ๋ง์ด ๊ฒช์๋๋ฐ ์ ๊ท ํํ์์ ์ ๋๋ก ๋ชจ๋ฅด๋ฉด์ ๊ตฌํ๋ถํฐ ํ๋ ค๋ค ๋ณด๋ ์ด๋ ค์ ๋ค๊ณ ์๊ฐํฉ๋๋ค.
์ด์ฐธ์ ์ ๊ท ํํ์์ ์ฒ์๋ถํฐ ๊ณต๋ถํ๋ฉด์ ์ ๋ฆฌํ๋ ค๊ณ ํฉ๋๋ค.