๐ gray-matter: Markdown ํ์ผ์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ง๋ฒ ๋๊ตฌ
Markdown์ ํ์ฉํด ๋ธ๋ก๊ทธ๋ ์ ์ ์ฌ์ดํธ๋ฅผ ๋ง๋ ๋ค๋ฉด, ๋ฉํ๋ฐ์ดํฐ ๊ด๋ฆฌ๊ฐ ์ค์ํ ๋ถ๋ถ ์ค ํ๋์ธ๋ฐ์. ์ฌ๊ธฐ์ ๋ฑ์ฅํ๋ ๊ฒ ๋ฐ๋ก gray-matter์ ๋๋ค. gray-matter๋ Markdown ํ์ผ์ **ํ๋ก ํธ๋งคํฐ(frontmatter)**๋ฅผ ์ฝ๊ณ , ์ด๋ฅผ ํ์ฑ(parse)ํ์ฌ JSON ํํ๋ก ์ฌ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๋ฉ์ง ๋๊ตฌ์์. ์ด๋ฒ ๊ธ์์๋ gray-matter๊ฐ ๋ฌด์์ธ์ง, ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง, ๊ทธ๋ฆฌ๊ณ ์ค๋ฌด์์ ์ด๋ป๊ฒ ํ์ฉํ ์ ์๋์ง ์ดํด๋ณด๊ฒ ์ต๋๋ค!
1. gray-matter๋?
โจ “Markdown ํ์ผ์์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ”
gray-matter๋ **Markdown ํ์ผ ์๋จ์ ์์ฑ๋ ๋ฉํ๋ฐ์ดํฐ(frontmatter)**๋ฅผ JavaScript ๊ฐ์ฒด๋ก ๋ณํํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ํ๋ก ํธ๋งคํฐ๋ YAML, JSON ๋๋ ๊ธฐํ ํ์์ผ๋ก ์์ฑ๋๋ฉฐ, ์ ๋ชฉ, ๋ ์ง, ํ๊ทธ์ ๊ฐ์ ์ ๋ณด๋ฅผ ํฌํจํฉ๋๋ค.
๐ ์ ํ์ํ ๊น?
1. ๋ธ๋ก๊ทธ๋ CMS์ ๋ฉํ๋ฐ์ดํฐ ๊ด๋ฆฌ:
• ๊ธ ์ ๋ชฉ, ์์ฑ ๋ ์ง, ํ๊ทธ ๋ฑ์ Markdown ํ์ผ์ ์ถ๊ฐํด ๊ด๋ฆฌ.
2. ์ ์ ์ฌ์ดํธ ์์ฑ๊ธฐ์์ ์ฝํ ์ธ ๋ฐ์ดํฐ ํ์ฑ:
• Gatsby, Next.js, Hugo์ ๊ฐ์ ํด๊ณผ ๊ฒฐํฉ.
3. ์ ์ฐํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ ๊ณต:
• YAML, JSON, TOML ๋ฑ ๋ค์ํ ํ์ ์ง์.
2. ํ๋ก ํธ๋งคํฐ(Frontmatter)๋?
ํ๋ก ํธ๋งคํฐ๋ Markdown ํ์ผ์ ์๋จ์ ์ถ๊ฐ๋๋ ๋ฉํ๋ฐ์ดํฐ ๋ธ๋ก์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก YAML ํ์์ผ๋ก ์์ฑ๋ฉ๋๋ค.
๐๏ธ YAML ๊ธฐ๋ฐ ํ๋ก ํธ๋งคํฐ ์์
---
title: "gray-matter๋ ๋ฌด์์ธ๊ฐ?"
date: "2025-01-17"
tags:
- JavaScript
- Markdown
summary: "gray-matter๋ฅผ ํ์ฉํด Markdown์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋ด ์๋ค."
---
# gray-matter๋ฅผ ํ์ฉํด ๋ฉํ๋ฐ์ดํฐ ์ถ์ถํ๊ธฐ
์ด ๊ธ์ gray-matter๋ฅผ ์๊ฐํฉ๋๋ค.
๐๏ธ JSON ๊ธฐ๋ฐ ํ๋ก ํธ๋งคํฐ ์์
{
"title": "gray-matter๋ ๋ฌด์์ธ๊ฐ?",
"date": "2025-01-17",
"tags": ["JavaScript", "Markdown"],
"summary": "gray-matter๋ฅผ ํ์ฉํด Markdown์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋ด ์๋ค."
}
---
# gray-matter๋ฅผ ํ์ฉํด ๋ฉํ๋ฐ์ดํฐ ์ถ์ถํ๊ธฐ
์ด ๊ธ์ gray-matter๋ฅผ ์๊ฐํฉ๋๋ค.
3. gray-matter ์ค์น ๋ฐ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
๐ ๏ธ ์ค์น
npm install gray-matter
๐ ์ฌ์ฉ๋ฒ
Markdown ํ์ผ์์ ๋ฉํ๋ฐ์ดํฐ์ ๋ณธ๋ฌธ์ ๋ถ๋ฆฌํ๋ ๊ธฐ๋ณธ ์์ ์ ๋๋ค.
Markdown ํ์ผ (example.md)
---
title: "gray-matter ์์ํ๊ธฐ"
author: "์ต์น์"
date: "2025-01-17"
tags:
- JavaScript
- Markdown
---
Markdown์์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ๊ณ ์ถ์ผ์ ๊ฐ์? gray-matter๊ฐ ๋์๋๋ฆด๊ฒ์!
JavaScript ์ฝ๋
const fs = require('fs');
const matter = require('gray-matter');
// Markdown ํ์ผ ์ฝ๊ธฐ
const fileContent = fs.readFileSync('./example.md', 'utf8');
// gray-matter๋ก ํ์ฑ
const parsed = matter(fileContent);
console.log(parsed.data); // ๋ฉํ๋ฐ์ดํฐ
console.log(parsed.content); // Markdown ๋ณธ๋ฌธ
์ถ๋ ฅ ๊ฒฐ๊ณผ
// parsed.data
{
title: "gray-matter ์์ํ๊ธฐ",
author: "์ต์น์",
date: "2025-01-17",
tags: ["JavaScript", "Markdown"]
}
// parsed.content
"Markdown์์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํ๊ณ ์ถ์ผ์ ๊ฐ์? gray-matter๊ฐ ๋์๋๋ฆด๊ฒ์!"
4. gray-matter์ ์ฃผ์ ๊ธฐ๋ฅ
๐ 1) ๋ฉํ๋ฐ์ดํฐ๋ง ์ถ์ถ
ํน์ ์ํฉ์์๋ Markdown์ ๋ณธ๋ฌธ์ ๋ฌด์ํ๊ณ ๋ฉํ๋ฐ์ดํฐ๋ง ํ์ํ ๋๊ฐ ์์ต๋๋ค.
const { data } = matter(fileContent);
console.log(data); // ๋ฉํ๋ฐ์ดํฐ๋ง ์ถ๋ ฅ
๐ง 2) ๋ค์ํ ํ๋ก ํธ๋งคํฐ ํ์ ์ง์
gray-matter๋ YAML๋ฟ ์๋๋ผ JSON, TOML ํ์๋ ์ง์ํฉ๋๋ค.
const { data } = matter(fileContent, {
engines: {
toml: require('toml'),
},
});
๐๏ธ 3) ํ๋ก ํธ๋งคํฐ๋ฅผ ๋์ ์ผ๋ก ์์ฑ
Markdown ํ์ผ์ ์์ฑํ๊ฑฐ๋ ์ ๋ฐ์ดํธํ ๋ ์ ์ฉํฉ๋๋ค.
const updatedContent = matter.stringify('๋ณธ๋ฌธ ๋ด์ฉ', {
title: '์๋ก์ด ๊ธ',
author: '์ต์น์',
date: '2025-01-17',
});
console.log(updatedContent);
์ถ๋ ฅ ๊ฒฐ๊ณผ:
---
title: "์๋ก์ด ๊ธ"
author: "์ต์น์"
date: "2025-01-17"
---
๋ณธ๋ฌธ ๋ด์ฉ
5. gray-matter ์ค์ ํ์ฉ ์์
๐ฏ Next.js์์ ๋ธ๋ก๊ทธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
gray-matter๋ Next.js์ ๊ฐ์ ์ ์ ์ฌ์ดํธ ์์ฑ๊ธฐ์์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
// Markdown ํ์ผ ์ฝ๊ธฐ
export const getPosts = () => {
const files = fs.readdirSync(path.join('posts'));
return files.map((filename) => {
const fileContent = fs.readFileSync(path.join('posts', filename), 'utf8');
const { data, content } = matter(fileContent);
return {
metadata: data,
content,
};
});
};
6. gray-matter์ ํจ๊ป ์ฐ์ด๋ ๋๊ตฌ
1. Markdown-it: Markdown ํ์ฑ.
2. remark/rehype: Markdown๊ณผ HTML ์ฒ๋ฆฌ.
3. Next.js, Gatsby: ์ ์ ์ฌ์ดํธ ์์ฑ๊ธฐ.
4. Contentlayer: CMS ๋ฐ์ดํฐ๋ฅผ ํ์ ์ธ์ดํํ๊ฒ ๊ด๋ฆฌ.
7. gray-matter์ ์ฅ๋จ์
๐ ์ฅ์
• ๋ค์ํ ํ๋ก ํธ๋งคํฐ ํ์ ์ง์.
• ๊ฐ๋จํ API๋ก ๋ฉํ๋ฐ์ดํฐ ์ถ์ถ.
• Markdown ๊ธฐ๋ฐ ํ๋ก์ ํธ์ ๋์ ํธํ์ฑ.
๐ ๋จ์
• ๋ฉํ๋ฐ์ดํฐ์ ๋ณธ๋ฌธ์ด ๋ช ํํ ๊ตฌ๋ถ๋์ง ์์ผ๋ฉด ์๋ฌ ๋ฐ์ ๊ฐ๋ฅ.
• ๋งค์ฐ ํฐ ํ์ผ์์ ์ฑ๋ฅ ์ด์ ๋ฐ์ ๊ฐ๋ฅ.
๊ฒฐ๋ก
gray-matter๋ Markdown์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๊ฐ๋ ฅํ ๋๊ตฌ๋ก, ๋ธ๋ก๊ทธ, ๋ฌธ์ํ, ์ ์ ์ฌ์ดํธ ๋ฑ ๋ค์ํ ํ๋ก์ ํธ์์ ์ ์ฉํ๊ฒ ํ์ฉํ ์ ์์ต๋๋ค.
Markdown ํ์ผ์ ํ๋ก ํธ๋งคํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ ์ถ๋ค๋ฉด, gray-matter๋ฅผ ํ์ฉํด๋ณด์ธ์.
๐ท์ ์ค์ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ด ์๋น!๐ท
'Front-end > Good Front' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐จ๏ธ Giscus: GitHub ๊ธฐ๋ฐ์ ๋๊ธ ์์คํ (0) | 2025.03.06 |
---|---|
Compound Components๋? ๐ก (0) | 2025.02.23 |
๐ก ํ๋ก ํธ์๋์์ IoC๋ ๋ฌด์์ธ๊ฐ์? (0) | 2025.02.21 |