Skip to content

Commit

Permalink
Ignore backlinks to daily notes (#32)
Browse files Browse the repository at this point in the history
* fixing the daily backlinks

* fixing the test

* Update src/helpers/to-id.ts

Co-authored-by: ocavue <[email protected]>

* Update src/convertors/logseq/logseq-convertor.ts

Co-authored-by: ocavue <[email protected]>

---------

Co-authored-by: ocavue <[email protected]>
  • Loading branch information
vojto and ocavue authored Nov 23, 2023
1 parent bb9f6a1 commit e092e4e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,7 @@ exports[`LogseqConvertor > parses example 1`] = `
"subject": "Contents",
},
{
"backlinks": [
{
"id": "02052023",
"label": "May 2nd, 2023",
},
{
"id": "04052023",
"label": "May 4th, 2023",
},
],
"backlinks": [],
"dailyAt": 1683072000000,
"html": "<h1>May 3rd, 2023</h1><ul><li><p>This is a test graph</p></li><li><p><a class=\\"backlink new\\" href=\\"https://reflect.app/g/123/02052023\\">May 2nd, 2023</a></p><ul><li><p>This is some notes for yesterday</p></li></ul></li><li><p><a class=\\"backlink new\\" href=\\"https://reflect.app/g/123/04052023\\">May 4th, 2023</a></p><ul><li><p>This is some notes for tomorrow</p></li></ul></li></ul>",
"id": "03052023",
Expand Down
18 changes: 18 additions & 0 deletions src/convertors/logseq/logseq-convertor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('LogseqConvertor', () => {

expect(notes).toMatchSnapshot()
})

it('parses properties example', async () => {
const convertor = new LogseqConvertor({graphId: '123'})
const {notes} = await convertor.convert({
Expand All @@ -22,12 +23,14 @@ describe('LogseqConvertor', () => {

expect(notes).toMatchSnapshot()
})

it('parses TODO example', async () => {
const convertor = new LogseqConvertor({graphId: '123'})
const {notes} = await convertor.convert({data: JSON.stringify(exampleTodoExport)})

expect(notes).toMatchSnapshot()
})

it('handles and ignores whiteboard pages', async () => {
const convertor = new LogseqConvertor({graphId: '123'})
const {notes} = await convertor.convert({
Expand All @@ -36,10 +39,25 @@ describe('LogseqConvertor', () => {

expect(notes).toMatchSnapshot()
})

it('exception for Org example', () => {
const convertor = new LogseqConvertor({graphId: '123'})
expect(
async () => await convertor.convert({data: JSON.stringify(exampleOrgExport)}),
).rejects.toThrowError()
})

it('does not include daily backlinks', async () => {
const data = {
...exampleExport,
blocks: exampleExport.blocks.filter((b) => b['page-name'] === 'May 3rd, 2023'),
}

const convertor = new LogseqConvertor({graphId: '123'})
const {notes} = await convertor.convert({data: JSON.stringify(data)})
expect(notes.length).toBe(1)
const note = notes[0]

expect(note.backlinks).toEqual([])
})
})
9 changes: 9 additions & 0 deletions src/convertors/logseq/logseq-convertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {exportSchema} from './schema'
import {LogseqBlock, LogseqConversionError, LogseqExport, LogseqNote} from './types'
import {Convertor} from '../../convertor'
import {Backlink, ConvertedNote, ConvertOptions, ConvertResponse} from '../../types'

Check warning on line 11 in src/convertors/logseq/logseq-convertor.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

There should be at least one empty line between import groups
import {isDailyNoteId} from 'helpers/to-id'

Check warning on line 12 in src/convertors/logseq/logseq-convertor.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

`helpers/to-id` import should occur before import of `helpers/validate`

export class LogseqConvertor extends Convertor {
static accept = {'application/json': ['.json']}
Expand Down Expand Up @@ -120,6 +121,14 @@ export class LogseqConvertor extends Convertor {
pageResolver: (pageName) => toLogseqId(this.noteIds[pageName], pageName),
})

// Remove backlinks to daily notes. Reflect will automatically create the
// daily note when daily backlink is clicked. Also, Reflect importing logic
// doesn't recognize daily backlinks, so this would create regular note with
// daily-like ID, which would break the UI.
//
// See also: https://height.app/dWwdXWnlP/T-2442
backlinks = backlinks.filter((backlink) => !isDailyNoteId(backlink.id))

// If the block has children then we need to get the html for the children
if (block.children?.length) {
const {html: childHtml, backlinks: childBacklinks} = this.parseBlocks(
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/to-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export const toNoteId = (value: string) => {
export const toDailyNoteId = (date: Date) => {
return format(date, 'ddMMyyyy')
}

export const isDailyNoteId = (value: string) => {
return (/^(\d{2})(\d{2})(\d{4})$/).test(value)

Check warning on line 17 in src/helpers/to-id.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

Replace `(/^(\d{2})(\d{2})(\d{4})$/)` with `/^(\d{2})(\d{2})(\d{4})$/`
}

0 comments on commit e092e4e

Please sign in to comment.