Skip to content

Basic importRoam #888

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 12 commits into from
Nov 6, 2020
17 changes: 17 additions & 0 deletions src/@types/ROAM/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare module 'roam' {
interface RoamBlock {
uid: string,
string: string,
'create-email': string,
'create-time': number,
children?: RoamBlock[],
'edit-time'?: number,
'edit-email'?: string,
}

interface RoamPage {
title: string,
children: RoamBlock[],
}

}
12 changes: 9 additions & 3 deletions src/action-creators/importText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { parse } from 'jex-block-parser'
import he from 'he'
import { parentOf, convertHTMLtoJSON, head, importJSON, pathToContext, rootedParentOf, strip } from '../util'
import { simplifyPath } from '../selectors'
import { ActionCreator, Path } from '../types'
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
export interface Block {
scope: string,
created?: Timestamp,
lastUpdated?: Timestamp,
children: Block[],
}

Expand Down Expand Up @@ -119,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 @@ -166,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
8 changes: 8 additions & 0 deletions src/test-helpers/removeRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Remove root, de-indent (trim), and append newline to make tests cleaner.
*/
export const removeRoot = exported => exported.slice(exported.indexOf('\n'))
.split('\n')
.map(line => line.slice(2))
.join('\n')
+ '\n'
9 changes: 2 additions & 7 deletions src/util/__tests__/importHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
exportContext,
} from '../../selectors'
import { removeRoot } from '../../test-helpers/removeRoot'

const RANKED_ROOT = [{ value: ROOT_TOKEN, rank: 0 }]
const initialState = {
Expand Down Expand Up @@ -50,13 +51,7 @@ const importExport = html => {
const exported = exportContext(state, [ROOT_TOKEN], 'text/plaintext')

// remove root, de-indent (trim), and append newline to make tests cleaner
const exportedWithoutRoot = exported.slice(exported.indexOf('\n'))
.split('\n')
.map(line => line.slice(2))
.join('\n')
+ '\n'

return exportedWithoutRoot
return removeRoot(exported)
}

it('simple', () => {
Expand Down
103 changes: 96 additions & 7 deletions src/util/__tests__/importText.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
importText,
} from '../../action-creators'
import { removeRoot } from '../../test-helpers/removeRoot'

const RANKED_ROOT = [{ value: ROOT_TOKEN, rank: 0 }]
const initialState = {
Expand Down Expand Up @@ -57,13 +58,7 @@ const importExport = async text => {
const exported = exportContext(state, [ROOT_TOKEN], 'text/plaintext')

// remove root, de-indent (trim), and append newline to make tests cleaner
const exportedWithoutRoot = exported.slice(exported.indexOf('\n'))
.split('\n')
.map(line => line.slice(2))
.join('\n')
+ '\n'

return exportedWithoutRoot
return removeRoot(exported)
}

it('initialSettings', async () => {
Expand Down Expand Up @@ -128,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 })
// }
Comment on lines +180 to +186
Copy link
Contributor

Choose a reason for hiding this comment

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

Old code should be removed


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]
`)
})
Loading