Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Serialize all attachments to files
Browse files Browse the repository at this point in the history
  • Loading branch information
vHanda committed Feb 14, 2017
1 parent 50a3f2f commit c80a8c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 3 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ var convert = function(filePath, outputDir) {
var note = parser(data);
var output = serializer.serialize(note);

if (output.length == 1) {
var d = output[0];
console.log(d[0]);
output.forEach(d => {
console.log(filePath, d[0]);
fs.writeFileSync(outputDir + '/' + d[0], d[1]);
}
});
}

var files = fs.readdirSync(inputDir);
Expand Down
20 changes: 17 additions & 3 deletions serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ var uuidV4 = require('uuid/v4');
*/
var serialize = function(note) {
// FIXME: Serialize the attachments!
return [
[generateFilename(note), generateOutputFile(note)]
];
var out = note.attachments.map(generateAttachment);
var mainOutput = generateOutputFile(note);
out.forEach(a => {
var fileName = a[0];
mainOutput += '\n![](./' + fileName + ')\n';
});
out.push([generateFilename(note), mainOutput]);
return out;
};


Expand Down Expand Up @@ -60,6 +65,15 @@ function generateFilename(note) {
return sanitizeString(note.title || note.date || uuidV4()) + '.md';
}

function generateAttachment(a) {
var regex = /^data:.+\/(.+);base64,(.*)$/;
var matches = a.substr(0, 100).match(regex);
var ext = matches[1];
var data = a.substr(a.indexOf('base64') + 7)
var buffer = new Buffer(data, 'base64');
return [uuidV4() + '.' + ext, buffer];
}

module.exports = {
serialize: serialize
};

0 comments on commit c80a8c3

Please sign in to comment.