Skip to content

Commit

Permalink
Merge pull request #63 from photogabble/patch/v1.1.0/#62
Browse files Browse the repository at this point in the history
FEATURE: Accept either string or array as page alias
  • Loading branch information
carbontwelve authored Nov 6, 2024
2 parents b092297 + e9aed09 commit 4417e12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ You can link to pages by their project path, or a path relative to the linking p

Aliases provide you a way of referencing a file using different names, use the `aliases` property in your font matter to list one or more aliases that can be used to reference the file from a Wiki Link. For example, you might add _AI_ as an alias of a file titled _Artificial Intelligence_ which would then be linkable via `[[AI]]`.

These can be defined as either an array as shown below or a single alias via `aliaes: AI`.

```yaml
---
title: Artificial Intelligence
Expand Down
5 changes: 4 additions & 1 deletion src/find-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const pageLookup = (allPages = []) => {
return true;
}

const aliases = ((page.data.aliases && Array.isArray(page.data.aliases)) ? page.data.aliases : []).reduce(function (set, alias) {
const aliases = ((page.data.aliases && Array.isArray(page.data.aliases))
? page.data.aliases
: (typeof page.data.aliases === 'string' ? [page.data.aliases] : [])
).reduce(function (set, alias) {
set.add(alias);
return set;
}, new Set());
Expand Down
21 changes: 21 additions & 0 deletions tests/find-page-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const pageDirectory = pageLookup([
aliases: ['test-alias']
},
url: '/something/else/'
},
{
fileSlug: 'string-lookup-test',
data: {
title: 'This is another page',
aliases: 'test-alias-string'
},
url: '/something/else/'
}
]);

Expand Down Expand Up @@ -58,4 +66,17 @@ test('pageLookup (find by alias)', t => {
t.is(page.fileSlug, 'something-else');
});

test('pageLookup (find by alias as string)', t => {
const {page} = pageDirectory.findByLink({
title: 'This is another page',
name: 'test-alias-string',
anchor: null,
link: '[[test-alias-string]]',
slug: 'test-alias-string',
isEmbed: false,
});
t.is(typeof page, 'object');
t.is(page.fileSlug, 'string-lookup-test');
});

// TODO: add testing when two pages share the same alias, what _should_ happen ?

0 comments on commit 4417e12

Please sign in to comment.