Skip to content

Commit

Permalink
Added new Export Note as HTML File option (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani committed Jan 19, 2019
1 parent 767f8f9 commit cf24656
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 55 deletions.
55 changes: 0 additions & 55 deletions src/md.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/menu/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ module.exports = {
click() {
activate('export');
}
}, {
label: 'HTML File',
accelerator: setAcc('export-html', 'CmdorCtrl+Shift+H'),
click() {
activate('export-as-html');
}
}, {
label: 'Markdown File',
accelerator: setAcc('export-markdown', 'CmdorCtrl+O'),
Expand Down
81 changes: 81 additions & 0 deletions src/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';
const {writeFile} = require('fs');
const electron = require('electron');
const Turndown = require('turndown');
const {is} = require('./util');
const mode = require('./mode');

const {dialog} = electron.remote;
const {log} = console;

class Save {
get _opts() {
return {
md: {
filters: [
{
name: 'Markdown File',
extensions: ['md']
}, {
name: 'All Files',
extensions: ['*']
}
]
},
html: {
filters: [
{
name: 'HTML File',
extensions: ['html']
}, {
name: 'All Files',
extensions: ['*']
}
]
}
};
}

_getTitle() {
const title = document.querySelector('#gwt-debug-NoteTitleView-label').innerHTML;
return title.length > 0 ? title.trim().replace(/ /g, ' ') : 'note';
}

_toHTML(noteFrame) {
return noteFrame.contentDocument.body.innerHTML;
}

_toMarkdown(noteFrame) {
const turndownUtil = new Turndown();
return turndownUtil.turndown(noteFrame.contentDocument.body);
}

_save(opts, data) {
const options = Object.assign(opts, {defaultPath: this._getTitle()});

dialog.showSaveDialog(options, path => {
if (is.undef(path)) {
return;
}

writeFile(path, data, error => {
if (error) {
dialog.showErrorBox('Failed to export note.', error.message);
return log(error);
}
});
});
}

async md() {
const noteFrame = await mode.getNoteFrame();
return this._save(this._opts.md, this._toMarkdown(noteFrame));
}

async html() {
const noteFrame = await mode.getNoteFrame();
return this._save(this._opts.html, this._toHTML(noteFrame));
}
}

module.exports = new Save();

0 comments on commit cf24656

Please sign in to comment.