11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.29k stars 496 forks source link

Unable to call `absoluteUrl` from `leventy-plugin-rss` #580

Closed sarunw closed 5 years ago

sarunw commented 5 years ago

Unable to call absoluteUrl from leventy-plugin-rss. Getting error "Unable to call absoluteUrl, which is undefined or falsey"

Any extra step I need to be able to use this? I add this to .eleventy.js and package.json

const pluginRss = require("@11ty/eleventy-plugin-rss");

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(pluginRss);
}
jevets commented 5 years ago

Are you using absoluteUrl as a nunjucks filter from a nunjucks file, like the sample file does?

  {%- for post in collections.posts %}
  {% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %}
  <!-- ... -->
  {%- endfor %}

The plugin only defines a nunjucks filter for use in .njk files, not a universal filter. So that could be the culprit. Are you trying to use it from another template language (i.e. liquid or handlebars)?

https://github.com/11ty/eleventy-plugin-rss/blob/master/.eleventy.js#L17

sarunw commented 5 years ago

Yes, I use that in .njk

Actually it don't even work in https://github.com/11ty/eleventy-base-blog

You can try change /sitemap.xml.njk line 8 from

{{ page.url | url | absoluteUrl(metadata.url) }}

to

{{ absoluteUrl(metadata.url) }}
jevets commented 5 years ago

{{ absoluteUrl(metadata.url) }} isn't proper nunjucks filter syntax. (Can't call them like functions)

Tried this syntax? {{ metadata.url | absoluteUrl }}

Edit: What I mean is that a nunjucks filter is applied to an initial value, like this: {{ someValue | someFilter }}

sarunw commented 5 years ago

Ahhhh thanks, this part {{ page.url | url | absoluteUrl(metadata.url) }} made me think I can call it like a function.

Aetherpoint commented 5 years ago

Sarunw, encountered something similar, looks like that example is using some plugins that would enable that syntax. You’d need to make sure those were also installed.

denisbrodbeck commented 5 years ago

@sarunw specifically, you need to add the @11ty/eleventy-plugin-rss package to your project. absoluteUrl belongs to the api of that package.

KyleMit commented 4 years ago

To clear up the syntax question, this is valid syntax for a nunjucks filter. Here's the docs on filters:

Filters are essentially functions that can be applied to variables. They are called with a pipe operator (|) and can take arguments.

{{ foo | title }}
{{ foo | join(",") }}
{{ foo | replace("foo", "bar") | capitalize }}

In this particular case, this is the example used in the eleventy-base-blog

Which adds the filter via the eleventy-plugin-rss plugin like this (slightly simplified):

eleventyConfig.addNunjucksFilter("absoluteUrl", (href, base) => {
  let { URL } = require("url");
  return (new URL(href, base)).toString()
})

So as long as that the rss plugin is installed in your project, or you've manually added the filter above, you should be able to use that filter like this {{ url | absoluteUrl(metadata.url) }}

Also, make sure you've added your project URL to a metadata data file