Skip to content

Commit

Permalink
Some internal filter simplification from #2250
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 14, 2022
1 parent a0b6928 commit 44a48cb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Filters/GetCollectionItemIndex.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO locale-friendly, see GetLocaleCollectionItem.js)
module.exports = function getCollectionItemIndex(collection, page) {
if (!page) {
page = this.page || this.ctx?.page || this.context?.environments?.page;
page = this.page;
}

let j = 0;
Expand Down
11 changes: 7 additions & 4 deletions src/Filters/GetLocaleCollectionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ function getLocaleCollectionItem(
langCode,
indexModifier = 0
) {
let page = this.page || this.ctx?.page || this.context?.environments?.page;
if (!langCode) {
// if page.lang exists (2.0.0-canary.14 and i18n plugin added, use page language)
if (page.lang) {
langCode = page.lang;
if (this.page.lang) {
langCode = this.page.lang;
} else {
return getCollectionItem(collection, pageOverride || page, indexModifier);
return getCollectionItem(
collection,
pageOverride || this.page,
indexModifier
);
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/Plugins/HtmlBasePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ module.exports = function (eleventyConfig, defaultOptions = {}) {

return transformUrl.call(this, url, base, {
pathPrefix: eleventyConfig.pathPrefix,
pageUrl:
pageUrlOverride ||
this.page?.url ||
this.ctx?.page?.url ||
this.context?.environments?.page?.url,
pageUrl: pageUrlOverride || this.page?.url,
});
}
);
Expand All @@ -126,14 +122,10 @@ module.exports = function (eleventyConfig, defaultOptions = {}) {
return content;
}

let fallbackPageUrl =
this.page?.url ||
this.ctx?.page?.url ||
this.context?.environments?.page?.url;
return addToAllHtmlUrls(content, (url) => {
return transformUrl.call(this, url.trim(), base, {
pathPrefix: eleventyConfig.pathPrefix,
pageUrl: pageUrlOverride || fallbackPageUrl,
pageUrl: pageUrlOverride || this.page?.url,
});
});
}
Expand Down
12 changes: 3 additions & 9 deletions src/Plugins/I18nPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ class Comparator {
}
}

function getPageInFilter(context) {
return (
context.page || context.ctx?.page || context.context?.environments?.page
);
}

function normalizeInputPath(inputPath, extensionMap) {
if (extensionMap) {
return extensionMap.removeTemplateExtension(inputPath);
Expand Down Expand Up @@ -248,7 +242,7 @@ function EleventyPlugin(eleventyConfig, opts = {}) {
function (url, langCodeOverride) {
let langCode =
langCodeOverride ||
LangUtils.getLanguageCodeFromUrl(getPageInFilter(this)?.url) ||
LangUtils.getLanguageCodeFromUrl(this.page?.url) ||
options.defaultLanguage;

// Already has a language code on it and has a relevant url with the target language code
Expand Down Expand Up @@ -298,7 +292,7 @@ function EleventyPlugin(eleventyConfig, opts = {}) {
// Refactor to use url
// Find the links that are localized alternates to the inputPath argument
eleventyConfig.addFilter(options.filters.links, function (urlOverride) {
let url = urlOverride || getPageInFilter(this)?.url;
let url = urlOverride || this.page?.url;
return contentMaps.localeUrlsMap[url] || [];
});

Expand All @@ -312,7 +306,7 @@ function EleventyPlugin(eleventyConfig, opts = {}) {
languageCode = options.defaultLanguage;
}

let page = pageOverride || getPageInFilter(this);
let page = pageOverride || this.page;
let url; // new url
if (contentMaps.localeUrlsMap[page.url]) {
for (let entry of contentMaps.localeUrlsMap[page.url]) {
Expand Down

0 comments on commit 44a48cb

Please sign in to comment.