Skip to content

Commit 6c57ac8

Browse files
authored
Merge pull request #2383 from ehhc/exportMDexportsAttachments
export folder should also export the attachments -> fixes #2374
2 parents 830ade9 + d79b6e0 commit 6c57ac8

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
@@ -291,26 +291,7 @@ export default class MarkdownPreview extends React.Component {
291291
}
292292

293293
handleSaveAsMd () {
294-
this.exportAsDocument('md', (noteContent, exportTasks) => {
295-
let result = noteContent
296-
if (this.props && this.props.storagePath && this.props.noteKey) {
297-
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(
298-
noteContent,
299-
this.props.storagePath
300-
)
301-
attachmentsAbsolutePaths.forEach(attachment => {
302-
exportTasks.push({
303-
src: attachment,
304-
dst: attachmentManagement.DESTINATION_FOLDER
305-
})
306-
})
307-
result = attachmentManagement.removeStorageAndNoteReferences(
308-
noteContent,
309-
this.props.noteKey
310-
)
311-
}
312-
return result
313-
})
294+
this.exportAsDocument('md')
314295
}
315296

316297
handleSaveAsHtml () {
@@ -339,11 +320,6 @@ export default class MarkdownPreview extends React.Component {
339320
)
340321
let body = this.markdown.render(noteContent)
341322
const files = [this.GetCodeThemeLink(codeBlockTheme), ...CSS_FILES]
342-
const attachmentsAbsolutePaths = attachmentManagement.getAbsolutePathsOfAttachmentsInContent(
343-
noteContent,
344-
this.props.storagePath
345-
)
346-
347323
files.forEach(file => {
348324
if (global.process.platform === 'win32') {
349325
file = file.replace('file:///', '')
@@ -355,16 +331,6 @@ export default class MarkdownPreview extends React.Component {
355331
dst: 'css'
356332
})
357333
})
358-
attachmentsAbsolutePaths.forEach(attachment => {
359-
exportTasks.push({
360-
src: attachment,
361-
dst: attachmentManagement.DESTINATION_FOLDER
362-
})
363-
})
364-
body = attachmentManagement.removeStorageAndNoteReferences(
365-
body,
366-
this.props.noteKey
367-
)
368334

369335
let styles = ''
370336
files.forEach(file => {
@@ -397,8 +363,9 @@ export default class MarkdownPreview extends React.Component {
397363
if (filename) {
398364
const content = this.props.value
399365
const storage = this.props.storagePath
366+
const nodeKey = this.props.noteKey
400367

401-
exportNote(storage, content, filename, contentFormatter)
368+
exportNote(nodeKey, storage, content, filename, contentFormatter)
402369
.then(res => {
403370
dialog.showMessageBox(remote.getCurrentWindow(), {
404371
type: 'info',

browser/main/SideNav/StorageItem.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ class StorageItem extends React.Component {
204204
folderKey: data.folderKey,
205205
fileType: data.fileType
206206
})
207+
return data
208+
})
209+
.then(data => {
210+
dialog.showMessageBox(remote.getCurrentWindow(), {
211+
type: 'info',
212+
message: 'Exported to "' + data.exportDir + '"'
213+
})
214+
})
215+
.catch(err => {
216+
dialog.showErrorBox(
217+
'Export error',
218+
err ? err.message || err : 'Unexpected error during export'
219+
)
220+
throw err
207221
})
208222
}
209223
})

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
@@ -62,7 +62,7 @@
6262
"escape-string-regexp": "^1.0.5",
6363
"file-uri-to-path": "^1.0.0",
6464
"file-url": "^2.0.2",
65-
"filenamify": "^2.0.0",
65+
"filenamify": "^2.1.0",
6666
"flowchart.js": "^1.6.5",
6767
"font-awesome": "^4.3.0",
6868
"fs-extra": "^5.0.0",

yarn.lock

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

3594-
filenamify@^2.0.0:
3595-
version "2.0.0"
3596-
resolved "https://registry.yarnpkg.com/filenamify/-/filenamify-2.0.0.tgz#bd162262c0b6e94bfbcdcf19a3bbb3764f785695"
3594+
filenamify@^2.1.0:
3595+
version "2.1.0"
3596+
resolved "https://registry.yarnpkg.com/filenamify/-/filenamify-2.1.0.tgz#88faf495fb1b47abfd612300002a16228c677ee9"
35973597
dependencies:
35983598
filename-reserved-regex "^2.0.0"
35993599
strip-outer "^1.0.0"

0 commit comments

Comments
 (0)