Merge pull request #48 from benjifs/jsonfeed

JSON Feed
This commit is contained in:
Zach Leatherman 2020-07-27 11:30:43 -05:00 committed by GitHub
commit 7be229d9d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View File

@ -24,6 +24,14 @@ module.exports = function(eleventyConfig) {
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd');
});
// Content for jsonfeed
eleventyConfig.addFilter('jsonStringify', content => {
if (!content) {
content = "";
}
return JSON.stringify(content);
});
// Get the first `n` elements of a collection.
eleventyConfig.addFilter("head", (array, n) => {
if( n < 0 ) {

View File

@ -8,6 +8,10 @@
"path": "/feed/feed.xml",
"id": "https://myurl.com/"
},
"jsonfeed": {
"path": "/feed/feed.json",
"url": "https://myurl.com/feed/feed.json"
},
"author": {
"name": "Your Name Here",
"email": "youremailaddress@example.com"

View File

@ -8,6 +8,7 @@
<link rel="stylesheet" href="{{ '/css/index.css' | url }}">
<link rel="stylesheet" href="{{ '/css/prism-base16-monokai.dark.css' | url }}">
<link rel="alternate" href="{{ metadata.feed.path | url }}" type="application/atom+xml" title="{{ metadata.title }}">
<link rel="alternate" href="{{ metadata.jsonfeed.path | url }}" type="application/json" title="{{ metadata.title }}">
</head>
<body>
<header>

38
feed/json.njk Normal file
View File

@ -0,0 +1,38 @@
---
permalink: feed/feed.json
eleventyExcludeFromCollections: true
---
{
"version": "https://jsonfeed.org/version/1",
"title": "{{ metadata.title }}",
"home_page_url": "{{ metadata.url }}",
"feed_url": "{{ metadata.jsonfeed.url }}",
"description": "{{ metadata.description }}",
"author": {
"name": "{{ metadata.author.name }}",
"url": "{{ metadata.author.url }}"
},
"items": [
{%- for post in collections.posts %}
{% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset -%}
{
"id": "{{ absolutePostUrl }}",
"url": "{{ absolutePostUrl }}",
"title": "{{ post.data.title }}",
"content_html": {{ post.templateContent | jsonStringify | safe }},
"date_published": "{{ post.date | rssDate }}",
"tags": [
{%- for c in post.data.category %}
"{{ c }}"
{%- if not loop.last -%}
,
{%- endif -%}
{%- endfor %}
]
}
{%- if not loop.last -%}
,
{%- endif -%}
{%- endfor %}
]
}