Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url filter should detect absolute urls without the protocol #1247

Closed
oscarotero opened this issue Jun 11, 2020 · 4 comments · Fixed by #1276
Closed

url filter should detect absolute urls without the protocol #1247

oscarotero opened this issue Jun 11, 2020 · 4 comments · Fixed by #1276

Comments

@oscarotero
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm using the url filter for images in this way:

<img src="{{ image.src | url }}">

But sometimes, the images are external urls. To make them work in http and https contexts, I remove the protocol, so the urls are like this: //placehold.it/300x100. Seems like the filter does not detect this and generate the invalid url /placehold.it/300x100.

Describe the solution you'd like
I can see that the filter can detect http and https protocols (https://github.com/11ty/eleventy/blob/master/src/Filters/Url.js#L8) so I propose adding another condition: url.indexOf("//") === 0.

@denisbrodbeck
Copy link

You could write a wrapper filter and use that instead.

// .eleventy.js

const url = require(`@11ty/eleventy/src/Filters/Url`);

module.exports = function (config) {
  config.addFilter(`myurl`, (uri) => {
    if( condition) {
        return myspecialcase;
    }
    return url(uri);
  });

  // Base Config
  return {
    dir: {
      input: `src`,
      output: `dist`,
      includes: `_includes`,
      layouts: `_layouts`,
      data: `_data`,
    },
    templateFormats: [`njk`, `md`, `11ty.js`],
    htmlTemplateEngine: `njk`,
    markdownTemplateEngine: `njk`,
  };
};

Then use it instead of url:

<img src="{{ image.src | myurl }}">

@oscarotero
Copy link
Contributor Author

@denisbrodbeck Yep, this would be a workaround, but I think this is a malfunction of the filter, so I'd rather fix it.

@denisbrodbeck
Copy link

Sure, fire away with a PR 😄

@zachleat
Copy link
Member

zachleat commented Jul 1, 2021

Thanks for the PR! It’s merged and will ship with 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants