-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new
Export Note as HTML File
option (#155)
- Loading branch information
1 parent
767f8f9
commit cf24656
Showing
3 changed files
with
87 additions
and
55 deletions.
There are no files selected for viewing
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
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
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(); |