Using Techy as a blog platform leads to the need of RSS feed. And because every page is actually a separate object we are able to generate RSS pretty easily. Let's say that we have the following file structure:
/blog
/_articles
/A.md
/B.md
/C.md
The content of the Markdown files is as follows:
A.md
---
title: Article A
date: 10-04-2014
---
# <% get('title') %>
B.md
---
title: Article B
date: 12-04-2014
---
# <% get('title') %>
C.md
---
title: Article C
date: 22-02-2014
---
# <% get('title') %>
By using the Yaml Front Matter we are defining title
and date
properties for every page.
If we go to the blog
directory and run techy --src ./_articles
command we will get three new files in the _dist/articles
directory
- A.html
, B.html
and C.html
. So far so good. We have our pages generated and the users are able to see them. Now, we need
to write our RSS. Let's create a new file in the blog
folder called feed.xml.source
. It will act as a template for the final file. Its extension is not .md
so
Techy will not process it by default. We have to additionally point that out in the TechyFile.js
of the project:
// blog/TechyFile.js
module.exports = function() {
return {
process: [
'feed.xml.source'
]
}
}
Running the same command command again brings feed.xml
file. The next step is to fetch all the pages in the blog and fill the xml with their titles and links. Here is how feed.xml.source
may
look like:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Example Channel</title>
<link>http://example.com</link>
<description>My example channel</description>
<% rss() %>
</channel>
</rss>
The actual generation of the items is happening in a custom function rss
. Techy gives you ability to write such functions and put whatever you need there. All you have to do is to create a file with the
function's name ending on techy.js
and the library will include it in the global scope. It's also nice that it is executed in the context of the current page so for example this.get
or
this.set
are valid methods.
// blog/rss.techy.js
module.exports = function() {
var rss = '',
pages = this.pages();
var sortByDate = function(page1, page2) {
var strToDate = function(str) {
var tmp = str.split('-');
return new Date(tmp[2], tmp[1], tmp[0]);
}
return strToDate(page1.get('date')) < strToDate(page2.get('date'));
}
pages.sort(sortByDate);
for(var i=0; i<pages.length, page=pages[i]; i++) {
rss += '\n<item>';
rss += '<title>' + page.get('title') + '</title>';
rss += '<link>http://example.com/blog/' + page.get('paths').url + '</link>';
rss += '</item>';
}
return rss;
}
Notice that we are sorting the pages by the variable date
which is defined in the Markdown files.
The resulted feed.xml
contains all the three articles sorted by date:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Example Channel</title>
<link>http://example.com/</link>
<description>My example channel</description>
<item><title>Article B</title><link>http://example.com/blog/articles/B.html</link></item>
<item><title>Article A</title><link>http://example.com/blog/articles/A.html</link></item>
<item><title>Article C</title><link>http://example.com/blog/articles/C.html</link></item>
</channel>
</rss>
The full code of the example could be found here.