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

front page filter by source file type #1635

Closed
btrem opened this issue Feb 8, 2021 · 2 comments
Closed

front page filter by source file type #1635

btrem opened this issue Feb 8, 2021 · 2 comments

Comments

@btrem
Copy link

btrem commented Feb 8, 2021

My front page shows excerpts of recent posts, some of which are markdown, some nunjucks. If I filter the content with safe {{ post.data.page.excerpt | safe }} the njk excerpts display correctly, but md excerpts are not processed into html. If I add a markdown filter {{ post.data.page.excerpt | md | safe }}, the md excerpts display correctly, but njk do not; because the html code in njk files are indented, the excerpts are converted to code blocks.

To solve this, I write this code in my layout:

{% if post.inputPath.endsWith(".md")  %}
    {{ post.data.page.excerpt | md | safe }}
{% else %}
    {{ post.data.page.excerpt | safe }}
{% endif %}

Is this the right way to handle this? Is there another way I don't know about, maybe a way to determine file type instead of checking the end of the source file extension?

@btrem btrem added the education label Feb 8, 2021
@pdehaan
Copy link
Contributor

pdehaan commented Feb 8, 2021

Neat, I didn't know you could do {% if post.inputPath.endsWith(".md") %} in Nunjucks.

I think this is a great solution. Only alternative I could think of would be adding mdExcerpt frontmatter to files that you want preprocessed by your custom "md" filter, then you could do something like this if you're looping over a collection or whatever:

{% if post.data.mdExcerpt  %}
    {{ post.data.page.excerpt | md | safe }}
{% else %}
    {{ post.data.page.excerpt | safe }}
{% endif %}

My other suggestions would be worse, like a filter named "maybeMd" that takes the input string and a boolean value on whether it should return the raw input string or a markdown-ified version:

{{ post.data.page.excerpt | maybeMd(post.data.mdExcerpt) | safe }}
eleventyConfig.addFilter("maybeMd", (value, isMd=false) => {
  if (isMd) {
    return eleventyConfig.getFilter("md")(value);
  }
  return value;
});

@btrem
Copy link
Author

btrem commented Feb 8, 2021

Neat, I didn't know you could do {% if post.inputPath.endsWith(".md") %} in Nunjucks.

Neither did I 'til I tried it. I found it in a thread about filters, and decided to blindly give it a go. :)

For now, at least, I'll stick with checking the extension. Otherwise, I'd have to remember to add an mdExcerpt item to the front matter of all posts, but only when the post is written in markdown.

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

No branches or pull requests

2 participants