-
-
Save bryangingechen/90e9c5cc453c16014b431e0b8bcd90c6 to your computer and use it in GitHub Desktop.
reindex Observable notebook json (see https://gist.github.com/bryangingechen/9d86f1e5ec01674a32dd9c54bdc13947)
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
const fs = require('fs'); | |
const [fileName, outputFile] = process.argv.slice(2); | |
if (fileName && outputFile) { | |
const nodes = JSON.parse(fs.readFileSync(fileName, 'utf8')); | |
if (!Array.isArray(nodes)) | |
return console.log(`${fileName} could not be parsed into an Array`); | |
if (nodes.some(d => Object.keys(d).some(e => !['id', 'value', 'pinned'].includes(e)))) | |
return console.log(`the objects in ${fileName} may only have 'id', 'version' and 'pinned' properties`); | |
if (nodes.some(d => d.hasOwnProperty('value') && typeof d.value !== 'string') || | |
nodes.some(d => d.hasOwnProperty('pinned') && typeof d.pinned !== 'boolean')) | |
return console.log(`the 'value' and 'pinnned' values in ${fileName}, if present, must be Strings and Booleans, respectively`); | |
const newNodes = nodes.reduce((acc, node) => { | |
// mutates the elements of nodes, so don't use nodes again after this! | |
node.id = acc.length; | |
acc.push(node); | |
return acc; | |
}, []); | |
console.log(`${newNodes.length} cells, can be reuploaded as version ${newNodes.length+1}`); | |
console.log(`writing to ${outputFile}`); | |
fs.writeFileSync(outputFile, JSON.stringify(newNodes), {flag:'wx'}); | |
} else { | |
console.log(`run with: | |
node reindex-nbjson.js inputFile outputFile`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment