Skip to content

Instantly share code, notes, and snippets.

@OrigamingWasTaken
Created December 6, 2023 20:18
Show Gist options
  • Save OrigamingWasTaken/7fa8617d7c9b5c35581aba682d27a5bb to your computer and use it in GitHub Desktop.
Save OrigamingWasTaken/7fa8617d7c9b5c35581aba682d27a5bb to your computer and use it in GitHub Desktop.
Encrypt notes
const fs = require('fs');
const path = require('path');
const grayMatter = require('gray-matter');
const { exec } = require("child_process")
function scanContentDirectory(contentDir, publicDir) {
const result = [];
// Function to process files in the content folder
function processContentFiles(folderPath, relativePath = '') {
const files = fs.readdirSync(folderPath);
files.forEach(file => {
const filePath = path.join(folderPath, file);
const relativeFilePath = path.join(relativePath, file);
// Check if it's a directory
if (fs.statSync(filePath).isDirectory()) {
processContentFiles(filePath, relativeFilePath);
} else {
// Check if it's an MD file
if (path.extname(file) === '.md') {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const frontmatter = grayMatter(fileContent).data;
// Generate corresponding HTML file path in the "public" directory
const htmlFileName = path.basename(file, '.md').replace(/\s/g, '-') + '.html';
let fpath = filePath.split("/")
fpath[0] = "public"
const publicPath = path.join(path.dirname(__dirname), fpath.join("/").replace(/\s/g, '-')).replace(/.md$/,".html");
const publicDir = path.dirname(publicPath)
// Add information to the result array
result.push({
path: filePath,
publicPath,
publicDir,
frontmatter,
});
}
}
});
}
// Start processing files in the content folder
processContentFiles(contentDir);
return result;
}
// Example usage
const contentDir = 'content';
const publicDir = 'public';
const scanResult = scanContentDirectory(contentDir, publicDir);
const encrypted = []
for (const note of scanResult) {
const frontmatter = note.frontmatter
if ("password" in frontmatter) {
const cmd = `npx staticrypt ${note.publicPath} -d ${note.publicDir} --password '${frontmatter.password}' --remember 1 --template-instructions '<p>Entrez le mot de passe de cette note pour&nbsp;acc&eacute;der &agrave; son contenu.</p><br><br><p><strong>La seule personne poss&eacute;dant les mot de passes est moi-m&ecirc;me.</strong></p>' --template-title 'Note protégée' --template-error 'Mot de passe invalide' --template-button 'Accéder'`
exec(cmd,(error,stdout,stderr)=>{
if (error) {
console.log(error)
}
if (stdout) {
console.log(stdout)
}
if (stderr) {
console.log(stderr)
}
})
encrypted.push(note)
}
}
console.log(`Encrypted [${encrypted.map(r => r.publicPath)}]`)
process.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment