Building a Static Website with Markdown and Jekyll

Posted by Andrew Denner on November 22, 2024 · 14 mins read

Building a Static Website with Markdown and Jekyll

As software developers and Linux enthusiasts, we often seek efficient and streamlined ways to share our projects, documentations, and thoughts. Static websites offer a lightweight and secure solution without the overhead of dynamic server-side processing. In this post, we’ll explore how to use Markdown and Jekyll to create a static website, delve into the advantages of this approach, and provide detailed instructions to get you started.

Table of Contents

  1. Introduction
  2. The Journey to Static Sites
  3. Understanding Markdown
  4. Getting Started with Jekyll
  5. Building Your First Jekyll Site
  6. Hosting Your Static Website
  7. Conclusion
  8. Additional Resources

Introduction

Building a personal website or blog doesn’t have to be a daunting task. With tools like Markdown for content creation and Jekyll for site generation, you can efficiently create and manage a static website. This approach eliminates the need for complex setups involving databases and server-side scripting languages, making your site faster, more secure, and easier to maintain.

The Journey to Static Sites

Early Days: WYSIWYG Editors

In the early days of web development, tools like Microsoft FrontPage and Adobe Dreamweaver were popular. They allowed users to design websites using a graphical interface (What You See Is What You Get - WYSIWYG), which was ideal for beginners. However, the HTML generated by these tools was often bloated and not standards-compliant.

Transition to Raw HTML and Dynamic Sites

As developers became more proficient, many transitioned to writing raw HTML and CSS to have finer control over their websites. This approach, while offering more flexibility, was time-consuming, especially for larger sites where content updates were frequent.

The desire for dynamic content led to the adoption of content management systems (CMS) like WordPress. WordPress made it easy to create and manage content, but it introduced new challenges:

  • Security Risks: Being a popular platform, WordPress sites are frequent targets for hackers.
  • Maintenance Overhead: Regular updates are necessary to patch vulnerabilities and maintain plugins.
  • Performance Issues: Dynamic content generation can slow down site performance.

Embracing Static Site Generators

For many developers, the optimal solution lies in static site generators like Jekyll. They combine the simplicity and performance of static sites with the ease of content management provided by dynamic sites.

Understanding Markdown

What Is Markdown?

Markdown is a lightweight markup language that allows you to write content in a plain text format that is easy to read and write. It converts simple text formatting into valid HTML, making it ideal for writing documentation, blog posts, and any content destined for the web.

Why Use Markdown?

  • Simplicity: Easy to learn and use, even for those unfamiliar with HTML.
  • Readability: The plain text format is readable without rendering, which is beneficial for version control and collaboration.
  • Flexibility: Can include raw HTML for complex formatting when needed.

Basic Markdown Syntax

Here’s a quick reference to some common Markdown elements:

Headings

# Heading Level 1
## Heading Level 2
### Heading Level 3
#### Heading Level 4
##### Heading Level 5
###### Heading Level 6

Emphasis

*Italic* or _Italic_
**Bold** or __Bold__
***Bold and Italic*** or ___Bold and Italic___

Lists

  • Unordered Lists

    - Item 1
    - Item 2
      - Subitem 2.1
      - Subitem 2.2
    
  • Ordered Lists

    1. First Item
    2. Second Item
       1. Subitem 2.1
       2. Subitem 2.2
    
[Link Text](https://example.com)
```layout: post
title:  "Jekyll: the special sauce that makes this blog tick"
date:   2021-09-25 00:35:14 -0500
background: /img/textedit.jpg


#### Images

```markdown
![Alt Text](https://example.com/image.jpg)

Code Blocks

  • Inline Code

    Use the `code()` function.
    
  • Block Code

    ```language
    // Code example
    function greet() {
        console.log("Hello, World!");
    }
    

    ```

Tables

| Column 1 | Column 2 | Column 3 |
| -------- | -------- | -------- |
| Data 1   | Data 2   | Data 3   |
| Data 4   | Data 5   | Data 6   |

Using Markdown in Different Platforms

Markdown is widely supported and can be used in various environments:

  • Version Control: Platforms like GitHub and GitLab render Markdown files beautifully, making it ideal for README files and documentation.
  • Chat Clients: Many chat applications support Markdown or a subset of its syntax for formatting messages.
  • Static Site Generators: Tools like Jekyll, Hugo, and Gatsby use Markdown files for content creation.

Getting Started with Jekyll

What Is Jekyll?

Jekyll is a static site generator written in Ruby. It takes your Markdown files and converts them into a complete static website. Jekyll provides templating, includes, and other features that allow you to build complex sites without server-side processing.

Why Choose Jekyll?

  • Simplicity: No database or server-side scripting required.
  • Performance: Static sites are fast and require fewer resources to serve.
  • Security: Reduced risk of common web vulnerabilities that affect dynamic sites.
  • Community and Support: Active development and extensive documentation.

Prerequisites

To get started with Jekyll, ensure you have the following installed:

  • Ruby: Jekyll is a Ruby gem, so you’ll need Ruby installed on your system.
  • RubyGems: For managing Ruby gems (packages).
  • Bundler: Helps manage gem dependencies.

Installing Ruby and Bundler on Linux

For Debian/Ubuntu-based systems:

sudo apt-get install ruby-full build-essential zlib1g-dev

Add the following to your .bashrc or .zshrc to avoid installing gems as root:

# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"

Install Bundler:

gem install bundler

Building Your First Jekyll Site

Step 1: Install Jekyll

Install Jekyll and Bundler gems:

gem install jekyll bundler

Step 2: Create a New Jekyll Site

Navigate to the directory where you want to create your site and run:

jekyll new my-awesome-site

This command creates a new directory named my-awesome-site with the default Jekyll structure.

Step 3: Navigate to Your Site Directory

cd my-awesome-site

Step 4: Build and Serve Your Site Locally

To build your site and start a local development server, run:

bundle exec jekyll serve

Your site will be accessible at http://localhost:4000.

Step 5: Explore the Directory Structure

  • _config.yml: Main configuration file for your Jekyll site.
  • _posts/: Directory for blog posts. Posts follow the naming convention YEAR-MONTH-DAY-title.MARKUP.
  • _layouts/: Templates that define the structure of your pages.
  • _includes/: Reusable page fragments (e.g., header, footer).
  • _site/: The destination folder where your site is generated. Do not edit files here.

Step 6: Customize Your Site

Editing the Configuration

Open _config.yml and set your site parameters:

title: My Awesome Site
email: [email protected]
description: >- # this means to treat the following lines as a single string
  Welcome to my awesome site built with Jekyll.
baseurl: "" # the subpath of your site, e.g., /blog
url: "https://yourdomain.com" # the base hostname & protocol for your site

Creating Posts

Create a new Markdown file in the _posts directory:

cd _posts
touch 2023-10-25-welcome-to-jekyll.md

Add the following content:

---
layout: post
title:  "Welcome to Jekyll!"
date:   2023-10-25 12:00:00 +0000
categories: jekyll update
---

You can write content here in **Markdown**.

Here's some code:

```ruby
def print_hello
  puts 'Hello, world!'
end

#### Customizing Layouts and Includes

- **Layouts**: Modify files in `_layouts/` to change the structure of your pages.
- **Includes**: Use `_includes/` for snippets like headers, footers, or other reusable elements.

### Step 7: Build the Site

Whenever you make changes, rebuild the site:

```bash
bundle exec jekyll build

This command regenerates the static files in the _site directory.

Hosting Your Static Website

Option 1: GitHub Pages

GitHub Pages supports Jekyll out of the box.

Steps:

  1. Create a Repository: The repository name should be <your-username>.github.io.

  2. Push Your Site: Commit and push your Jekyll site to this repository.

  3. Access Your Site: Navigate to https://<your-username>.github.io.

Note: GitHub Pages uses a specific version of Jekyll. Ensure compatibility by referencing the GitHub Pages documentation.

Option 2: GitLab Pages

Similarly, GitLab Pages can host your Jekyll site.

Steps:

  1. Create a New Project: Name it appropriately.

  2. Add a .gitlab-ci.yml: This file defines the CI/CD pipeline to build and deploy your site.

    image: ruby:2.7
    
    before_script:
      - gem install jekyll bundler
    
    stages:
      - build
    
    variables:
      JEKYLL_ENV: production
    
    cache:
      paths:
        - vendor/
    
    build:
      stage: build
      script:
        - bundle install --path vendor
        - bundle exec jekyll build -d public
      artifacts:
        paths:
          - public
    
  3. Push Your Site: Commit and push your site files along with the .gitlab-ci.yml.

  4. Access Your Site: GitLab provides a URL where your site is hosted.

Option 3: Static Hosting Services

Services like Netlify and Vercel offer free plans for deploying static sites. They integrate with Git repositories and automatically build and deploy your site when you push changes.

Option 4: Self-Hosting

You can host your static site on any server or even on object storage services like Amazon S3.

Using Amazon S3 and CloudFront:

  1. Create an S3 Bucket: Enable static website hosting.

  2. Upload Your Site: Copy the contents of _site to your S3 bucket.

  3. Configure CloudFront: Set up a CDN distribution for better performance and HTTPS support.

Conclusion

Building a static website with Markdown and Jekyll offers a streamlined and efficient way to maintain an online presence. It’s particularly well-suited for developers and Linux users who appreciate simplicity, performance, and security.

By leveraging Markdown’s ease of use and Jekyll’s powerful static site generation, you can create a website that is both easy to manage and delightful for your visitors.

Additional Resources


This post is orriginally from a presentation that I gave to the Central Iowa Linux User’s Group and the video can be seen here


About the Author

As a seasoned software developer and the president of the Central Iowa Linux User Group (CIALUG), I have been navigating the evolving landscape of web development since the 90s. From the early days of WYSIWYG editors to embracing static site generators, my journey reflects a pursuit of efficient and effective solutions that empower developers to focus on what matters most: creating and sharing valuable content.