|
1 | 1 | const fs = require('fs'); |
2 | | -const marked = require('./ckeditor/plugins/markdown/js/marked'); |
3 | | - |
| 2 | +const marked = require('marked'); |
| 3 | +const remote = require('electron').remote; |
4 | 4 |
|
5 | 5 | /* ---------------- state --------------------------------------------------- */ |
| 6 | +// default markdown directory |
| 7 | +const defaultMarkdownDir = './markdown'; |
| 8 | + |
6 | 9 | // when the overlay text is specified the editor is hidden |
7 | 10 | let overlayText = 'Please select a document.'; |
8 | 11 |
|
@@ -35,13 +38,45 @@ let tree = null; |
35 | 38 | // timer id for same tree node reselection upon multiple clicks |
36 | 39 | let reselectTimerId = -1; |
37 | 40 |
|
| 41 | +// remote process variable |
| 42 | +const remoteProcess = remote.getGlobal('sharedArgs').proc; |
| 43 | + |
38 | 44 | // shortcut method to obtain the ckeditor instance |
39 | 45 | const editor = () => CKEDITOR.instances.editor1; |
40 | 46 |
|
| 47 | +// app environment detection (https://github.com/electron/electron/pull/5421) |
| 48 | +const isProdEnvironment = () => (remoteProcess.defaultApp === undefined); |
| 49 | + |
| 50 | + |
| 51 | +// return the markdown directory |
| 52 | +const getMarkdownDir = () => { |
| 53 | + // obtain command line arguments |
| 54 | + const args = remoteProcess.argv.slice(isProdEnvironment() ? 1 : 2); |
| 55 | + |
| 56 | + if (args.length === 0) { |
| 57 | + if (args.length > 1) |
| 58 | + console.error('wrong arguments'); // eslint-disable-line no-console |
| 59 | + return defaultMarkdownDir; |
| 60 | + } |
| 61 | + |
| 62 | + // we expect the markdown dir as the only parameter |
| 63 | + return args[0]; |
| 64 | +}; |
| 65 | + |
| 66 | + |
41 | 67 | // walk through a directory tree and return a JS object that will be used to |
42 | 68 | // initialize the treeview sidebar |
43 | 69 | const walk = (dir) => { |
44 | 70 | const tree = []; |
| 71 | + |
| 72 | + try { |
| 73 | + fs.statSync(dir).isDirectory(); |
| 74 | + } |
| 75 | + catch (err) { |
| 76 | + overlayText = 'Cannot find markdown directory'; |
| 77 | + return tree; |
| 78 | + } |
| 79 | + |
45 | 80 | fs.readdirSync(dir).forEach((f) => { |
46 | 81 | const path = dir + '/' + f; |
47 | 82 | if (fs.statSync(path).isDirectory()) |
@@ -194,7 +229,7 @@ $(document).ready(() => { |
194 | 229 | color: "#428BCA", |
195 | 230 | expandIcon: 'glyphicon glyphicon-folder-close', |
196 | 231 | collapseIcon: 'glyphicon glyphicon-folder-open', |
197 | | - data: walk('./testdir'), |
| 232 | + data: walk(getMarkdownDir()), |
198 | 233 | onNodeSelected, |
199 | 234 | onNodeUnselected, |
200 | 235 | onNodeCollapsed: update, |
|
0 commit comments