-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
779 lines (693 loc) · 21.2 KB
/
app.js
File metadata and controls
779 lines (693 loc) · 21.2 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// CodeMd - Main Application
const notyf = new Notyf({
duration: 3000,
position: { x: "right", y: "top" },
});
let fileHandles = []; // {file, path, size}
let markdown = "";
let projectRootName = "project-structure"; // Default fallback name
/* ---------- DOM References ---------- */
const picker = document.getElementById("folderPicker");
const selectBtn = document.getElementById("selectBtn");
const dropZone = document.getElementById("dropZone");
const progress = document.getElementById("progress");
const tree = $("#tree");
const copyBtn = document.getElementById("copyBtn");
const downloadBtn = document.getElementById("downloadBtn");
const resetBtn = document.getElementById("resetBtn");
const preview = document.getElementById("preview");
const previewCode = document.getElementById("previewCode");
const filterTree = document.getElementById("filterTree");
const fileStats = document.getElementById("fileStats");
/* ---------- Theme Management ---------- */
const themeToggle = document.getElementById("themeToggle");
// Initialize theme from localStorage or system preference
function initTheme() {
const savedTheme = localStorage.getItem("theme");
const systemDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
const isDark = savedTheme === "dark" || (!savedTheme && systemDark);
document.documentElement.dataset.theme = isDark ? "dark" : "";
updateThemeIcon(isDark);
}
function updateThemeIcon(isDark) {
themeToggle.innerHTML = `<i class="bi ${
isDark ? "bi-sun-fill" : "bi-moon-fill"
}"></i>`;
}
themeToggle.onclick = () => {
const isDark = document.documentElement.dataset.theme === "dark";
const newTheme = isDark ? "" : "dark";
document.documentElement.dataset.theme = newTheme;
localStorage.setItem("theme", newTheme || "light");
updateThemeIcon(!isDark);
};
/* ---------- File Input Handling ---------- */
selectBtn.onclick = () => picker.click();
picker.onchange = handleFiles;
/* ---------- Drag & Drop ---------- */
["dragenter", "dragover"].forEach((evt) => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
dropZone.classList.add("drag");
});
});
["dragleave", "drop"].forEach((evt) => {
dropZone.addEventListener(evt, (e) => {
e.preventDefault();
dropZone.classList.remove("drag");
});
});
dropZone.addEventListener("drop", async (e) => {
e.preventDefault();
dropZone.classList.remove("drag");
// Handle DataTransferItemList for better folder support
if (e.dataTransfer.items) {
const items = Array.from(e.dataTransfer.items);
const files = [];
// Process each item
for (const item of items) {
if (item.kind === 'file') {
const entry = item.webkitGetAsEntry ? item.webkitGetAsEntry() : null;
if (entry) {
if (entry.isDirectory) {
// Recursively read directory
await readDirectory(entry, entry.name, files);
} else if (entry.isFile) {
// Single file
entry.file((file) => {
// Create a new file object with proper path
const fileWithPath = new File([file], file.name, { type: file.type });
Object.defineProperty(fileWithPath, 'webkitRelativePath', {
value: entry.fullPath.startsWith('/') ? entry.fullPath.slice(1) : entry.fullPath,
writable: false
});
files.push(fileWithPath);
});
}
} else {
// Fallback for browsers that don't support webkitGetAsEntry
const file = item.getAsFile();
if (file) {
files.push(file);
}
}
}
}
// Wait a bit to ensure all files are processed
await new Promise(resolve => setTimeout(resolve, 100));
if (files.length > 0) {
handleFiles({ target: { files } });
} else {
notyf.error("No valid files found. Please try selecting the folder instead.");
}
} else {
// Fallback to old method if DataTransferItemList not supported
const files = Array.from(e.dataTransfer.files);
if (files.length > 0) {
// Check if files have proper paths
const hasValidPaths = files.some(f => f.webkitRelativePath);
if (!hasValidPaths && files.length > 1) {
notyf.error("Drag & drop may not preserve folder structure in this browser. Please use 'Select Folder' instead.");
return;
}
handleFiles({ target: { files } });
}
}
});
/* ---------- Directory Reading Helper for Drag & Drop ---------- */
async function readDirectory(directoryEntry, path, fileArray) {
const directoryReader = directoryEntry.createReader();
return new Promise((resolve, reject) => {
const readEntries = () => {
directoryReader.readEntries(async (entries) => {
if (entries.length === 0) {
// No more entries, we're done with this directory
resolve();
return;
}
// Process all entries in this batch
for (const entry of entries) {
if (entry.isFile) {
await new Promise((resolveFile) => {
entry.file((file) => {
// Create file with proper relative path
const relativePath = path ? `${path}/${file.name}` : file.name;
const fileWithPath = new File([file], file.name, { type: file.type });
Object.defineProperty(fileWithPath, 'webkitRelativePath', {
value: relativePath,
writable: false
});
fileArray.push(fileWithPath);
resolveFile();
}, (error) => {
console.error('Error reading file:', error);
resolveFile();
});
});
} else if (entry.isDirectory) {
// Recursively read subdirectory
const subPath = path ? `${path}/${entry.name}` : entry.name;
await readDirectory(entry, subPath, fileArray);
}
}
// Continue reading if there might be more entries
readEntries();
}, (error) => {
console.error('Error reading directory:', error);
reject(error);
});
};
readEntries();
});
}
/* ---------- Reset Functionality ---------- */
resetBtn.onclick = () => {
fileHandles = [];
markdown = "";
projectRootName = "project-structure"; // Reset to default
dropZone.hidden = false;
document.getElementById("treeContainer").hidden = true;
preview.hidden = true;
progress.hidden = true;
progress.value = 0;
tree.jstree("destroy");
filterTree.value = "";
previewCode.textContent = "";
notyf.success("Reset complete!");
};
/* ---------- Main File Processing ---------- */
async function handleFiles({ target }) {
const files = Array.from(target.files);
if (!files.length) {
notyf.error("No files selected");
return;
}
fileHandles = [];
// Determine root name with better fallback logic
let rootName = "Selected Files";
if (files[0].webkitRelativePath) {
rootName = files[0].webkitRelativePath.split("/")[0];
} else if (files.length === 1) {
rootName = files[0].name;
} else {
// Try to find a common pattern in file names
const firstPath = files[0].name;
rootName = firstPath.split(".")[0] || "Project";
}
// Store the root name globally for download filename
projectRootName = rootName;
progress.value = 0;
progress.hidden = false;
selectBtn.classList.add("loading");
const total = files.length;
let processed = 0;
let totalSize = 0;
try {
for (let i = 0; i < total; i++) {
const file = files[i];
// Better path resolution
let path = file.webkitRelativePath || file.name;
// Ensure path doesn't start with a slash
if (path.startsWith('/')) {
path = path.slice(1);
}
// Skip hidden files and common ignore patterns
if (shouldSkipFile(path)) {
processed++;
progress.value = (processed / total) * 100;
continue;
}
totalSize += file.size;
fileHandles.push({
file,
path,
size: file.size,
isBinary: isBinary(file.name),
});
processed++;
progress.value = (processed / total) * 100;
// Allow UI to update
if (i % 10 === 0) {
await new Promise((resolve) => setTimeout(resolve, 1));
}
}
if (fileHandles.length === 0) {
notyf.error("No valid files found after filtering");
progress.hidden = true;
selectBtn.classList.remove("loading");
return;
}
updateFileStats(fileHandles.length, totalSize);
await renderTree(rootName);
await buildMarkdown(rootName);
progress.hidden = true;
dropZone.hidden = true;
document.getElementById("treeContainer").hidden = false;
notyf.success(`Processed ${fileHandles.length} files successfully!`);
} catch (error) {
console.error("Error processing files:", error);
notyf.error(`Error processing files: ${error.message}`);
} finally {
selectBtn.classList.remove("loading");
}
}
/* ---------- File Filtering ---------- */
function shouldSkipFile(path) {
const skipPatterns = [
/\/\.git\//,
/\/node_modules\//,
/\/\.vscode\//,
/\/\.idea\//,
/\/dist\//,
/\/build\//,
/\/coverage\//,
/\/\.nyc_output\//,
/\/\.DS_Store$/,
/\/Thumbs\.db$/,
/\/\.gitignore$/,
/\/\.gitkeep$/,
];
return skipPatterns.some((pattern) => pattern.test(path));
}
function isBinary(name) {
const ext = name.split(".").pop()?.toLowerCase() || "";
const binaryExts = [
"jpg",
"jpeg",
"png",
"gif",
"ico",
"webp",
"svg",
"bmp",
"tiff",
"pdf",
"doc",
"docx",
"xls",
"xlsx",
"ppt",
"pptx",
"zip",
"rar",
"7z",
"tar",
"gz",
"bz2",
"mp3",
"mp4",
"avi",
"mov",
"wmv",
"flv",
"woff",
"woff2",
"ttf",
"eot",
"otf",
"exe",
"dll",
"so",
"dylib",
"class",
"jar",
"war",
];
return binaryExts.includes(ext);
}
/* ---------- File Statistics ---------- */
function updateFileStats(fileCount, totalSize) {
const sizeFormatted = formatBytes(totalSize);
fileStats.textContent = `${fileCount} files • ${sizeFormatted}`;
}
function formatBytes(bytes) {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
}
/* ---------- Tree Rendering ---------- */
async function renderTree(rootName) {
const nodes = buildJsTreeNodes(rootName);
tree.jstree("destroy");
tree.jstree({
core: {
data: nodes,
themes: {
name: "default",
responsive: true,
icons: true, // Enable icons
},
},
plugins: ["search", "types"],
types: {
default: {
icon: "bi bi-file-earmark",
},
folder: {
icon: "bi bi-folder-fill",
},
file: {
icon: "bi bi-file-earmark-code",
},
},
});
// Setup search
filterTree.addEventListener("keyup", () => {
const searchTerm = filterTree.value.trim();
tree.jstree(true).search(searchTerm);
});
}
function buildJsTreeNodes(rootName) {
const map = new Map();
const root = {
id: rootName,
text: rootName,
children: [],
state: { opened: true },
type: "folder",
};
map.set(rootName, root);
// Sort files for consistent ordering
const sortedFiles = [...fileHandles].sort((a, b) =>
a.path.localeCompare(b.path)
);
sortedFiles.forEach(({ path, size, isBinary }) => {
const parts = path.split("/");
let parent = root;
for (let i = 1; i < parts.length; i++) {
const subPath = parts.slice(0, i + 1).join("/");
if (!map.has(subPath)) {
const isFile = i === parts.length - 1;
const sizeText = isFile && size ? ` (${formatBytes(size)})` : "";
const binaryText = isFile && isBinary ? " [binary]" : "";
const node = {
id: subPath,
text: `${parts[i]}${sizeText}${binaryText}`,
children: isFile ? undefined : [],
type: isFile ? "file" : "folder",
icon: isFile ? getJsTreeFileIcon(parts[i]) : "bi bi-folder-fill",
};
map.set(subPath, node);
parent.children.push(node);
}
parent = map.get(subPath);
}
});
return [root];
}
/* Get Bootstrap Icon class for jsTree */
function getJsTreeFileIcon(filename) {
const ext = filename.split(".").pop()?.toLowerCase() || "";
const iconMap = {
js: "bi bi-filetype-js",
jsx: "bi bi-filetype-jsx",
ts: "bi bi-filetype-ts",
tsx: "bi bi-filetype-tsx",
html: "bi bi-filetype-html",
htm: "bi bi-filetype-html",
css: "bi bi-filetype-css",
scss: "bi bi-filetype-scss",
sass: "bi bi-filetype-sass",
json: "bi bi-filetype-json",
xml: "bi bi-filetype-xml",
yaml: "bi bi-filetype-yml",
yml: "bi bi-filetype-yml",
md: "bi bi-filetype-md",
txt: "bi bi-filetype-txt",
py: "bi bi-filetype-py",
java: "bi bi-filetype-java",
cpp: "bi bi-filetype-cpp",
c: "bi bi-filetype-c",
php: "bi bi-filetype-php",
rb: "bi bi-filetype-rb",
go: "bi bi-filetype-go",
rs: "bi bi-filetype-rs",
sh: "bi bi-filetype-sh",
sql: "bi bi-filetype-sql",
png: "bi bi-file-image",
jpg: "bi bi-file-image",
jpeg: "bi bi-file-image",
gif: "bi bi-file-image",
svg: "bi bi-file-image",
pdf: "bi bi-file-pdf",
zip: "bi bi-file-zip",
tar: "bi bi-file-zip",
gz: "bi bi-file-zip",
mp3: "bi bi-file-music",
mp4: "bi bi-file-play",
avi: "bi bi-file-play",
};
return iconMap[ext] || "bi bi-file-earmark-code";
}
/* Get emoji icon for markdown output (kept for markdown compatibility) */
function getMarkdownFileIcon(filename) {
const ext = filename.split(".").pop()?.toLowerCase() || "";
const iconMap = {
js: "📄",
ts: "📘",
jsx: "⚛️",
tsx: "⚛️",
html: "🌐",
css: "🎨",
scss: "🎨",
sass: "🎨",
json: "📋",
xml: "📋",
yaml: "📋",
yml: "📋",
md: "📝",
txt: "📄",
py: "🐍",
java: "☕",
cpp: "⚙️",
c: "⚙️",
php: "🐘",
rb: "💎",
go: "🐹",
png: "🖼️",
jpg: "🖼️",
jpeg: "🖼️",
gif: "🖼️",
svg: "🖼️",
pdf: "📕",
zip: "📦",
tar: "📦",
gz: "📦",
};
return iconMap[ext] || "📄";
}
/* ---------- ASCII Tree Generation ---------- */
function renderAsciiTree(root) {
const lines = [];
const tree = {};
// Build tree structure
fileHandles.forEach(({ path }) => {
const parts = path.split("/");
let current = tree;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (!current[part]) {
current[part] = i === parts.length - 1 ? null : {}; // null for files, {} for folders
}
if (current[part] !== null) {
current = current[part];
}
}
});
function buildLines(obj, prefix = "", isRoot = true) {
const entries = Object.entries(obj).sort(([a, aVal], [b, bVal]) => {
// Folders first, then files
if ((aVal === null) !== (bVal === null)) {
return aVal === null ? 1 : -1;
}
return a.localeCompare(b);
});
entries.forEach(([name, value], index) => {
const isLast = index === entries.length - 1;
const isFile = value === null;
if (isRoot && entries.length === 1) {
// Root folder
lines.push(name + "/");
if (value && Object.keys(value).length > 0) {
buildLines(value, "", false);
}
} else {
const connector = isLast ? "└── " : "├── ";
const suffix = isFile ? "" : "/";
lines.push(prefix + connector + name + suffix);
if (value && Object.keys(value).length > 0) {
const newPrefix = prefix + (isLast ? " " : "│ ");
buildLines(value, newPrefix, false);
}
}
});
}
buildLines(tree);
return lines.join("\n");
}
/* ---------- Markdown Generation ---------- */
async function buildMarkdown(rootName) {
markdown = "";
// ASCII Tree - Clean format like your specification
const asciiTree = renderAsciiTree(rootName);
markdown += `${asciiTree}\n\n`;
// File Contents with clean separators
const textFileHandles = fileHandles.filter((f) => !f.isBinary);
const sortedTextFiles = textFileHandles.sort((a, b) =>
a.path.localeCompare(b.path)
);
for (let i = 0; i < sortedTextFiles.length; i++) {
const { file, path } = sortedTextFiles[i];
try {
// Skip very large files
if (file.size > 1024 * 1024) {
// 1MB
const fileIcon = getMarkdownFileIcon(path.split("/").pop());
markdown += `---\n\n### ${fileIcon} \`${path}\`\n\n`;
markdown += `*File too large (${formatBytes(
file.size
)}) - content skipped*\n\n`;
continue;
}
const content = await file.text();
const ext = path.split(".").pop()?.toLowerCase() || "";
const lang = mapExtToLang(ext);
// Clean format with separators
const fileIcon = getMarkdownFileIcon(path.split("/").pop());
markdown += `---\n\n### ${fileIcon} \`${path}\`\n\n`;
markdown += `\`\`\`${lang}\n${escapeCode(content)}\n\`\`\`\n\n`;
} catch (error) {
console.error(`Error reading file ${path}:`, error);
const fileIcon = getMarkdownFileIcon(path.split("/").pop());
markdown += `---\n\n### ${fileIcon} \`${path}\`\n\n`;
markdown += `*Error reading file content*\n\n`;
}
// Update progress and allow UI updates
if (i % 5 === 0) {
await new Promise((resolve) => setTimeout(resolve, 1));
}
}
// List binary files if any
const binaryFileHandles = fileHandles.filter((f) => f.isBinary);
if (binaryFileHandles.length > 0) {
markdown += `---\n\n## 🗂️ Binary Files (Skipped)\n\n`;
binaryFileHandles.forEach(({ path, size }) => {
markdown += `- \`${path}\` (${formatBytes(size)})\n`;
});
markdown += "\n";
}
// Update preview
previewCode.textContent = markdown;
if (window.Prism) {
Prism.highlightElement(previewCode);
}
preview.hidden = false;
}
function mapExtToLang(ext) {
const langMap = {
js: "javascript",
jsx: "javascript",
ts: "typescript",
tsx: "typescript",
html: "markup",
htm: "markup",
xml: "markup",
css: "css",
scss: "scss",
sass: "sass",
less: "less",
json: "json",
yaml: "yaml",
yml: "yaml",
md: "markdown",
markdown: "markdown",
py: "python",
java: "java",
c: "c",
cpp: "cpp",
cc: "cpp",
php: "php",
rb: "ruby",
go: "go",
rs: "rust",
sh: "bash",
bash: "bash",
zsh: "bash",
sql: "sql",
r: "r",
swift: "swift",
kt: "kotlin",
dart: "dart",
scala: "scala",
clj: "clojure",
};
return langMap[ext] || ext;
}
function escapeCode(str) {
return str.replace(/```/g, "\\`\\`\\`");
}
/* ---------- Export Functions ---------- */
copyBtn.onclick = async () => {
try {
await navigator.clipboard.writeText(markdown);
notyf.success("Markdown copied to clipboard!");
} catch (error) {
console.error("Copy failed:", error);
notyf.error("Failed to copy to clipboard");
}
};
downloadBtn.onclick = () => {
try {
const blob = new Blob([markdown], { type: "text/markdown;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
// Use dynamic filename based on project root name
const sanitizedName = projectRootName
.replace(/[^a-z0-9]/gi, '-') // Replace non-alphanumeric with dash
.replace(/-+/g, '-') // Replace multiple dashes with single
.replace(/^-|-$/g, '') // Remove leading/trailing dashes
.toLowerCase(); // Convert to lowercase
a.download = `${sanitizedName || 'project-structure'}.md`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
notyf.success("Markdown file downloaded!");
} catch (error) {
console.error("Download failed:", error);
notyf.error("Failed to download file");
}
};
/* ---------- Initialize Application ---------- */
document.addEventListener("DOMContentLoaded", () => {
initTheme();
// System theme change listener
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
if (!localStorage.getItem("theme")) {
document.documentElement.dataset.theme = e.matches ? "dark" : "";
updateThemeIcon(e.matches);
}
});
// Check drag and drop support
const div = document.createElement('div');
const supportsDragDrop = (('draggable' in div) || ('ondragstart' in div && 'ondrop' in div));
const supportsFileAPI = 'FileReader' in window;
if (!supportsDragDrop || !supportsFileAPI) {
console.warn('Drag and drop not fully supported');
const dropHint = document.querySelector('#dropZone p');
if (dropHint) {
dropHint.textContent = 'Drag & drop not supported in this browser - please use the select button';
}
}
});
// Handle browser compatibility
if (!("webkitdirectory" in document.createElement("input"))) {
console.warn("Directory upload not supported in this browser");
selectBtn.innerHTML =
'<i class="bi bi-files"></i> Select Files (Folder upload not supported)';
}