Creating a Jekyll theme involves several steps, from setting up a Jekyll environment to designing and coding the theme, and finally packaging it for reuse. Here’s a step-by-step guide to help you create a Jekyll theme:
Install Ruby and Jekyll: If you haven’t already, install Ruby and Jekyll. You can follow the instructions on the Jekyll installation page.
gem install jekyll bundler
Create a New Jekyll Site:
jekyll new my-theme
cd my-theme
Start the Jekyll Server:
bundle exec jekyll serve
Open your browser and navigate to http://localhost:4000
to see your new Jekyll site.
Directory Structure: Organize your theme with the following structure:
my-theme/
├── _includes/
├── _layouts/
├── _sass/
├── assets/
│ ├── css/
│ ├── images/
│ └── js/
├── _config.yml
├── index.html
├── LICENSE.txt
└── README.md
Create Layouts:
In _layouts/
, create a default layout file:
<!-- _layouts/default.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ page.title }}</title>
<link rel="stylesheet" href="{{ '/assets/css/styles.css' | relative_url }}">
</head>
<body>
{% include header.html %}
{{ content }}
{% include footer.html %}
</body>
</html>
Create Includes:
Add reusable components like header and footer in the _includes/
directory:
<!-- _includes/header.html -->
<header>
<h1>{{ site.title }}</h1>
<nav>
<ul>
<li><a href="{{ '/' | relative_url }}">Home</a></li>
<!-- Add more navigation links here -->
</ul>
</nav>
</header>
<!-- _includes/footer.html -->
<footer>
<p>© {{ site.time | date: '%Y' }} {{ site.title }}</p>
</footer>
Sass Files:
In _sass/
, create partials for styles. For example:
// _sass/_variables.scss
$primary-color: #333;
// _sass/_base.scss
body {
font-family: Arial, sans-serif;
color: $primary-color;
}
Main Stylesheet:
Create a main stylesheet in assets/css/
that imports these partials:
// assets/css/styles.scss
@import "variables";
@import "base";
JavaScript Files:
Place your JavaScript files in assets/js/
.
_config.yml: Update your site configuration:
title: My Jekyll Theme
description: A custom Jekyll theme
Gemfile: Add theme dependencies (if any):
gem "jekyll-theme-my-theme", path: "."
Convert Site to Theme: Modify your theme to follow Jekyll theme guidelines:
# _config.yml
theme: "jekyll-theme-my-theme"
Theme Gem Specification:
Create a my-theme.gemspec
file:
Gem::Specification.new do |spec|
spec.name = "jekyll-theme-my-theme"
spec.version = "0.1.0"
spec.summary = "A custom Jekyll theme"
spec.license = "MIT"
spec.files = Dir.glob("**/*").reject { |f| f.include?("vendor/") || f.include?("node_modules/") }
spec.add_runtime_dependency "jekyll", "~> 4.0"
end
Build and Install the Theme: Build the gem and install it locally:
gem build my-theme.gemspec
gem install jekyll-theme-my-theme-0.1.0.gem
Publish to GitHub: Push your theme to a GitHub repository.
Release on RubyGems: To make your theme available to others, you can publish it to RubyGems. Follow the RubyGems guidelines for publishing a gem.
Test Your Theme: Create a new Jekyll site and apply your theme to ensure it works correctly.
Document Your Theme:
Provide clear documentation on how to use and customize your theme in the README.md
.
By following these steps, you can create a Jekyll theme from scratch, package it, and share it with others.