-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathextract-markdown.js
More file actions
57 lines (50 loc) · 1.98 KB
/
extract-markdown.js
File metadata and controls
57 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const fs = require('fs');
const cheerio = require('cheerio');
// HTML 파일 읽기
const html = fs.readFileSync('codedeck-flutter-animation.html', 'utf-8');
const $ = cheerio.load(html);
// 메타데이터 추출
const title = $('h1[id*="flutter"]').first().text().trim() || 'Flutter 애니메이션 구현 완벽 가이드';
const description = $('meta[name="description"]').attr('content');
const author = $('meta[name="author"]').attr('content');
const datePublished = $('script[type="application/ld+json"]').first().text();
let markdown = `# ${title}\n\n`;
markdown += `> ${description}\n\n`;
markdown += `**작성자:** ${author}\n`;
markdown += `**출처:** CodeDeck - https://www.codedeck.kr/card-news/13775a2d-799d-43a1-b3f7-1738079bfaf6\n\n`;
markdown += `---\n\n`;
// 본문 콘텐츠 추출
const content = $('.markdown-content');
content.find('h1, h2, h3, h4, p, pre, ul, ol, hr').each((i, elem) => {
const $elem = $(elem);
const tagName = elem.tagName.toLowerCase();
if (tagName === 'h1') {
markdown += `# ${$elem.text().trim()}\n\n`;
} else if (tagName === 'h2') {
markdown += `## ${$elem.text().trim()}\n\n`;
} else if (tagName === 'h3') {
markdown += `### ${$elem.text().trim()}\n\n`;
} else if (tagName === 'h4') {
markdown += `#### ${$elem.text().trim()}\n\n`;
} else if (tagName === 'p') {
markdown += `${$elem.text().trim()}\n\n`;
} else if (tagName === 'pre') {
const code = $elem.find('code').text();
const lang = $elem.find('code').attr('class')?.match(/language-(\w+)/)?.[1] || '';
markdown += `\`\`\`${lang}\n${code}\n\`\`\`\n\n`;
} else if (tagName === 'ul') {
$elem.find('li').each((j, li) => {
markdown += `- ${$(li).text().trim()}\n`;
});
markdown += `\n`;
} else if (tagName === 'ol') {
$elem.find('li').each((j, li) => {
markdown += `${j + 1}. ${$(li).text().trim()}\n`;
});
markdown += `\n`;
} else if (tagName === 'hr') {
markdown += `---\n\n`;
}
});
// 파일 저장
console.log(markdown);