Skip to content

Commit

Permalink
Add support for readme's with front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
qwtel committed Jun 30, 2018
1 parent 86f43bc commit 403a277
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/jekyll-readme-index/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Generator < Jekyll::Generator
CONFIG_KEY = "readme_index".freeze
ENABLED_KEY = "enabled".freeze
CLEANUP_KEY = "remove_originals".freeze
FRONTMATTER_KEY = "with_frontmatter".freeze

def initialize(site)
@site = site
Expand All @@ -26,6 +27,13 @@ def generate(site)
site.pages << readme.to_page
site.static_files.delete(readme) if cleanup?
end

if with_frontmatter?
readmes_with_frontmatter.each do |readme|
next unless should_be_index?(readme)
readme.update_permalink
end
end
end

private
Expand All @@ -35,6 +43,10 @@ def readmes
site.static_files.select { |file| file.relative_path =~ readme_regex }
end

def readmes_with_frontmatter
site.pages.select { |file| ("/" + file.path) =~ readme_regex }
end

# Should the given readme be the containing directory's index?
def should_be_index?(readme)
return false unless readme
Expand Down Expand Up @@ -70,5 +82,9 @@ def disabled?
def cleanup?
option(CLEANUP_KEY) == true
end

def with_frontmatter?
option(FRONTMATTER_KEY) == true
end
end
end
11 changes: 11 additions & 0 deletions lib/jekyll/static_file_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ def to_page
page
end
end

class Page
def update_permalink
data["permalink"] = File.dirname(url) + "/"
@url = URL.new(
:template => template,
:placeholders => url_placeholders,
:permalink => permalink
).to_s
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/readme-with-frontmatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
layout: page
---

# README
24 changes: 24 additions & 0 deletions spec/jekyll-readme-index/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
let(:overrides) { {} }
let(:site) { fixture_site(fixture, overrides) }
let(:readmes) { subject.send(:readmes) }
let(:readmes_with_frontmatter) { subject.send(:readmes_with_frontmatter) }
let(:dir) { "/" }
let(:index?) { subject.send(:dir_has_index?, dir) }
let(:readme) { readmes.find { |r| r.url =~ %r!#{dir}README\..*!i } }
let(:readme_with_frontmatter) do
readmes_with_frontmatter.find { |r| r.url =~ %r!#{dir}README\..*!i }
end
let(:page) { readme.to_page }
let(:should_be_index?) { subject.send(:should_be_index?, readme) }
let(:index_name) { "index.html" }
Expand Down Expand Up @@ -445,6 +449,26 @@
end
end

context "with frontmatter" do
let(:overrides) { { "readme_index" => { "with_frontmatter" => true } } }

context "with a single README" do
let(:fixture) { "readme-with-frontmatter" }

it "knows there's a readme" do
expect(readme_with_frontmatter).to_not be_nil
expect(readme_with_frontmatter.class).to eql(Jekyll::Page)
expect(readme_with_frontmatter.path).to eql("README.md")
end

it "creates the index page" do
subject.generate(site)
expect(site.pages.map(&:name)).to include("README.md")
expect(site.pages.map(&:url)).to include("/")
end
end
end

context "cleanup" do
let(:overrides) { { "readme_index" => { "remove_originals" => true } } }

Expand Down

0 comments on commit 403a277

Please sign in to comment.