Skip to content

Commit

Permalink
feat: update findByLink to return lookup meta
Browse files Browse the repository at this point in the history
  • Loading branch information
carbontwelve committed May 4, 2024
1 parent 10ea08e commit 477e212
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
40 changes: 25 additions & 15 deletions src/find-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
14 changes: 9 additions & 5 deletions tests/find-page-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?

0 comments on commit 477e212

Please sign in to comment.