Skip to content

Commit

Permalink
Merge pull request #42 from photogabble/patch/v1.1.0/#41
Browse files Browse the repository at this point in the history
BUGFIX: Use alias as link text if it's the lookup source
  • Loading branch information
carbontwelve authored May 4, 2024
2 parents 608c0a1 + 787a379 commit 24175a6
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Bugfix use alias as link text if it's the lookup source (#42)
- Bugfix HTML encode link titles (#40)
- Bugfix broken dead-links lookup due to typo (#38)
- Bugfix do not render embeds if the page linked doesn't exist (#35)
Expand Down
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
2 changes: 1 addition & 1 deletion src/html-link-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = class HTMLLinkParser {
isEmbed: false,
};

if (!pageDirectory.findByLink(meta)) {
if (!pageDirectory.findByLink(meta).found) {
this.deadLinks.add(link);
}

Expand Down
4 changes: 2 additions & 2 deletions src/interlinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ module.exports = class Interlinker {
...this.HTMLLinkParser.find(pageContent, pageDirectory),
].map((link) => {
// Lookup the page this link, links to and add this page to its backlinks
const page = pageDirectory.findByLink(link);
if (!page) return link;
const {page, found} = pageDirectory.findByLink(link);
if (!found) return link;

if (!page.data.backlinks) {
page.data.backlinks = [];
Expand Down
8 changes: 6 additions & 2 deletions src/wikilink-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ module.exports = class WikilinkParser {
}

// Lookup page data from 11ty's collection to obtain url and title if currently null
const page = pageDirectory.findByLink(meta);
const {page, foundByAlias} = pageDirectory.findByLink(meta);
if (page) {
if (meta.title === null && page.data.title) meta.title = page.data.title;
if (foundByAlias) {
meta.title = meta.name;
} else if (meta.title === null && page.data.title) {
meta.title = page.data.title;
}
meta.href = page.url;
meta.path = page.inputPath;
meta.exists = true;
Expand Down
15 changes: 14 additions & 1 deletion tests/eleventy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test("Sample small Website (wikilinks and regular links)", async t => {

let results = await elev.toJSON();

t.is(results.length, 4);
t.is(results.length, 5);

t.is(
normalize(findResultByUrl(results, '/about/').content),
Expand All @@ -41,6 +41,19 @@ test("Sample small Website (htmlenteties)", async t => {
);
});

test("Sample small Website (alias text used for link)", async t => {
let elev = new Eleventy(fixturePath('sample-small-website'), fixturePath('sample-small-website/_site'), {
configPath: fixturePath('sample-small-website/eleventy.config.js'),
});

let results = await elev.toJSON();

t.is(
normalize(findResultByUrl(results, '/aliased-link-to-lonelyjuly/').content),
`<div><p>This should link with the alias as text <a href="/lonelyjuly/">Aliased WikiLink</a>.</p></div><div></div>`
);
});

test.serial("Broken page (wikilinks and regular links)", async t => {
const mock = sinon.stub(console, 'warn');

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 ?
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Aliased Link to LonelyJuly
layout: default.liquid
---

This should link with the alias as text [[Aliased WikiLink]].
1 change: 1 addition & 0 deletions tests/fixtures/sample-small-website/lonelyjuly.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: '>>LONELYJULY<<'
aliases:
- "LonelyJuly"
- "Aliased WikiLink"
---

Lonely July Page

0 comments on commit 24175a6

Please sign in to comment.