-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-assets.js
More file actions
65 lines (52 loc) · 2.13 KB
/
cleanup-assets.js
File metadata and controls
65 lines (52 loc) · 2.13 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
const fs = require('fs');
const path = require('path');
// Directory paths
const markdownDir = './';
const assetsDir = './.gitbook/assets';
// Function to get all markdown files
function getMarkdownFiles(dir) {
return fs.readdirSync(dir).filter(file => file.endsWith('.md'));
}
// Function to extract asset references from markdown files
function extractAssetReferences(markdownFiles) {
const assetReferences = new Set();
const assetRegex = /!\[.*?\]\((.*?)\)/g; // Matches image references
markdownFiles.forEach(file => {
const content = fs.readFileSync(file, 'utf-8');
let match;
while ((match = assetRegex.exec(content)) !== null) {
const assetPath = match[1].startsWith('.gitbook/assets/') ? match[1] : `.gitbook/assets/${match[1]}`;
assetReferences.add(path.normalize(assetPath)); // Normalize and capture the asset path
console.log(`Found asset reference: ${assetPath}`); // Log found asset references
}
});
return assetReferences;
}
// Function to list all assets in the assets directory
function listAssets() {
return fs.readdirSync(assetsDir);
}
// Function to delete unreferenced assets
function deleteUnreferencedAssets(assetReferences) {
const assets = listAssets();
assets.forEach(asset => {
const assetPath = path.resolve(path.join(assetsDir, asset));
const normalizedAssetPath = path.resolve(path.normalize(assetPath));
const hasReference = Array.from(assetReferences).some(ref => path.resolve(ref) === normalizedAssetPath);
if (!hasReference) {
fs.unlinkSync(assetPath);
console.log(`Deleted unreferenced asset: ${asset}`);
}
});
}
// Main function
function main() {
const markdownFiles = getMarkdownFiles(markdownDir);
const assetReferences = extractAssetReferences(markdownFiles);
const totalAssets = listAssets();
console.log(`Total assets in directory: ${totalAssets.length}`);
console.log(`Referenced assets from markdown files: ${Array.from(assetReferences)}`);
// Proceed with deletion
deleteUnreferencedAssets(assetReferences);
}
main();