feat(filters): move permalink function into file, add test

# Conflicts:
#	.eleventy.js
This commit is contained in:
Oscar 2020-02-16 23:27:17 +01:00
parent d2f7547d2d
commit aaf3ea3b73
3 changed files with 14 additions and 4 deletions

View File

@ -1,15 +1,15 @@
const makeItemLink = (slug) => `#${slug}`;
const findExistingDefinition = require('./11ty/filters/helpers/findExistingDefinition'); const findExistingDefinition = require('./11ty/filters/helpers/findExistingDefinition');
const definitionPermalink = require('./11ty/filters/definitionPermalink');
module.exports = function(config) { module.exports = function(config) {
// Add a filter using the Config API // Add a filter using the Config API
config.addFilter('linkTarget', makeItemLink); config.addFilter('linkTarget', definitionPermalink);
config.addFilter('linkIfExistsInCollection', (word, collection) => { config.addFilter('linkIfExistsInCollection', (word, collection) => {
const existingDefinition = findExistingDefinition(word, collection); const existingDefinition = findExistingDefinition(word, collection);
if (existingDefinition) { if (existingDefinition) {
return `<a href="${makeItemLink( return `<a href="${definitionPermalink(
existingDefinition.data.slug existingDefinition.data.slug
)}">${word}</a>`; )}">${word}</a>`;
} }
@ -24,7 +24,7 @@ module.exports = function(config) {
); );
if (existingDefinition) { if (existingDefinition) {
return `<a href="${makeItemLink( return `<a href="${definitionPermalink(
existingDefinition.data.slug existingDefinition.data.slug
)}" aria-label="${subTermData.full_title}">${subTermData.text}</a>`; )}" aria-label="${subTermData.full_title}">${subTermData.text}</a>`;
} }

View File

@ -0,0 +1,7 @@
import test from 'ava';
import definitionPermalink from '../definitionPermalink';
test('constructs correct detail link', (t) => {
t.is(definitionPermalink('test-slug'), '/definitions/test-slug/');
});

View File

@ -0,0 +1,3 @@
module.exports = function(slug) {
return `/definitions/${slug}/`;
};