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

Adds simple website example #251

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
127 changes: 127 additions & 0 deletions examples/simple-website/README.md
@@ -0,0 +1,127 @@
# Simple website

This example uses the Javascript API to create a simple site containing links to a Twitter, GitHub, and Facebook page.

## Building the site

1. Clone the `metalsmith` repo: `git clone git@githib.com:metalmsith/metalsmith.git`
2. Navigate to the site in terminal: `cd path/to/metalsmith/examples/simple-website`
3. Install dependencies: `npm i`
4. Run the build script: `node index.js`

## Creating a similar site

#### Initial setup

First, make sure to have `node` & `npm` installed globally. Then, create a working directory, like so:

```bash
mkdir my-website
cd my-website
```

Let's create some folders to work with later:

```bash
mkdir src layouts
```

Next, let's create a `package.json` file:

```bash
npm init
```

Setup is almost done! Final step is to install our dependencies:

```bash
npm install --save metalsmith metalsmith-markdown metalsmith-layouts pug
```

#### The build script

Now that we have our site structure & setup ready, we can start developing! First step is probably to create out our build script. Create a file named `index.js` that looks something like this:

```js
var Metalsmith = require('metalsmith')
var layouts = require('metalsmith-layouts')
var markdown = require('metalsmith-markdown')

Metalsmith(__dirname)
.metadata({
title: 'Simple Website',
description: 'An extremely simple example website for the extremely simple static-site generator',
twitter: 'metalsmith',
github: 'metalsmith',
facebook: 'metalsmith'
})
.source('./src')
.destination('./build')
.use(markdown())
.use(layouts({
engine: 'pug',
pretty: true,
directory: 'layouts',
default: 'default.pug',
pattern: '**/*.html'
}))
.build(function(err, files) {
if (err) { throw err; }
})
```

Since the metadata in the file above is creating most of the content in the site, it's a good idea to update that.

Let's look at what our build script does:

1. It defines our dependencies (`var Metalsmith = require('metalsmith')`)
2. It initializes our Metalsmith instance (`Metalsmith(__dirname)`)
3. It defines the metadata for our site to use (`.metadata({...})`)
4. It defines our source and output directories (`.source()` & `.destination()`)
5. It processes the markdown files in the `./src` directory (`.use(markdown())`)
6. It runs our processed markdown through [Pug templates](http://pugjs.org) (`.use(layouts({...}))`)
7. It builds the site to our destination directory (`.build(...)`)

#### Our content & layout

A build script is only good if we have something to build. Let's use the metadata defined in our build script to create a simple homepage with links to our social media destinations. Here's what our `default.pug` layout should look like:

```pug
doctype html
head
title= title
meta(name="description", content= description)
body
h1= title
p!= description
.links
a(href="https://twitter.com/" + twitter) Twitter
a(href="https://github.com/" + github) GitHub
a(href="https://facebook.com/" + facebook) Facebook
```

This might look confusing if you've never used Jade/Pug before, but it's very simple. The first word of each line represents an HTML tag (`h1 === <h1>, .links === <div class="links">`). We're then defining our document structure with indentation (Pug is whitespace-sensitive), using `=` to input our metadata into the template, and using `+` to concatenate our Twitter, GitHub, and Facebook usernames into their respective links. Let's save this file as `layouts/defaul.pug`.

Next, we need a file to run through this template. It's a very complicated document:

```md
---
layout: default.pug
---
```

...that's it. The key/value pairs (YAML data) between each line tell Metalsmith how to process our file. Here, we're simply telling Metalsmith to process this file through our `default.pug` template. This file should live at `src/index.md`.

#### Building our site

To build our site, let's move back to terminal.

```bash
node index.js
```

...and our site is built into `/build`!

#### Next steps

Our site is looking mighty plain. Try using `metalsmith-sass`, `metalsmith-postcss`, or one of [the many other plugins](http://www.metalsmith.io/#the-plugins) to add styles to the site. Alternatively, run your typical build system alongside Metalsmith. You can also try adding an icon set, including more information, or adding more pages to the site!
10 changes: 10 additions & 0 deletions examples/simple-website/build/index.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<head>
<title>Simple Website</title>
<meta name="description" content="An extremely simple example website for the extremely simple static-site generator">
</head>
<body>
<h1>Simple Website</h1>
<p>An extremely simple example website for the extremely simple static-site generator</p>
<div class="links"><a href="https://twitter.com/metalsmith">Twitter</a><a href="https://github.com/metalsmith">GitHub</a><a href="https://facebook.com/metalsmith">Facebook</a></div>
</body>
25 changes: 25 additions & 0 deletions examples/simple-website/index.js
@@ -0,0 +1,25 @@
var Metalsmith = require('metalsmith')
var layouts = require('metalsmith-layouts')
var markdown = require('metalsmith-markdown')

Metalsmith(__dirname)
.metadata({
title: 'Simple Website',
description: 'An extremely simple example website for the extremely simple static-site generator',
twitter: 'metalsmith',
github: 'metalsmith',
facebook: 'metalsmith'
})
.source('./src')
.destination('./build')
.use(markdown())
.use(layouts({
engine: 'pug',
pretty: true,
directory: 'layouts',
default: 'default.pug',
pattern: '**/*.html'
}))
.build(function(err, files) {
if (err) { throw err; }
})
11 changes: 11 additions & 0 deletions examples/simple-website/layouts/default.pug
@@ -0,0 +1,11 @@
doctype html
head
title= title
meta(name='description', content= description)
body
h1= title
p!= description
.links
a(href="https://twitter.com/" + twitter) Twitter
a(href="https://github.com/" + github) GitHub
a(href="https://facebook.com/" + facebook) Facebook
10 changes: 10 additions & 0 deletions examples/simple-website/package.json
@@ -0,0 +1,10 @@
{
"name": "simple-website-example",
"private": true,
"dependencies": {
"metalsmith": "^2.2.0",
"metalsmith-layouts": "^1.6.5",
"metalsmith-markdown": "^0.2.1",
"pug": "^2.0.0-beta6"
}
}
Empty file.