Skip to content

Commit

Permalink
filesystem does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
madneal committed Jun 4, 2017
1 parent 60c1165 commit 7af6d96
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
"https://*/*",
{"fileSystem": ["write", "retainEntries", "directory"]}
],
"browser_action": {
"default_title": "Export to Markdown",
Expand Down
58 changes: 52 additions & 6 deletions scripts/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ chrome.browserAction.onClicked.addListener(function(tab) {
})
.then(function(text) {
const story = parseJsonToMarkdown(text);
console.log(story);
return story.markdown;
})
.then(function(contents) {
let savedFileEntry, fileDisplayPath;
doExportToDisk(savedFileEntry);
})
.catch(function(err) {
console.error(err);
Expand Down Expand Up @@ -83,8 +87,9 @@ function processSection(s) {
function processParagraph(p) {
const markups_array = createMarkupsArray(p.markups);
if (markups_array.length > 0) {
var previousIndex = 0, text = p.text, tokens = [];
for (let j = 0; j <markups_array.length; j++) {
let previousIndex = 0, text = p.text, tokens = [];
let j = 0;
for (; j <markups_array.length; j++) {
if (markups_array[j]) {
token = text.substring(previousIndex, j);
previousIndex = j;
Expand All @@ -105,10 +110,10 @@ function processParagraph(p) {
p.text = '\n#' + p.text.replace(/\n/g, '\n#');
break;
case 3:
p.text = '\n##' + p.text.repace(/\n/g, '\n##');
p.text = '\n##' + p.text.replace(/\n/g, '\n##');
break;
case 4: //image and caption
const imageWidth = parseInt(p.metadata.orgionWidth, 10);
const imageWidth = parseInt(p.metadata.originalWidth, 10);
const imageSrc = MEDIUM_IMG_CDN + Math.max(imageWidth*2, 2000) + '/' + p.metadata
.id;
p.text = '\n![' + p.text + '](' + imageSrc + ')';
Expand Down Expand Up @@ -141,8 +146,8 @@ function processParagraph(p) {
p.text = markup + p.text;
if (p.alignment === 2 && p.type !== 6 && p.type !==7) {
p.text = '<center>' + p.text + '</center>';
return p.text;
}
return p.text;
}

function addMarkup(markups_array, open, close, start, end) {
Expand Down Expand Up @@ -182,4 +187,45 @@ function createMarkupsArray(markups) {
}
}
return markups_array;
}

function doExportToDisk(savedFileEntry) {
if (savedFileEntry) {
exportToFileEntry(savedFileEntry);
} else {
chrome.fileSystem.chooseEntry({
type: 'saveFile',
accepts: [{description: 'Markdown files (*.md)',
extensions: ['txt']}],
acceptsAllTypes: true
}, exportToFileEntry);
}
}

function exportToFileEntry(fileEntry) {
savedFileEntry = fileEntry;

chrome.fileSystem.getDisplayPath(fileEntry, function(path) {
fileDisplayPath = path;
});

fileEntry.createWriter(function(fileWriter) {
let truncated = false;
let blob = new Blob([contents]);

fileWriter.onwriteend = function(e) {
if (!truncated) {
truncated = true;
this.truncate(blob.size);
return;
}
console.log('Export to ' + fileDisplayPath + ' completed');
};

fileWriter.onerror = function(e) {
console.error('Export failed: ' + e.toString());
};

fileWriter.write(blob);
})
}

0 comments on commit 7af6d96

Please sign in to comment.