-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Export all markdown files in a storage #2292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,63 @@ | ||
| import { findStorage } from 'browser/lib/findStorage' | ||
| import resolveStorageData from './resolveStorageData' | ||
| import resolveStorageNotes from './resolveStorageNotes' | ||
| import filenamify from 'filenamify' | ||
| import * as path from 'path' | ||
| import * as fs from 'fs' | ||
|
|
||
| /** | ||
| * @param {String} storageKey | ||
| * @param {String} fileType | ||
| * @param {String} exportDir | ||
| * | ||
| * @return {Object} | ||
| * ``` | ||
| * { | ||
| * storage: Object, | ||
| * fileType: String, | ||
| * exportDir: String | ||
| * } | ||
| * ``` | ||
| */ | ||
|
|
||
| function exportStorage (storageKey, fileType, exportDir) { | ||
| let targetStorage | ||
| try { | ||
| targetStorage = findStorage(storageKey) | ||
| } catch (e) { | ||
| return Promise.reject(e) | ||
| } | ||
|
|
||
| return resolveStorageData(targetStorage) | ||
| .then(storage => ( | ||
| resolveStorageNotes(storage).then(notes => ({storage, notes})) | ||
| )) | ||
| .then(function exportNotes (data) { | ||
| const { storage, notes } = data | ||
| const folderNamesMapping = {} | ||
| storage.folders.forEach(folder => { | ||
| const folderExportedDir = path.join(exportDir, filenamify(folder.name, {replacement: '_'})) | ||
| folderNamesMapping[folder.key] = folderExportedDir | ||
| // make sure directory exists | ||
| try { | ||
| fs.mkdirSync(folderExportedDir) | ||
| } catch (e) {} | ||
| }) | ||
| notes | ||
| .filter(note => !note.isTrashed && note.type === 'MARKDOWN_NOTE') | ||
| .forEach(markdownNote => { | ||
| const folderExportedDir = folderNamesMapping[markdownNote.folder] | ||
| const snippetName = `${filenamify(markdownNote.title, {replacement: '_'})}.${fileType}` | ||
| const notePath = path.join(folderExportedDir, snippetName) | ||
| fs.writeFileSync(notePath, markdownNote.content) | ||
| }) | ||
|
|
||
| return { | ||
| storage, | ||
| fileType, | ||
| exportDir | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| module.exports = exportStorage |
This file contains hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| const test = require('ava') | ||
| const exportStorage = require('browser/main/lib/dataApi/exportStorage') | ||
|
|
||
| global.document = require('jsdom').jsdom('<body></body>') | ||
| global.window = document.defaultView | ||
| global.navigator = window.navigator | ||
|
|
||
| const Storage = require('dom-storage') | ||
| const localStorage = window.localStorage = global.localStorage = new Storage(null, { strict: true }) | ||
| const path = require('path') | ||
| const TestDummy = require('../fixtures/TestDummy') | ||
| const os = require('os') | ||
| const fs = require('fs') | ||
| const sander = require('sander') | ||
|
|
||
| test.beforeEach(t => { | ||
| t.context.storageDir = path.join(os.tmpdir(), 'test/export-storage') | ||
| t.context.storage = TestDummy.dummyStorage(t.context.storageDir) | ||
| t.context.exportDir = path.join(os.tmpdir(), 'test/export-storage-output') | ||
| try { fs.mkdirSync(t.context.exportDir) } catch (e) {} | ||
| localStorage.setItem('storages', JSON.stringify([t.context.storage.cache])) | ||
| }) | ||
|
|
||
| test.serial('Export a storage', t => { | ||
| const storageKey = t.context.storage.cache.key | ||
| const folders = t.context.storage.json.folders | ||
| const notes = t.context.storage.notes | ||
| const exportDir = t.context.exportDir | ||
| const folderKeyToName = folders.reduce( | ||
| (acc, folder) => { | ||
| acc[folder.key] = folder.name | ||
| return acc | ||
| }, {}) | ||
| return exportStorage(storageKey, 'md', exportDir) | ||
| .then(() => { | ||
| notes.forEach(note => { | ||
| const noteDir = path.join(exportDir, folderKeyToName[note.folder], `${note.title}.md`) | ||
| if (note.type === 'MARKDOWN_NOTE') { | ||
| t.true(fs.existsSync(noteDir)) | ||
| t.is(fs.readFileSync(noteDir, 'utf8'), note.content) | ||
| } else if (note.type === 'SNIPPET_NOTE') { | ||
| t.false(fs.existsSync(noteDir)) | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test.afterEach.always(t => { | ||
| localStorage.clear() | ||
| sander.rimrafSync(t.context.storageDir) | ||
| sander.rimrafSync(t.context.exportDir) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's check note contents too. Not just check the existence of the files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Rokt33r , done it!