diff --git a/index.d.ts b/index.d.ts index 9baa6a0..385a80f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -53,7 +53,7 @@ type WikilinkMeta = { } interface PageDirectoryService { - findByLink(link : WikilinkMeta|LinkMeta): any; + findByLink(link : WikilinkMeta|LinkMeta): {page: any, found: boolean, foundByAlias: boolean}; findByFile(file : any): any; } diff --git a/src/find-page.js b/src/find-page.js index 516f1e0..60fa9bc 100644 --- a/src/find-page.js +++ b/src/find-page.js @@ -8,26 +8,36 @@ */ const pageLookup = (allPages = [], slugifyFn) => { return { - findByLink: (link) => allPages.find((page) => { - if (link.href && (page.url === link.href || page.url === `${link.href}/`)) { - return true; - } + findByLink: (link) => { + let foundByAlias = false; + const page = allPages.find((page) => { + if (link.href && (page.url === link.href || page.url === `${link.href}/`)) { + return true; + } - // TODO: is there a need to slug the page title for comparison? We can match on link.name === page.data.title! + // TODO: is there a need to slug the page title for comparison? We can match on link.name === page.data.title! - if (page.fileSlug === link.slug || (page.data.title && slugifyFn(page.data.title) === link.slug)) { - return true; - } + if (page.fileSlug === link.slug || (page.data.title && slugifyFn(page.data.title) === link.slug)) { + return true; + } - // TODO: need a way to identify that wikilink is pointing to an alias, because the alias then becomes the link title + // TODO: need a way to identify that wikilink is pointing to an alias, because the alias then becomes the link title - const aliases = ((page.data.aliases && Array.isArray(page.data.aliases)) ? page.data.aliases : []).reduce(function (set, alias) { - set.add(slugifyFn(alias)); - return set; - }, new Set()); + const aliases = ((page.data.aliases && Array.isArray(page.data.aliases)) ? page.data.aliases : []).reduce(function (set, alias) { + set.add(alias); + return set; + }, new Set()); - return aliases.has(link.slug); - }), + foundByAlias = aliases.has(link.name); + return foundByAlias; + }); + + return { + found: !!page, + page, + foundByAlias, + } + }, findByFile: (file) => allPages.find((page) => page.url === file.page.url), } diff --git a/tests/find-page-service.test.js b/tests/find-page-service.test.js index 54115da..7f2aedc 100644 --- a/tests/find-page-service.test.js +++ b/tests/find-page-service.test.js @@ -30,29 +30,33 @@ const pageDirectory = pageLookup([ ], slugify); test('pageLookup (find by href)', t => { - t.is(pageDirectory.findByLink({href: '/something/else', isEmbed: false}).fileSlug, 'something-else'); + const {page} = pageDirectory.findByLink({href: '/something/else', isEmbed: false}); + t.is(page.fileSlug, 'something-else'); }); test('pageLookup (find by wikilink)', t => { - t.is(pageDirectory.findByLink({ + const {page} = pageDirectory.findByLink({ title: 'Hello World, Title', name: 'Hello World, Title', anchor: null, link: '[[Hello World, Title]]', slug: 'hello-world', isEmbed: false, - }).fileSlug, 'hello-world'); + }); + + t.is(page.fileSlug, 'hello-world'); }); test('pageLookup (find by alias)', t => { - t.is(pageDirectory.findByLink({ + const {page} = pageDirectory.findByLink({ title: 'This is another page', name: 'test-alias', anchor: null, link: '[[test-alias]]', slug: 'test-alias', isEmbed: false, - }).fileSlug, 'something-else'); + }); + t.is(page.fileSlug, 'something-else'); }); // TODO: add testing when two pages share the same alias, what _should_ happen ?