Skip to content

Commit d80f508

Browse files
author
Phil Hawksworth
committed
Nicer cloundinary URL formatting
1 parent a6eb465 commit d80f508

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.eleventy.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,23 @@ module.exports = function(eleventyConfig) {
161161
});
162162

163163

164-
eleventyConfig.addFilter("replace", (str, from, to) => {
165-
if (str) {
166-
return str.replace(new RegExp(from, "g"), to);
164+
// Format a path to avoid any Cloudinary URL API miss-steps.
165+
eleventyConfig.addFilter("cloudinaryifyPath", (str) => {
166+
167+
if(str) {
168+
// we need to double escape some characters which might appear in text
169+
// but are meaningful in cloudinary URLs
170+
str = str.replace(/,/g, '%252C');
171+
str = str.replace(/\//g, '%252F');
172+
173+
// add generic url encoding
174+
str = encodeURI(str);
167175
}
176+
return str;
168177
});
169178

170179

180+
171181
// favicons files
172182
eleventyConfig.addPassthroughCopy("src/site/browserconfig.xml");
173183
eleventyConfig.addPassthroughCopy("src/site/site.webmanifest");

src/site/glossary-pages.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ layout: layouts/base.njk
1010
eleventyComputed:
1111
title: "{{ item.term }}"
1212
description: "What is {{ item.term }}? {{ item.summary}}"
13-
ogimage: "/img/og/glossary/{{ item.term | replace(',','%252C') | replace('/', '%252F') }}/{{ item.summary | replace(',','%252C') | replace('/', '%252F') }}"
13+
ogimage: "/img/og/glossary/{{ item.term | cloudinaryifyPath }}/{{ item.summary | cloudinaryifyPath }}"
1414

1515
---
1616

0 commit comments

Comments
 (0)