Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Sep 11, 2016
0 parents commit d0475cb
Show file tree
Hide file tree
Showing 17 changed files with 252 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spec/examples.txt
_site
*.gem
Gemfile.lock
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
inherit_gem:
jekyll: .rubocop.yml

AllCops:
Exclude:
- vendor/**/*
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rvm:
- 2.2
before_install: gem install bundler
language: ruby
script: script/cibuild
sudo: false
cache: bundler
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Jekyll Optional Front Matter

A Jekyll plugin to make front matter optional for Markdown files

## What it does

Out of the box, Jekyll requires that any markdown file have YAML front matter (key/value pairs separated by two sets of three dashes) in order to be processed and converted to HTML.

While that behavior may be helpful for large, complex sites, sometimes it's easier to simply add a plain markdown file and have it render without fanfare.

This plugin does just that. Any Markdown file in your site's source will be treated as a Page and rendered as HTML, even if it doesn't have YAML front matter.

## Usage

1. Add the following to your site's Gemfile:

```ruby
gem 'jekyll-optional-front-matter'
```

2. Add the following to your site's config file:

```yml
gems:
- jekyll-optional-front-matter
```
## Disabling
Even if the plugin is enabled (e.g., via the `:jekyll_plugins` group in your Gemfile) you can disable it by adding the following to your site's config:

```yml
require_front_matter: true
```
22 changes: 22 additions & 0 deletions jekyll-optional-front-matter.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# encoding: utf-8

$:.unshift File.expand_path('../lib', __FILE__)
require 'jekyll-optional-front-matter/version'

Gem::Specification.new do |s|
s.name = "jekyll-optional-front-matter"
s.version = JekyllOptionalFrontMatter::VERSION
s.authors = ["Ben Balter"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/benbalter/jekyll-optional-front-matter"
s.summary = "A Jekyll plugin to make front matter optional for Markdown files"

s.files = `git ls-files app lib`.split("\n")
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']
s.license = "MIT"

s.add_runtime_dependency "jekyll", "~> 3.0"
s.add_development_dependency "rspec", "~> 3.5"
s.add_development_dependency "rubocop", "~> 0.40"
end
5 changes: 5 additions & 0 deletions lib/jekyll-optional-front-matter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "jekyll"

module JekyllOptionalFrontMatter
autoload :Generator, "jekyll-optional-front-matter/generator"
end
39 changes: 39 additions & 0 deletions lib/jekyll-optional-front-matter/generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module JekyllOptionalFrontMatter
class Generator < Jekyll::Generator
attr_accessor :site

safe :true
priority :low

def initialize(site)
@site = site
end

def generate(site)
@site = site
return if site.config["require_front_matter"]
site.pages.concat(pages)
end

private

def pages
markdown_files.map { |static_file| page_from_static_file(static_file) }
end

def page_from_static_file(static_file)
base = static_file.instance_variable_get("@base")
dir = static_file.instance_variable_get("@dir")
name = static_file.instance_variable_get("@name")
Jekyll::Page.new(site, base, dir, name)
end

def markdown_files
site.static_files.select { |f| markdown_converter.matches(f.extname) }
end

def markdown_converter
site.find_converter_instance(Jekyll::Converters::Markdown)
end
end
end
3 changes: 3 additions & 0 deletions lib/jekyll-optional-front-matter/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module JekyllOptionalFrontMatter
VERSION = "0.0.1".freeze
end
5 changes: 5 additions & 0 deletions script/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -e

bundle install
7 changes: 7 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

set -e

bundle exec rspec
bundle exec rubocop -S -D
bundle exec gem build jekyll-optional-front-matter.gemspec
1 change: 1 addition & 0 deletions spec/fixtures/site/another-file.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Another file
5 changes: 5 additions & 0 deletions spec/fixtures/site/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
permalink: /
---

# Index
1 change: 1 addition & 0 deletions spec/fixtures/site/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Readme
72 changes: 72 additions & 0 deletions spec/jekyll-optional-front-matter/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
describe JekyllOptionalFrontMatter::Generator do
let(:site) { fixture_site("site") }
let(:generator) { described_class.new(site) }
let(:markdown_converter) { generator.send(:markdown_converter) }
let(:markdown_files) { generator.send(:markdown_files) }
let(:pages) { generator.send(:pages) }

before do
site.reset
site.read
end

it "grabs the markdown converter" do
expect(markdown_converter.class).to eql(Jekyll::Converters::Markdown)
end

it "grabs the markdown files" do
expect(markdown_files.count).to eql(2)
paths = markdown_files.map(&:relative_path)
expect(paths).to include("/readme.md")
expect(paths).to include("/another-file.markdown")
end

it "builds a page from a static file" do
static_file = Jekyll::StaticFile.new(site, site.source, "/", "readme.md")
page = generator.send(:page_from_static_file, static_file)
expect(page.class).to eql(Jekyll::Page)
expect(page.name).to eql("readme.md")
expect(page.content).to eql("# Readme\n")
end

it "builds the array of pages" do
expect(pages.count).to eql(2)
names = pages.map(&:name)
expect(names).to include("readme.md")
expect(names).to include("another-file.markdown")
end

context "generating" do
before { generator.generate(site) }

it "adds the pages to the site" do
expect(site.pages.count).to eql(3)
names = site.pages.map(&:name)
expect(names).to include("readme.md")
expect(names).to include("another-file.markdown")
expect(names).to include("index.md")
end
end

context "when disabled" do
let(:site) { fixture_site("site", { "require_front_matter" => true }) }
context "generating" do
before { generator.generate(site) }

it "doesn't add the pages to the site" do
expect(site.pages.count).to eql(1)
end
end
end

context "when explicitly enabled" do
let(:site) { fixture_site("site", { "require_front_matter" => false }) }
context "generating" do
before { generator.generate(site) }

it "doesn't add the pages to the site" do
expect(site.pages.count).to eql(3)
end
end
end
end
36 changes: 36 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "jekyll-optional-front-matter"

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
config.example_status_persistence_file_path = "spec/examples.txt"

if config.files_to_run.one?
config.default_formatter = "doc"
end

config.order = :random
Kernel.srand config.seed
end

def fixture_path(fixture)
File.expand_path "./fixtures/#{fixture}", File.dirname(__FILE__)
end

def fixture_site(fixture, override = {})
default_config = { "source" => fixture_path(fixture) }
config = Jekyll::Utils.deep_merge_hashes(default_config, override)
config = Jekyll.configuration(config)
Jekyll::Site.new(config)
end

RSpec::Matchers.define :be_an_existing_file do
match { |path| File.exist?(path) }
end

0 comments on commit d0475cb

Please sign in to comment.