-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectMarkdown.js
More file actions
89 lines (73 loc) · 3.16 KB
/
ProjectMarkdown.js
File metadata and controls
89 lines (73 loc) · 3.16 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
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
const fs = require('fs');
const path = require('path');
const directoryPath = __dirname;
const outputFileName = path.basename(directoryPath) + '_structure.md';
function isDirectory(filePath) {
return fs.lstatSync(filePath).isDirectory();
}
function shouldExclude(filePath) {
const excludedPaths = ['.vercel', '.git', 'node_modules'];
return excludedPaths.some(excluded => filePath.includes(path.join(directoryPath, excluded))) ||
path.basename(filePath) === 'package-lock.json';
}
function shouldExcludeContent(filePath) {
const binaryExtensions = ['.ico', '.jpg', '.jpeg', '.png', '.webp', '.svg', '.ttf', '.woff', '.woff2', '.eot', '.otf', '.mp3', '.mp4', '.avi', '.mov', '.pdf', '.docx', '.pptx', '.xlsx', '.bin'];
return binaryExtensions.some(ext => filePath.endsWith(ext));
}
function getFileExtension(filePath) {
return filePath.split('.').pop();
}
function markdownEscape(str) {
return str.replace(/([_#*])/g, '\\$1');
}
function createMarkdownLink(file, relativePath) {
const anchor = relativePath.split('/').join('').split('.').join('');
return `[${markdownEscape(file)}](#${anchor.toLowerCase()})`;
}
function createDirectoryMenu(dir, relativePath = '', depth = 0) {
let markdown = '';
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
const relPath = path.join(relativePath, file);
if (shouldExclude(fullPath)) {
return;
}
const indent = ' '.repeat(depth);
if (isDirectory(fullPath)) {
markdown += `${indent}- ${createMarkdownLink(file, relPath)}\n`;
markdown += createDirectoryMenu(fullPath, relPath, depth + 1);
} else {
markdown += `${indent}- ${createMarkdownLink(file, relPath)}\n`;
}
});
return markdown;
}
function createFileSection(dir, relativePath = '') {
let content = '';
fs.readdirSync(dir).forEach(file => {
const fullPath = path.join(dir, file);
const relPath = path.join(relativePath, file);
if (shouldExclude(fullPath)) {
return;
}
const anchor = relPath.split('/').join('').split('.').join('').toLowerCase();
if (isDirectory(fullPath)) {
content += `\n## ${markdownEscape(relPath)}\n\n`;
content += createFileSection(fullPath, relPath);
} else {
const extension = getFileExtension(file);
let fileContent = shouldExcludeContent(fullPath) ? 'Binary file - content not shown'
: (extension === 'env' ? 'Sensitive content - not shown'
: fs.readFileSync(fullPath, 'utf8'));
content += `\n### ${markdownEscape(relPath)}\n\n`;
content += '```' + extension + '\n';
content += markdownEscape(fileContent) + '\n';
content += '```\n';
}
});
return content;
}
const directoryMenu = createDirectoryMenu(directoryPath);
const fileSections = createFileSection(directoryPath);
const markdownDocument = `# Project Structure\n\n## Directory Menu\n\n${directoryMenu}\n\n## File Contents\n\n${fileSections}`;
fs.writeFileSync(outputFileName, markdownDocument);