Skip to content

Commit d8fb447

Browse files
committed
feat: added TODO
1 parent 9111c47 commit d8fb447

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

scripts/copyMarkdown.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
11
const fs = require('fs');
22
const path = require('path');
33

4+
5+
/**
6+
* Parses the frontmatter of a Markdown file and extracts metadata.
7+
*
8+
* Currently, this function only extracts the `slug` property if present.
9+
*
10+
* @param {string} content - The content of the Markdown file.
11+
* @returns {Object} An object containing the extracted frontmatter properties.
12+
* Example: { slug: '/my-custom-slug' }
13+
*/
14+
function parseFrontmatter(content) {
15+
const frontmatterRegex = /^---\n([\s\S]*?)\n---\n/;
16+
const match = content.match(frontmatterRegex);
17+
if (!match) return {};
18+
19+
const frontmatter = match[1];
20+
const slugMatch = frontmatter.match(/^slug:\s*(.+)$/m);
21+
return {
22+
slug: slugMatch ? slugMatch[1].trim() : null,
23+
};
24+
}
25+
26+
// TODO: Some files are in wrong directories, e.g. `/academy/actor-marketing-playbook.html` and `/academy/get_most_of_actors/actor-marketing-playbook.md`
27+
28+
/**
29+
* Recursively copies Markdown (.md) files from the source directory to the destination directory,
30+
* transforming filenames as needed:
31+
* - If a custom `slug` is present in the file's frontmatter, the output filename is based on the slug.
32+
* - Otherwise, underscores in the filename are replaced with dashes.
33+
*
34+
* This makes Markdown files accessible from the website in the same location as their corresponding HTML files.
35+
*
36+
* @param {string} srcDir - The source directory to copy from.
37+
* @param {string} destDir - The destination directory to copy to.
38+
*/
439
function copyRecursive(srcDir, destDir) {
540
if (!fs.existsSync(destDir)) fs.mkdirSync(destDir, { recursive: true });
641
fs.readdirSync(srcDir).forEach((file) => {
@@ -9,7 +44,26 @@ function copyRecursive(srcDir, destDir) {
944
if (fs.statSync(srcPath).isDirectory()) {
1045
copyRecursive(srcPath, destPath);
1146
} else if (file.endsWith('.md')) {
12-
fs.copyFileSync(srcPath, destPath);
47+
// Read the file content to check for custom slug
48+
const content = fs.readFileSync(srcPath, 'utf8');
49+
const frontmatter = parseFrontmatter(content);
50+
51+
let targetFilename = file;
52+
53+
if (frontmatter.slug) {
54+
// Use the custom slug, but add .md extension
55+
const slugPath = frontmatter.slug.replace(/^\//, ''); // Remove leading slash
56+
targetFilename = slugPath.split('/').pop() + '.md';
57+
} else {
58+
// Transform filename from underscores to dashes
59+
targetFilename = file.replace(/_/g, '-');
60+
}
61+
62+
const transformedDestPath = path.join(destDir, targetFilename);
63+
64+
// Copy to the same location as the HTML file would be, but with transformed filename
65+
fs.copyFileSync(srcPath, transformedDestPath);
66+
console.log('🤖 Successfully copied Markdown file', srcPath, 'to', transformedDestPath);
1367
}
1468
});
1569
}
@@ -18,4 +72,6 @@ const src = path.join(__dirname, '../sources');
1872
const dest = path.join(__dirname, '../build');
1973
copyRecursive(src, dest);
2074

21-
console.log('Markdown files copied from /sources to /build');
75+
console.log(
76+
'🤖 Markdown files copied from /sources to /build to be accessible from the website'
77+
);

0 commit comments

Comments
 (0)