-
Notifications
You must be signed in to change notification settings - Fork 627
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
Sample RSS Feed #255
Open
rjm226
wants to merge
5
commits into
metalsmith:master
Choose a base branch
from
rjm226:sample-rss-feed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sample RSS Feed #255
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79f7410
added example blog
rjm226 e045ef5
added RSS feed example
rjm226 0a2c3e0
changed spacing issue in ReadMe@Creating and running your package.jso…
rjm226 1195ecd
removed clean()
rjm226 fd9fcd0
changed ReadMe to include instructions for nvm and nvm-windows
rjm226 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
build: node_modules | ||
node index.js | ||
|
||
node_modules: package.json | ||
npm install | ||
|
||
.PHONY: build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
|
||
# RSS Feed Example | ||
|
||
This example uses Metalsmith to create a static site and implements an rss feed of all your posts using the metalsmith-feed and metalsmith-collections plugins. | ||
|
||
## Install Node and Update NPM | ||
|
||
You should have npm and node installed. If you do not have these installed we recommend using [nvm](https://github.com/creationix/nvm/blob/master/README.markdown#installation) or [nvm-windows](https://github.com/coreybutler/nvm-windows) depending on what operating system you are using. The links above will provide you will detailed installation instructions. | ||
|
||
##Setting Up Your Project | ||
|
||
Let's create a directory for this project and cd into it. Navigate to the folder where you usually place all your projects. In your favorite bash client make the projects directory, I'm going to name mine `rss-feed-example`. | ||
|
||
$ mkdir rss-feed-example | ||
|
||
Now that the directory is created, I'm going to open it up with | ||
|
||
$ cd rss-feed-example | ||
|
||
##Creating and Running your package.json file | ||
|
||
Once your in the projects directory, create a file called `package.json`. The `package.json` file will have information relative to your project and is necessary for npm to install different tools and modules that will help you while building your project. The contents of your `package.json` should look like this. | ||
|
||
{ | ||
"name": "rss-feed-example", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"handlebars": "^4.0.5", | ||
"metalsmith": "^2.1.0", | ||
"metalsmith-layouts": "^1.4.1", | ||
"metalsmith-markdown": "^0.2.1", | ||
"metalsmith-permalinks": "^0.5.0", | ||
"metalsmith-collections": "^0.7.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there mixed tabs and spaces here? Is that why it doesn't line up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure could be from copy pasting. I'll look into it. |
||
"metalsmith-feed": "^0.2.0" | ||
} | ||
} | ||
|
||
Let's go through the fields in this file. | ||
|
||
1. The name field is the name of your project and is required. | ||
2. The version number is the version number of your project and is required. | ||
3. Dependencies are the dependencies the project will use. | ||
4. handlebars will enable handlebars templating to be used in our project. | ||
5. metalsmith allows metalsmith to be used in our project | ||
6. metalsmith-layouts helps us generate templates in conjunction with handlebars. | ||
7. metalsmith-markdown enables markdown files to be compiled into html files. | ||
8. metalsmith-permalinks will prettify our urls and nest our subdirectories to make them more managable. | ||
9. metalsmith-collection helps us group different files and iterate over these files in other places in our project. | ||
10. metalsmit-feed will generate the rss feed for us based on our metalsmith-collection plugin options. | ||
|
||
Now that we have created our package.json, file let's run it. If npm completes the installation with no errors you should see a new folder in the directory named `node-modules`, which will have all the dependencies we listed in the `package.json` file. | ||
|
||
$ npm install | ||
|
||
##Creating Our index.js file | ||
|
||
Lets create a simple index.js file in our projects root directory. The file should look like this: | ||
|
||
var Metalsmith = require('metalsmith'); | ||
var markdown = require('metalsmith-markdown'); | ||
var layouts = require('metalsmith-layouts'); | ||
var permalinks = require('metalsmith-permalinks'); | ||
var collections = require('metalsmith-collections'); | ||
var feed = require('metalsmith-feed'); | ||
|
||
|
||
Metalsmith(__dirname) | ||
.metadata({ | ||
title: "RSS Feed Example", | ||
description: "It's about saying »Hello« to the World and letting the world keep up with your blog via RSS feed.", | ||
generator: "Metalsmith", | ||
url: "http://www.metalsmith.io/", | ||
site:{ | ||
title: "RSS Feed Example", | ||
url: "http://www.metalsmith.io/", | ||
author: "George RR Martin" | ||
} | ||
}) | ||
.source('./src') | ||
.destination('./build') | ||
.use(collections({ | ||
posts: { | ||
pattern: 'posts/*.md' | ||
} | ||
})) | ||
.use(feed({ | ||
collection: 'posts' | ||
})) | ||
.use(markdown()) | ||
.use(permalinks()) | ||
.use(layouts({ | ||
engine: 'handlebars' | ||
})) | ||
.build(function(err, files) { | ||
if (err) { throw err; } | ||
}); | ||
|
||
Now lets dissect this file, line-by-line. | ||
|
||
var Metalsmith = require('metalsmith'); | ||
var markdown = require('metalsmith-markdown'); | ||
var layouts = require('metalsmith-layouts'); | ||
var permalinks = require('metalsmith-permalinks'); | ||
var collections = require('metalsmith-collections'); | ||
var feed = require('metalsmith-feed'); | ||
|
||
At the top of our `index.js` we declare our variables for metalsmith as well as all the plugins we will be using in this file. You should notice that all these variables are plugins that we declared in our package.json. | ||
|
||
Metalsmith(__dirname) | ||
.metadata({ | ||
title: "RSS Feed Example", | ||
description: "It's about saying »Hello« to the World and letting the world keep up with your blog via RSS feed.", | ||
generator: "Metalsmith", | ||
url: "http://www.metalsmith.io/", | ||
site:{ | ||
title: "RSS Feed Example", | ||
url: "http://www.metalsmith.io/", | ||
author: "George RR Martin" | ||
} | ||
}) | ||
.source('./src') | ||
.destination('./build') | ||
.use(collections({ | ||
posts: { | ||
pattern: 'posts/*.md' | ||
} | ||
})) | ||
.use(feed({ | ||
collection: 'posts' | ||
})) | ||
.use(markdown()) | ||
.use(permalinks()) | ||
.use(layouts({ | ||
engine: 'handlebars' | ||
})) | ||
.build(function(err, files) { | ||
if (err) { throw err; } | ||
}); | ||
|
||
Here is our metalsmith instance, where metalsmith declares its methods sequentially from metalsmiths native api as well as any methods from plugins we might be using. So line-by-line lets see what going on here. | ||
|
||
1. The `.metadata()` method is providing variables that can be used throughout your project. This is a good place to put general variables that will be used throughout the project. In this particular instance we use the title field to declare our ebook title which is a required variable in our custom ebook plugin we will be building later on in this tutorial. Make sure to add the title now. In this project we needed to add the site object to our metadata method as its required by our `metalsmith-feed` plugin. | ||
|
||
site:{ | ||
title: "RSS Feed Example", | ||
url: "http://www.metalsmith.io/", | ||
author: "George RR Martin" | ||
} | ||
|
||
2. The `source()` method declares the directory where the files will be pre-manipulation. | ||
3. The `destination()` method declares the destination directory the files will end up post-manipulation. | ||
4. The `use()` method wraps instances of plugins we will use in our project. | ||
5. The `collections()` method groups all our files from the `./posts` folder with the `.md` extension. We can now iterate through this object in our template files if we want to list all our chapters or chapters of a certain category. You can checkout this plugins docs [here](https://github.com/segmentio/metalsmith-collections). | ||
6. The `feed()` method grabs all the files in the `posts` collection and generates an RSS feed based on the data contained in these files. The `feeds()` method relies on the `collections()` metho and should be input into the Metalsmith instance in this order to make sure the plugins work properly. | ||
7. The `markdown()` enables the markdown syntax for editing pre-compilation. | ||
8. The `permalinks()` method nests our files in subdirectories. | ||
9. The `layouts()` enables handlebars as the templating engine. | ||
10. The `ebook()` plugin generates pdf, epub, and mobi files based on the html files in our project. | ||
11. The `build()` method will finally build the project using the plugins and options we've provided and spit out any errors if they occur. You can also add code to this block if maybe you want it to notify you that the build was successful. | ||
|
||
##Understanding the projects `./src` directory | ||
|
||
1. As designated in our `source()` method in our `index.js` file, everything in the `./src` directory will be compiled by metalsmith. After the compilation is complete all the compiled files will end up in our `./build` folder as designated in our `destination()` method in our `index.js` file. | ||
2. If we take a look in our `./src` file we see a folder called `/posts` with various markdown files in it. All of these markdown files will be converted into html files and sent to your `/build` folder. We also designate all `.md` files in this directory to become a collection named 'posts' as designated in our `collection()` method in our `index.js` file. This is important because our `feed()` method will use this collection to generate an RSS feed based on this collection. | ||
|
||
##Running our index.js file | ||
|
||
1. Once we are back in our projects root directory open up your favorite bash client and run: | ||
|
||
$ node index.js | ||
|
||
2. If there are no errors the book should have been compiled. | ||
3. Open up your projects `./build` folder. Along with an `index.html` and a `/posts` directory, there should be an `rss.xml` file. Your RSS feed is complete! | ||
4. For more options when using the `metalsmith-feed` plugin visit the plugins docs site [here](https://github.com/hurrymaplelad/metalsmith-feed). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var Metalsmith = require('metalsmith'); | ||
var markdown = require('metalsmith-markdown'); | ||
var layouts = require('metalsmith-layouts'); | ||
var permalinks = require('metalsmith-permalinks'); | ||
var collections = require('metalsmith-collections'); | ||
var feed = require('metalsmith-feed'); | ||
|
||
|
||
Metalsmith(__dirname) | ||
.metadata({ | ||
title: "RSS Feed Example", | ||
description: "It's about saying »Hello« to the World and letting the world keep up with your blog via RSS feed.", | ||
generator: "Metalsmith", | ||
url: "http://www.metalsmith.io/", | ||
site:{ | ||
title: "RSS Feed Example", | ||
url: "http://www.metalsmith.io/", | ||
author: "George RR Martin" | ||
} | ||
}) | ||
.source('./src') | ||
.destination('./build') | ||
.use(collections({ | ||
posts: { | ||
pattern: 'posts/*.md' | ||
} | ||
})) | ||
.use(feed({ | ||
collection: 'posts' | ||
})) | ||
.use(markdown()) | ||
.use(permalinks()) | ||
.use(layouts({ | ||
engine: 'handlebars' | ||
})) | ||
.build(function(err, files) { | ||
if (err) { throw err; } | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>{{ title }}</title> | ||
<meta name="description" content="{{ description }}"> | ||
</head> | ||
<body> | ||
<header> | ||
<p> | ||
<a href="/">Home</a> | ||
</p> | ||
</header> | ||
<h1>{{ title }}</h1> | ||
<p>{{ description }}</p> | ||
|
||
{{{ contents }}} | ||
|
||
<footer> | ||
<p>Generated with {{ generator }} — <a href="{{ url }}">{{ url }}</a></p> | ||
</footer> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>{{ title }}</title> | ||
<meta name="description" content="{{ description }}"> | ||
</head> | ||
<body> | ||
<header> | ||
<p> | ||
<a href="/">Home</a> | ||
</p> | ||
|
||
</header> | ||
<h1>{{ title }}</h1> | ||
<time>{{ date }}</time> | ||
{{{ contents }}} | ||
|
||
<footer> | ||
<p>Generated with {{ generator }} — <a href="{{ url }}">{{ url }}</a></p> | ||
</footer> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "rss-feed-example", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"handlebars": "^4.0.5", | ||
"metalsmith": "^2.1.0", | ||
"metalsmith-layouts": "^1.4.1", | ||
"metalsmith-markdown": "^0.2.1", | ||
"metalsmith-permalinks": "^0.5.0", | ||
"metalsmith-collections": "^0.7.0", | ||
"metalsmith-feed": "^0.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
layout: layout.html | ||
--- | ||
|
||
<h2>Read what I have to say</h2> | ||
|
||
<a href="/posts/first-post/">First post</a> | ||
|
||
<a href="/posts/second-post/">Second post</a> | ||
|
||
<a href="/posts/third-post/">Third post</a> | ||
|
||
<a href="/posts/fourth-post/">Fourth post</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: My First Post | ||
date: 2012-08-20 | ||
layout: post.html | ||
--- | ||
|
||
An interesting post about how it's going to be different this time around. I'm going to write a lot more nowadays and use this blog to improve my writing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: My Fourth Post | ||
date: 2012-12-07 | ||
layout: post.html | ||
--- | ||
|
||
A really short, rushed-feeling string of words. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: My Second Post | ||
date: 2012-08-23 | ||
layout: post.html | ||
--- | ||
|
||
A super-interesting piece of prose I have already written weeks ago. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
title: My Third Post | ||
date: 2012-09-28 | ||
layout: post.html | ||
--- | ||
|
||
A slightly late, less interesting piece of prose. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For running locally I'd recommend using nvm/nvm-windows to install node -
nvm install node
will install the latest stable version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. Lets make that change.