Skip to content

Commit d79b6e0

Browse files
committed
export folder should also export the attachments -> fixes #2374
1 parent fdfa3bb commit d79b6e0

File tree

6 files changed

+46
-49
lines changed

6 files changed

+46
-49
lines changed

browser/components/MarkdownPreview.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -251,26 +251,7 @@ export default class MarkdownPreview extends React.Component {
251251
}
252252

253253
handleSaveAsMd () {
254-
this.exportAsDocument('md', (noteContent, exportTasks) => {
255-
let result = noteContent
256-
if (this.props && this.props.storagePath && this.props.noteKey) {
257-
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(
258-
noteContent,
259-
this.props.storagePath
260-
)
261-
attachmentsAbsolutePaths.forEach(attachment => {
262-
exportTasks.push({
263-
src: attachment,
264-
dst: attachmentManagement.DESTINATION_FOLDER
265-
})
266-
})
267-
result = attachmentManagement.removeStorageAndNoteReferences(
268-
noteContent,
269-
this.props.noteKey
270-
)
271-
}
272-
return result
273-
})
254+
this.exportAsDocument('md')
274255
}
275256

276257
handleSaveAsHtml () {
@@ -301,11 +282,6 @@ export default class MarkdownPreview extends React.Component {
301282
escapeHtmlCharacters(noteContent, { detectCodeBlock: true })
302283
)
303284
const files = [this.GetCodeThemeLink(codeBlockTheme), ...CSS_FILES]
304-
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(
305-
noteContent,
306-
this.props.storagePath
307-
)
308-
309285
files.forEach(file => {
310286
if (global.process.platform === 'win32') {
311287
file = file.replace('file:///', '')
@@ -317,16 +293,6 @@ export default class MarkdownPreview extends React.Component {
317293
dst: 'css'
318294
})
319295
})
320-
attachmentsAbsolutePaths.forEach(attachment => {
321-
exportTasks.push({
322-
src: attachment,
323-
dst: attachmentManagement.DESTINATION_FOLDER
324-
})
325-
})
326-
body = attachmentManagement.removeStorageAndNoteReferences(
327-
body,
328-
this.props.noteKey
329-
)
330296

331297
let styles = ''
332298
files.forEach(file => {
@@ -359,8 +325,9 @@ export default class MarkdownPreview extends React.Component {
359325
if (filename) {
360326
const content = this.props.value
361327
const storage = this.props.storagePath
328+
const nodeKey = this.props.noteKey
362329

363-
exportNote(storage, content, filename, contentFormatter)
330+
exportNote(nodeKey, storage, content, filename, contentFormatter)
364331
.then(res => {
365332
dialog.showMessageBox(remote.getCurrentWindow(), {
366333
type: 'info',

browser/main/SideNav/StorageItem.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ class StorageItem extends React.Component {
164164
folderKey: data.folderKey,
165165
fileType: data.fileType
166166
})
167+
return data
168+
})
169+
.then(data => {
170+
dialog.showMessageBox(remote.getCurrentWindow(), {
171+
type: 'info',
172+
message: 'Exported to "' + data.exportDir + '"'
173+
})
174+
})
175+
.catch(err => {
176+
dialog.showErrorBox(
177+
'Export error',
178+
err ? err.message || err : 'Unexpected error during export'
179+
)
180+
throw err
167181
})
168182
}
169183
})

browser/main/lib/dataApi/exportFolder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { findStorage } from 'browser/lib/findStorage'
22
import resolveStorageData from './resolveStorageData'
33
import resolveStorageNotes from './resolveStorageNotes'
4+
import exportNote from './exportNote'
45
import filenamify from 'filenamify'
56
import * as path from 'path'
6-
import * as fs from 'fs'
77

88
/**
99
* @param {String} storageKey
@@ -45,9 +45,9 @@ function exportFolder (storageKey, folderKey, fileType, exportDir) {
4545

4646
notes
4747
.filter(note => note.folder === folderKey && note.isTrashed === false && note.type === 'MARKDOWN_NOTE')
48-
.forEach(snippet => {
49-
const notePath = path.join(exportDir, `${filenamify(snippet.title, {replacement: '_'})}.${fileType}`)
50-
fs.writeFileSync(notePath, snippet.content)
48+
.forEach(note => {
49+
const notePath = path.join(exportDir, `${filenamify(note.title, {replacement: '_'})}.${fileType}`)
50+
exportNote(note.key, storage.path, note.content, notePath, null)
5151
})
5252

5353
return {

browser/main/lib/dataApi/exportNote.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,43 @@ import { findStorage } from 'browser/lib/findStorage'
44
const fs = require('fs')
55
const path = require('path')
66

7+
const attachmentManagement = require('./attachmentManagement')
8+
79
/**
8-
* Export note together with images
10+
* Export note together with attachments
911
*
10-
* If images is stored in the storage, creates 'images' subfolder in target directory
11-
* and copies images to it. Changes links to images in the content of the note
12+
* If attachments are stored in the storage, creates 'attachments' subfolder in target directory
13+
* and copies attachments to it. Changes links to images in the content of the note
1214
*
15+
* @param {String} nodeKey key of the node that should be exported
1316
* @param {String} storageKey or storage path
1417
* @param {String} noteContent Content to export
1518
* @param {String} targetPath Path to exported file
1619
* @param {function} outputFormatter
1720
* @return {Promise.<*[]>}
1821
*/
19-
function exportNote (storageKey, noteContent, targetPath, outputFormatter) {
22+
function exportNote (nodeKey, storageKey, noteContent, targetPath, outputFormatter) {
2023
const storagePath = path.isAbsolute(storageKey) ? storageKey : findStorage(storageKey).path
2124
const exportTasks = []
2225

2326
if (!storagePath) {
2427
throw new Error('Storage path is not found')
2528
}
29+
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(
30+
noteContent,
31+
storagePath
32+
)
33+
attachmentsAbsolutePaths.forEach(attachment => {
34+
exportTasks.push({
35+
src: attachment,
36+
dst: attachmentManagement.DESTINATION_FOLDER
37+
})
38+
})
2639

27-
let exportedData = noteContent
40+
let exportedData = attachmentManagement.removeStorageAndNoteReferences(
41+
noteContent,
42+
nodeKey
43+
)
2844

2945
if (outputFormatter) {
3046
exportedData = outputFormatter(exportedData, exportTasks)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"electron-gh-releases": "^2.0.2",
6060
"escape-string-regexp": "^1.0.5",
6161
"file-url": "^2.0.2",
62-
"filenamify": "^2.0.0",
62+
"filenamify": "^2.1.0",
6363
"flowchart.js": "^1.6.5",
6464
"font-awesome": "^4.3.0",
6565
"fs-extra": "^5.0.0",

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,9 +3514,9 @@ filename-reserved-regex@^2.0.0:
35143514
version "2.0.0"
35153515
resolved "https://registry.yarnpkg.com/filename-reserved-regex/-/filename-reserved-regex-2.0.0.tgz#abf73dfab735d045440abfea2d91f389ebbfa229"
35163516

3517-
filenamify@^2.0.0:
3518-
version "2.0.0"
3519-
resolved "https://registry.yarnpkg.com/filenamify/-/filenamify-2.0.0.tgz#bd162262c0b6e94bfbcdcf19a3bbb3764f785695"
3517+
filenamify@^2.1.0:
3518+
version "2.1.0"
3519+
resolved "https://registry.yarnpkg.com/filenamify/-/filenamify-2.1.0.tgz#88faf495fb1b47abfd612300002a16228c677ee9"
35203520
dependencies:
35213521
filename-reserved-regex "^2.0.0"
35223522
strip-outer "^1.0.0"

0 commit comments

Comments
 (0)