-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-html.js
118 lines (96 loc) · 3.56 KB
/
generate-html.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const fs = require('fs');
const path = require('path');
const MarkdownIt = require('markdown-it');
const md = new MarkdownIt();
// Get the current directory where the script is run
const inputDir = path.join(process.cwd(), 'fcc-js');
// Get the output directory from the command-line argument or use the default value
const outputDir = process.argv[2] || 'dist';
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// Generate the complete HTML content with the template
function generateCompleteHtmlContent(htmlContent, directory) {
const cssFilePath = path.relative(directory, path.join(process.cwd(), 'css', 'styles.css'));
const backButton = directory.endsWith('fcc-js') ? '' : `<li><a href="../.">Go Back</a></li>`;
// Extract the title from the HTML content
const titleMatch = htmlContent.match(/<h1[^>]*>([^<]+)<\/h1>/i);
let pageTitle = 'Learning Javascript'; // Fallback default title
if (titleMatch) {
// Use the value of the first <h1> heading as the title
pageTitle = titleMatch[1];
} else {
// If no <h1> heading is available, use the first line as the title
const firstLineMatch = htmlContent.match(/^.*$/m);
if (firstLineMatch) {
pageTitle = firstLineMatch[0];
}
}
const template = `\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FCC JS | ${pageTitle}</title>
<link rel="stylesheet" href="${cssFilePath}">
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
${backButton}
</ul>
</nav>
<main>
${htmlContent}
</main>
</body>
</html>
`;
return template;
}
function processDirectory(directory) {
const files = fs.readdirSync(directory, { withFileTypes: true });
// Check if the directory or its subdirectories have Markdown files
const hasMarkdownFiles = files.some((file) => {
const filePath = path.join(directory, file.name);
if (file.isDirectory()) {
return processDirectory(filePath);
}
if (file.isFile() && path.extname(file.name).toLowerCase() === '.md') {
return true;
}
return false;
});
if (hasMarkdownFiles) {
files.forEach((file) => {
const filePath = path.join(directory, file.name);
if (file.isDirectory()) {
processDirectory(filePath);
} else if (file.isFile() && path.extname(file.name).toLowerCase() === '.md') {
// Convert the markdown content to HTML
const markdownContent = fs.readFileSync(filePath, 'utf8');
const htmlContent = md.render(markdownContent);
// Generate the complete HTML content with the template
const completeHtmlContent = generateCompleteHtmlContent(htmlContent, directory, files);
// Write the HTML content to a corresponding file in the output directory
const outputFileDir = path.join(outputDir, path.relative(inputDir, directory));
const outputFilePath = path.join(outputFileDir, `${path.parse(file.name).name}.html`);
// Create the output subdirectory if it doesn't exist
if (!fs.existsSync(outputFileDir)) {
fs.mkdirSync(outputFileDir, { recursive: true });
}
fs.writeFileSync(outputFilePath, completeHtmlContent);
// Rename README.md files to index.html
if (file.name.toLowerCase() === 'readme.md') {
fs.renameSync(outputFilePath, path.join(outputFileDir, 'index.html'));
}
}
});
}
return hasMarkdownFiles;
}
// Start processing from the current directory
processDirectory(inputDir);