Skip to content

Commit

Permalink
binds roamJsonToBlocks and validateRoam to importText
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolarora1 committed Nov 6, 2020
1 parent ddafbfd commit 0679694
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/action-creators/importText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import he from 'he'
import { parentOf, convertHTMLtoJSON, head, importJSON, pathToContext, rootedParentOf, strip } from '../util'
import { simplifyPath } from '../selectors'
import { ActionCreator, Path, Timestamp } from '../types'
import { validateRoam } from '../util/validateRoam'
import { roamJsonToBlocks } from '../util/roamJsonToBlocks'

// declare types until jex-block-parser merges PR
// https://github.com/reergymerej/block-parser/pull/1
Expand Down Expand Up @@ -121,8 +123,10 @@ const importText = (path: Path, inputText: string, { preventSetCursor, preventSy

const state = getState()

const isRoam = validateRoam(inputText)

const simplePath = simplifyPath(state, path)
const text = rawTextToHtml(inputText)
const text = isRoam ? inputText : rawTextToHtml(inputText)
const numLines = (text.match(regexpListItem) || []).length
const destThought = head(path)
const destValue = rawDestValue || destThought.value
Expand Down Expand Up @@ -168,7 +172,7 @@ const importText = (path: Path, inputText: string, { preventSetCursor, preventSy
})
}
else {
const json = convertHTMLtoJSON(text)
const json = isRoam ? roamJsonToBlocks(JSON.parse(text)) : convertHTMLtoJSON(text)
const { lastThoughtFirstLevel, thoughtIndexUpdates, contextIndexUpdates } = importJSON(state, simplePath, json, { skipRoot })
if (!preventSync) {
dispatch({
Expand Down
94 changes: 94 additions & 0 deletions src/util/__tests__/importText.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,97 @@ it('two root thoughts', async () => {
expect(exported.trim())
.toBe(text)
})

it('imports Roam json', async () => {
const roamString = JSON.stringify([
{
title: 'Fruits',
children: [
{
string: 'Apple',
'create-email': '[email protected]',
'edit-email': '[email protected]',
'create-time': 1600111381583,
'edit-time': 1600111381580,
uid: 'UK11200',
},
{
string: 'Orange',
'create-email': '[email protected]',
'edit-email': '[email protected]',
'create-time': 1600111383054,
'edit-time': 1600111383050,
uid: 'UK11233',
},
{
string: 'Banana',
'create-email': '[email protected]',
'edit-email': '[email protected]',
'create-time': 1600111383911,
'edit-time': 1600111383910,
uid: 'HMN_YQtZZ',
}
],
},
{
title: 'Veggies',
children: [
{
string: 'Broccoli',
'create-email': '[email protected]',
'edit-email': '[email protected]',
'create-time': 1600111381600,
'edit-time': 1600111381599,
uid: 'BK11200',
},
{
string: 'Spinach',
'create-email': '[email protected]',
'edit-email': '[email protected]',
'create-time': 1600111389054,
'edit-time': 1600111389050,
uid: 'BK11233',
}
],
}
])
// /**
// * Parses Roam and generates { contextIndexUpdates, thoughtIndexUpdates } that can be sync'd to state.
// */
// export const importRoam = (state: State, simplePath: SimplePath, roam: RoamPage[]) => {
// const thoughtsJSON = roamJsonToBlocks(roam)
// return importJSON(state, simplePath, thoughtsJSON, { skipRoot: false })
// }

const exported = await importExport(roamString)
expect(exported)
.toBe(`
- Fruits
- Apple
- =create-email
- [email protected]
- =edit-email
- [email protected]
- Orange
- =create-email
- [email protected]
- =edit-email
- [email protected]
- Banana
- =create-email
- [email protected]
- =edit-email
- [email protected]
- Veggies
- Broccoli
- =create-email
- [email protected]
- =edit-email
- [email protected]
- Spinach
- =create-email
- [email protected]
- =edit-email
- [email protected]
`)
})

0 comments on commit 0679694

Please sign in to comment.