forked from fluid-project/eleventy-plugin-fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-permalink.js
49 lines (41 loc) · 2.45 KB
/
generate-permalink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
const TemplateConfig = require("@11ty/eleventy/src/TemplateConfig.js");
/**
* @param {Object} data - The data object for the current collection item.
* @param {String} collectionType - The collection type.
* @param {String} collectionSlug - A localized, URL-safe slug for the collection type, used in the generated permalink.
* @param {String} paginationSlug - A localized, URL-safe slug for paginated URLs such as /page/2, used in the generated permalink.
*
* @return {String} - The generated permalink.
*/
const generatePermalink = (data, collectionType, collectionSlug, paginationSlug = "page") => {
/* If this post is a "stub" with no localized title, we assume it does not exist and prevent it from building. */
if (!data.hasOwnProperty("title")) {
return false;
}
const eleventyConfig = new TemplateConfig();
const locale = data.locale || data.defaultLanguage;
const langSlug = data.supportedLanguages[locale].slug || locale;
collectionSlug = collectionSlug || collectionType;
const slugify = eleventyConfig.userConfig.getFilter("slugify");
if (collectionType === "pages") {
/* If the page is a 404 page, return 404.html, optionally prepended with the language code. */
if (data.page.fileSlug === "404") {
return (locale === data.defaultLanguage) ? "/404.html" : `/${langSlug}/404.html`;
}
/** If the page is the index page, the base path, optionally prepended with the language code. */
if (data.page.fileSlug === locale || data.page.inputPath.endsWith("index.md")) {
return (locale === data.defaultLanguage) ? "/" : `/${langSlug}/`;
}
/* If the page is not the index page, return the page title in a URL-safe format, optionally prepended with the language code. */
const slug = data.slug || slugify(data.title);
if (data.hasOwnProperty("pagination") && data.pagination.pageNumber > 0) {
return (locale === data.defaultLanguage) ? `/${slug}/${paginationSlug}/${data.pagination.pageNumber + 1}/` : `/${langSlug}/${slug}/${paginationSlug}/${data.pagination.pageNumber + 1}/`;
}
return (locale === data.defaultLanguage) ? `/${slug}/` : `/${langSlug}/${slug}/`;
} else {
const slug = data.slug || slugify(data.title);
return (locale === data.defaultLanguage) ? `/${collectionSlug}/${slug}/` : `/${langSlug}/${collectionSlug}/${slug}/`;
}
};
module.exports = generatePermalink;