diff --git a/src/convertors/markdown/markdown-convertor.test.ts b/src/convertors/markdown/markdown-convertor.test.ts index 6c6df47..507b933 100644 --- a/src/convertors/markdown/markdown-convertor.test.ts +++ b/src/convertors/markdown/markdown-convertor.test.ts @@ -39,6 +39,29 @@ describe('id', () => { }) const [{id}] = notes - expect(id).toEqual('alexmaccaw') + expect(id).toEqual('md-alex-maccaw') + }) +}) + +describe('backlinks', () => { + it('creates backlinks', async () => { + const data = ` +# Alex MacCaw + +- line [[With Backlink]] +` + const convertor = new MarkdownConvertor({graphId: '123'}) + const {notes} = await convertor.convert({ + data, + filename: 'Alex MacCaw.md', + }) + const [{backlinks}] = notes + + expect(backlinks).toMatchObject([ + { + id: 'md-with-backlink', + label: 'With Backlink', + }, + ]) }) }) diff --git a/src/convertors/markdown/markdown-helpers.ts b/src/convertors/markdown/markdown-helpers.ts index 80a262b..5383bc8 100644 --- a/src/convertors/markdown/markdown-helpers.ts +++ b/src/convertors/markdown/markdown-helpers.ts @@ -1,6 +1,6 @@ -import slugify from '@sindresorhus/slugify' import {parse, isValid} from 'date-fns' +import {toMarkdownNoteId} from 'helpers/markdown/markdown-helpers' import {stripFileExtension} from 'helpers/path' export const dailyDateFromFilename = (filename: string): Date | undefined => { @@ -16,8 +16,7 @@ export const dailyDateFromFilename = (filename: string): Date | undefined => { export const filenameToId = (filename: string) => { const basename = stripFileExtension(filename) - - return `${slugify(basename, {lowercase: true, decamelize: false, separator: ''})}` + return toMarkdownNoteId(basename) } export const filenameToSubject = (filename: string) => { diff --git a/src/helpers/markdown/markdown-helpers.ts b/src/helpers/markdown/markdown-helpers.ts new file mode 100644 index 0000000..3d69c25 --- /dev/null +++ b/src/helpers/markdown/markdown-helpers.ts @@ -0,0 +1,8 @@ +import slugify from '@sindresorhus/slugify' + +export function toMarkdownNoteId(value: string) { + const prefix = value.startsWith('md-') ? '' : 'md-' + const slug = slugify(value, {lowercase: true, decamelize: false, separator: '-'}) + + return `${prefix}${slug}` +} diff --git a/src/helpers/markdown/markdown.test.ts b/src/helpers/markdown/markdown.test.ts index 8b55b7e..b0e4174 100644 --- a/src/helpers/markdown/markdown.test.ts +++ b/src/helpers/markdown/markdown.test.ts @@ -25,7 +25,7 @@ This is a test " `) }) @@ -60,11 +60,11 @@ This is a test expect(backlinks).toEqual([ { - id: 'mybacklink', + id: 'md-my-backlink', label: 'my backlink', }, { - id: 'anotherbacklink', + id: 'md-another-backlink', label: 'another backlink', }, ]) @@ -72,8 +72,8 @@ This is a test // Backlinks should be converted to links expect(html).toMatchInlineSnapshot(` "

Hello World

-

my backlink - another backlink

" +

my backlink + another backlink

" `) }) diff --git a/src/helpers/markdown/markdown.ts b/src/helpers/markdown/markdown.ts index 3f4d1ae..41645d1 100644 --- a/src/helpers/markdown/markdown.ts +++ b/src/helpers/markdown/markdown.ts @@ -7,8 +7,8 @@ import wikiLinkPlugin from 'remark-wiki-link' import {unified} from 'unified' import {buildBacklinkUrl} from 'helpers/backlink' -import {toNoteId} from 'helpers/to-id' +import {toMarkdownNoteId} from './markdown-helpers' import {hydrateBacklinks} from './plugins/hydrate-backlink-note-ids' import {hydrateSubject} from './plugins/hydrate-subject' import {parseTags} from './plugins/parse-tags' @@ -23,7 +23,12 @@ export const markdownToHtml = ( pageResolver?: (pageName: string) => string }, ) => { - const {constructsToDisable = [], graphId, linkHost, pageResolver = toNoteId} = options + const { + constructsToDisable = [], + graphId, + linkHost, + pageResolver = toMarkdownNoteId, + } = options const processor = unified() .data('micromarkExtensions', [