Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions browser/main/lib/dataApi/createNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const resolveStorageData = require('./resolveStorageData')
const _ = require('lodash')
const keygen = require('browser/lib/keygen')
const path = require('path')
const CSON = require('@rokt33r/season')
const { writeNote } = require('./noteIO')
const { findStorage } = require('browser/lib/findStorage')

function validateInput (input) {
Expand Down Expand Up @@ -48,7 +48,7 @@ function createNote (storageKey, input) {

return resolveStorageData(targetStorage)
.then(function checkFolderExists (storage) {
if (_.find(storage.folders, {key: input.folder}) == null) {
if (_.find(storage.folders, { key: input.folder }) == null) {
throw new Error('Target folder doesn\'t exist.')
}
return storage
Expand All @@ -58,7 +58,7 @@ function createNote (storageKey, input) {
let isUnique = false
while (!isUnique) {
try {
sander.statSync(path.join(storage.path, 'notes', key + '.cson'))
sander.statSync(path.join(storage.path, 'notes', key + '.md'))
key = keygen(true)
} catch (err) {
if (err.code === 'ENOENT') {
Expand All @@ -68,6 +68,7 @@ function createNote (storageKey, input) {
}
}
}

const noteData = Object.assign({},
{
createdAt: new Date(),
Expand All @@ -79,7 +80,7 @@ function createNote (storageKey, input) {
storage: storageKey
})

CSON.writeFileSync(path.join(storage.path, 'notes', key + '.cson'), _.omit(noteData, ['key', 'storage']))
writeNote(path.join(storage.path, 'notes', key + '.md'), _.omit(noteData, ['key', 'storage']))

return noteData
})
Expand Down
4 changes: 2 additions & 2 deletions browser/main/lib/dataApi/deleteNote.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function deleteNote (storageKey, noteKey) {

return resolveStorageData(targetStorage)
.then(function deleteNoteFile (storage) {
const notePath = path.join(storage.path, 'notes', noteKey + '.cson')
const notePath = path.join(storage.path, 'notes', noteKey + '.md')

try {
sander.unlinkSync(notePath)
} catch (err) {
console.warn('Failed to delete note cson', err)
console.warn('Failed to delete note markdown', err)
}
return {
noteKey,
Expand Down
7 changes: 4 additions & 3 deletions browser/main/lib/dataApi/migrateFromV5Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const keygen = require('browser/lib/keygen')
const resolveStorageData = require('./resolveStorageData')
const consts = require('browser/lib/consts')
const CSON = require('@rokt33r/season')
const { writeNote } = require('./noteIO')
const path = require('path')
const sander = require('sander')

Expand All @@ -12,7 +13,7 @@ function migrateFromV5Storage (storageKey, data) {
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')

targetStorage = _.find(cachedStorageList, {key: storageKey})
targetStorage = _.find(cachedStorageList, { key: storageKey })
if (targetStorage == null) throw new Error('Target storage doesn\'t exist.')
} catch (e) {
return Promise.reject(e)
Expand Down Expand Up @@ -46,7 +47,7 @@ function importAll (storage, data) {
let isUnique = false
while (!isUnique) {
try {
sander.statSync(path.join(storage.path, 'notes', noteKey + '.cson'))
sander.statSync(path.join(storage.path, 'notes', noteKey + '.md'))
noteKey = keygen()
} catch (err) {
if (err.code === 'ENOENT') {
Expand Down Expand Up @@ -98,7 +99,7 @@ function importAll (storage, data) {
})

notes.forEach(function (note) {
CSON.writeFileSync(path.join(storage.path, 'notes', note.key + '.cson'), _.omit(note, ['storage', 'key']))
writeNote(path.join(storage.path, 'notes', note.key + '.md'), _.omit(note, ['storage', 'key']))
})

CSON.writeFileSync(path.join(storage.path, 'boostnote.json'), _.pick(storage, ['version', 'folders']))
Expand Down
4 changes: 2 additions & 2 deletions browser/main/lib/dataApi/migrateFromV6Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path')
const sander = require('sander')
const keygen = require('browser/lib/keygen')
const _ = require('lodash')
const CSON = require('@rokt33r/season')
const { writeNote } = require('./noteIO')

function migrateFromV5Storage (storagePath) {
var boostnoteJSONPath = path.join(storagePath, 'boostnote.json')
Expand Down Expand Up @@ -66,7 +66,7 @@ function migrateFromV5Storage (storagePath) {
const noteDirPath = path.join(storagePath, 'notes')
notes
.map(function saveNote (note) {
CSON.writeFileSync(path.join(noteDirPath, note.key) + '.cson', note)
writeNote(path.join(noteDirPath, note.key) + '.md', note)
})
return true
})
Expand Down
16 changes: 8 additions & 8 deletions browser/main/lib/dataApi/moveNote.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const resolveStorageData = require('./resolveStorageData')
const _ = require('lodash')
const path = require('path')
const CSON = require('@rokt33r/season')
const { readNote, writeNote } = require('./noteIO')
const keygen = require('browser/lib/keygen')
const sander = require('sander')
const { findStorage } = require('browser/lib/findStorage')
Expand All @@ -21,11 +21,11 @@ function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) {
.then(function saveNote (_oldStorage) {
oldStorage = _oldStorage
let noteData
const notePath = path.join(oldStorage.path, 'notes', noteKey + '.cson')
const notePath = path.join(oldStorage.path, 'notes', noteKey + '.md')
try {
noteData = CSON.readFileSync(notePath)
noteData = readNote(notePath)
} catch (err) {
console.warn('Failed to find note cson', err)
console.warn('Failed to find note markdown', err)
throw err
}
let newNoteKey
Expand All @@ -42,7 +42,7 @@ function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) {
let isUnique = false
while (!isUnique) {
try {
sander.statSync(path.join(newStorage.path, 'notes', newNoteKey + '.cson'))
sander.statSync(path.join(newStorage.path, 'notes', newNoteKey + '.md'))
newNoteKey = keygen(true)
} catch (err) {
if (err.code === 'ENOENT') {
Expand All @@ -57,7 +57,7 @@ function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) {
})
})
.then(function checkFolderExistsAndPrepareNoteData (newStorage) {
if (_.find(newStorage.folders, {key: newFolderKey}) == null) throw new Error('Target folder doesn\'t exist.')
if (_.find(newStorage.folders, { key: newFolderKey }) == null) throw new Error('Target folder doesn\'t exist.')

noteData.folder = newFolderKey
noteData.key = newNoteKey
Expand All @@ -76,13 +76,13 @@ function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) {
return noteData
})
.then(function writeAndReturn (noteData) {
CSON.writeFileSync(path.join(newStorage.path, 'notes', noteData.key + '.cson'), _.omit(noteData, ['key', 'storage', 'oldContent']))
writeNote(path.join(newStorage.path, 'notes', noteData.key + '.md'), _.omit(noteData, ['key', 'storage', 'oldContent']))
return noteData
})
.then(function deleteOldNote (data) {
if (storageKey !== newStorageKey) {
try {
sander.unlinkSync(path.join(oldStorage.path, 'notes', noteKey + '.cson'))
sander.unlinkSync(path.join(oldStorage.path, 'notes', noteKey + '.md'))
} catch (err) {
console.warn(err)
}
Expand Down
35 changes: 35 additions & 0 deletions browser/main/lib/dataApi/noteIO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const _ = require('lodash')
const fs = require('fs-plus')
const CSON = require('@rokt33r/season')

function createMarkdown (input) {
const content = input.content || ''
const meta = _.omit(input, ['key', 'storage', 'content'])
const result = CSON.stringify(meta)

return `<!--\n${result}\n-->${content ? `\n\n${content}` : ''}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not using the frontmattre format or at least make it configurable?

}

function readNote (path) {
const note = fs.readFileSync(path, { encoding: 'utf-8' })
const split = note.split('-->')
const metaString = (split[0] || '').replace('<!--', '').trim()
const content = (split[1] || '').trim()
try {
const result = CSON.parse(metaString)
result.content = content

return result
} catch (e) {
console.log(metaString)
throw e
}
}

function writeNote (path, input) {
const markdown = createMarkdown(input)
fs.writeFileSync(path, markdown, { encoding: 'utf-8' })
}

module.exports.readNote = readNote
module.exports.writeNote = writeNote
12 changes: 6 additions & 6 deletions browser/main/lib/dataApi/resolveStorageNotes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sander = require('sander')
const path = require('path')
const CSON = require('@rokt33r/season')
const { readNote } = require('./noteIO')

function resolveStorageNotes (storage) {
const notesDirPath = path.join(storage.path, 'notes')
Expand All @@ -17,13 +17,13 @@ function resolveStorageNotes (storage) {
notePathList = []
}
const notes = notePathList
.filter(function filterOnlyCSONFile (notePath) {
return /\.cson$/.test(notePath)
.filter(function filterOnlyMDFile (notePath) {
return /\.md$/.test(notePath)
})
.map(function parseCSONFile (notePath) {
.map(function parseMDFile (notePath) {
try {
const data = CSON.readFileSync(path.join(notesDirPath, notePath))
data.key = path.basename(notePath, '.cson')
const data = readNote(path.join(notesDirPath, notePath))
data.key = path.basename(notePath, '.md')
data.storage = storage.key
return data
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions browser/main/lib/dataApi/updateNote.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const resolveStorageData = require('./resolveStorageData')
const { writeNote, readNote } = require('./noteIO')
const _ = require('lodash')
const path = require('path')
const CSON = require('@rokt33r/season')
const { findStorage } = require('browser/lib/findStorage')

function validateInput (input) {
Expand Down Expand Up @@ -88,11 +88,11 @@ function updateNote (storageKey, noteKey, input) {
return resolveStorageData(targetStorage)
.then(function saveNote (storage) {
let noteData
const notePath = path.join(storage.path, 'notes', noteKey + '.cson')
const notePath = path.join(storage.path, 'notes', noteKey + '.md')
try {
noteData = CSON.readFileSync(notePath)
noteData = readNote(notePath)
} catch (err) {
console.warn('Failed to find note cson', err)
console.warn('Failed to find note markdown', err)
noteData = input.type === 'SNIPPET_NOTE'
? {
type: 'SNIPPET_NOTE',
Expand Down Expand Up @@ -130,7 +130,7 @@ function updateNote (storageKey, noteKey, input) {
storage: storageKey
})

CSON.writeFileSync(path.join(storage.path, 'notes', noteKey + '.cson'), _.omit(noteData, ['key', 'storage']))
writeNote(path.join(storage.path, 'notes', noteKey + '.md'), _.omit(noteData, ['key', 'storage']))

return noteData
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"flowchart.js": "^1.6.5",
"font-awesome": "^4.3.0",
"fs-extra": "^5.0.0",
"fs-plus": "^3.1.1",
"highlight.js": "^9.13.1",
"i18n-2": "^0.7.2",
"iconv-lite": "^0.4.19",
Expand Down
6 changes: 3 additions & 3 deletions tests/dataApi/createNote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const CSON = require('@rokt33r/season')
const { readNote } = require('../../browser/main/lib/dataApi/noteIO')
const faker = require('faker')

const storagePath = path.join(os.tmpdir(), 'test/create-note')
Expand Down Expand Up @@ -62,7 +62,7 @@ test.serial('Create a note', (t) => {
const data2 = data[1]

t.is(storageKey, data1.storage)
const jsonData1 = CSON.readFileSync(path.join(storagePath, 'notes', data1.key + '.cson'))
const jsonData1 = readNote(path.join(storagePath, 'notes', data1.key + '.md'))

t.is(input1.title, data1.title)
t.is(input1.title, jsonData1.title)
Expand All @@ -80,7 +80,7 @@ test.serial('Create a note', (t) => {
t.deepEqual(input1.snippets[0].linesHighlighted, jsonData1.snippets[0].linesHighlighted)

t.is(storageKey, data2.storage)
const jsonData2 = CSON.readFileSync(path.join(storagePath, 'notes', data2.key + '.cson'))
const jsonData2 = readNote(path.join(storagePath, 'notes', data2.key + '.md'))
t.is(input2.title, data2.title)
t.is(input2.title, jsonData2.title)
t.is(input2.content, data2.content)
Expand Down
4 changes: 2 additions & 2 deletions tests/dataApi/createNoteFromUrl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const CSON = require('@rokt33r/season')
const { readNote } = require('../../browser/main/lib/dataApi/noteIO')

const storagePath = path.join(os.tmpdir(), 'test/create-note-from-url')

Expand All @@ -29,7 +29,7 @@ test.serial('Create a note from URL', (t) => {
return createNoteFromUrl(url, storageKey, folderKey)
.then(function assert ({ note }) {
t.is(storageKey, note.storage)
const jsonData = CSON.readFileSync(path.join(storagePath, 'notes', note.key + '.cson'))
const jsonData = readNote(path.join(storagePath, 'notes', note.key + '.md'))

// Test if saved content is matching the created in memory note
t.is(note.content, jsonData.content)
Expand Down
6 changes: 3 additions & 3 deletions tests/dataApi/deleteNote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const CSON = require('@rokt33r/season')
const { readNote } = require('../../browser/main/lib/dataApi/noteIO')
const faker = require('faker')
const fs = require('fs')
const attachmentManagement = require('browser/main/lib/dataApi/attachmentManagement')
Expand Down Expand Up @@ -55,8 +55,8 @@ test.serial('Delete a note', (t) => {
})
.then(function assert (data) {
try {
CSON.readFileSync(path.join(storagePath, 'notes', data.noteKey + '.cson'))
t.fail('note cson must be deleted.')
readNote(path.join(storagePath, 'notes', data.noteKey + '.md'))
t.fail('note markdown must be deleted.')
} catch (err) {
t.is(err.code, 'ENOENT')
return data
Expand Down
6 changes: 3 additions & 3 deletions tests/dataApi/migrateFromV6Storage-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const localStorage = window.localStorage = global.localStorage = new Storage(nul
const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const CSON = require('@rokt33r/season')
const { readNote } = require('../../browser/main/lib/dataApi/noteIO')
const _ = require('lodash')
const os = require('os')

Expand Down Expand Up @@ -38,11 +38,11 @@ test.serial('Migrate legacy storage into v1 storage', (t) => {
t.is(dummyData.notes.length, fileList.length)
const noteMap = fileList
.map((filePath) => {
return CSON.readFileSync(path.join(noteDirPath, filePath))
return readNote(path.join(noteDirPath, filePath))
})
dummyData.notes
.forEach(function (targetNote) {
t.true(_.find(noteMap, {title: targetNote.title, folder: targetNote.folder}) != null)
t.true(_.find(noteMap, { title: targetNote.title, folder: targetNote.folder }) != null)
})

// Check legacy folder directory is removed
Expand Down
8 changes: 4 additions & 4 deletions tests/dataApi/moveNote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const TestDummy = require('../fixtures/TestDummy')
const sander = require('sander')
const os = require('os')
const CSON = require('@rokt33r/season')
const { readNote } = require('../../browser/main/lib/dataApi/noteIO')

const storagePath = path.join(os.tmpdir(), 'test/move-note')
const storagePath2 = path.join(os.tmpdir(), 'test/move-note2')
Expand Down Expand Up @@ -41,16 +41,16 @@ test.serial('Move a note', (t) => {
const data1 = data[0]
const data2 = data[1]

const jsonData1 = CSON.readFileSync(path.join(storagePath, 'notes', data1.key + '.cson'))
const jsonData1 = readNote(path.join(storagePath, 'notes', data1.key + '.md'))

t.is(jsonData1.folder, folderKey1)
t.is(jsonData1.title, note1.title)

const jsonData2 = CSON.readFileSync(path.join(storagePath2, 'notes', data2.key + '.cson'))
const jsonData2 = readNote(path.join(storagePath2, 'notes', data2.key + '.md'))
t.is(jsonData2.folder, folderKey2)
t.is(jsonData2.title, note2.title)
try {
CSON.readFileSync(path.join(storagePath, 'notes', note2.key + '.cson'))
readNote(path.join(storagePath, 'notes', note2.key + '.md'))
t.fail('The old note should be deleted.')
} catch (err) {
t.is(err.code, 'ENOENT')
Expand Down
Loading