-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vitaly.basaraba
committed
Jan 19, 2025
1 parent
27e396c
commit 7c3fc72
Showing
2 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
// Get the input file path from command-line arguments | ||
const inputFilePath = process.argv[2]; | ||
if (!inputFilePath) { | ||
console.error('Please provide the path to the input JSON file as an argument.'); | ||
process.exit(1); | ||
} | ||
|
||
// Resolve the input file path | ||
const dataPath = path.resolve(__dirname, inputFilePath); | ||
|
||
// Read and parse the JSON file | ||
let data; | ||
try { | ||
data = require(dataPath); | ||
} catch (err) { | ||
console.error(`Failed to read or parse the file at ${dataPath}:`, err.message); | ||
process.exit(1); | ||
} | ||
|
||
// Function to generate a unique link based on the title | ||
function generateLink(title) { | ||
return `#${title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; | ||
} | ||
|
||
// Ensure all objects have a `link` property | ||
data = data.map(obj => { | ||
if (!obj.link) { | ||
obj.link = generateLink(obj.title || 'untitled'); | ||
} | ||
return obj; | ||
}); | ||
|
||
try { | ||
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2), 'utf8'); | ||
console.log(`Data has been saved to ${dataPath}`); | ||
} catch (err) { | ||
console.error(`Failed to write to file at ${dataPath}:`, err.message); | ||
process.exit(1); | ||
} |