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

Add option to exclude specific directories #50

Merged
merged 9 commits into from
Feb 11, 2019
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ relative_links:
collections: false
```

### Excluding files

To exclude specific directories and/or files:

```yml
relative_links:
exclude:
- directory
- file.md
```

### Processing Collections

Setting the `collections` option to `true` enables relative links from collection items (including posts).
Expand Down
34 changes: 27 additions & 7 deletions lib/jekyll-relative-links/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module JekyllRelativeLinks
class Generator < Jekyll::Generator
attr_accessor :site
attr_accessor :site, :config

# Use Jekyll's native relative_url filter
include Jekyll::Filters::URLFilters
Expand All @@ -16,26 +16,28 @@ class Generator < Jekyll::Generator
CONFIG_KEY = "relative_links"
ENABLED_KEY = "enabled"
COLLECTIONS_KEY = "collections"
LOG_KEY = "Relative Links:"

safe true
priority :lowest

def initialize(site)
@site = site
@context = context
def initialize(config)
@config = config
end

def generate(site)
return if disabled?

@site = site
@context = context
return if disabled?

documents = site.pages
documents = site.pages + site.docs_to_write if collections?

documents.each do |document|
next unless markdown_extension?(document.extname)
next if document.is_a?(Jekyll::StaticFile)
next if excluded?(document)

replace_relative_links!(document)
end
Expand Down Expand Up @@ -73,7 +75,7 @@ def link_parts(matches)
end

def context
JekyllRelativeLinks::Context.new(site)
@context ||= JekyllRelativeLinks::Context.new(site)
end

def markdown_extension?(extension)
Expand Down Expand Up @@ -122,7 +124,7 @@ def fragment?(string)
end

def option(key)
site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
config[CONFIG_KEY] && config[CONFIG_KEY][key]
end

def disabled?
Expand All @@ -132,5 +134,23 @@ def disabled?
def collections?
option(COLLECTIONS_KEY) == true
end

def excluded?(document)
return false unless option("exclude")

entry_filter = if document.respond_to?(:collection)
document.collection.entry_filter
else
global_entry_filter
end

entry_filter.glob_include?(option("exclude"), document.relative_path).tap do |excluded|
Jekyll.logger.debug(LOG_KEY, "excluded #{document.relative_path}") if excluded
end
end

def global_entry_filter
@global_entry_filter ||= Jekyll::EntryFilter.new(site)
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/site/another-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
---

# Another page

[Page](page.md)
64 changes: 55 additions & 9 deletions spec/jekyll-relative-links/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# frozen_string_literal: true

RSpec.describe JekyllRelativeLinks::Generator do
let(:site_config) do
overrides["relative_links"] = plugin_config if plugin_config
overrides
end
let(:overrides) { {} }
let(:site) { fixture_site("site", overrides) }
let(:plugin_config) { nil }
let(:site) { fixture_site("site", site_config) }
let(:page) { page_by_path(site, "page.md") }
let(:html_page) { page_by_path(site, "html-page.html") }
let(:another_page) { page_by_path(site, "another-page.md") }
Expand All @@ -12,18 +17,20 @@
let(:item) { doc_by_path(site, "_items/some-item.md") }
let(:item_2) { doc_by_path(site, "_items/some-subdir/another-item.md") }

subject { described_class.new(site) }
subject { described_class.new(site.config) }

before(:each) do
site.reset
site.read
end

it "saves the site" do
expect(subject.site).to eql(site)
it "saves the config" do
expect(subject.config).to eql(site.config)
end

context "detecting markdown" do
before { subject.instance_variable_set "@site", site }

it "knows when an extension is markdown" do
expect(subject.send(:markdown_extension?, ".md")).to eql(true)
end
Expand Down Expand Up @@ -173,20 +180,18 @@
end

context "disabled" do
let(:overrides) { { "relative_links" => { "enabled" => false } } }
let(:plugin_config) { { "enabled" => false } }

it "does not process pages when disabled" do
expect(page.content).to include("[Another Page](another-page.md)")
end
end

context "collections" do
let(:plugin_config) { { "collections" => true } }
let(:overrides) do
{
"relative_links" => {
"collections" => true,
},
"collections" => {
"collections" => {
"items" => {
"permalink" => "/items/:name/",
"output" => true,
Expand Down Expand Up @@ -254,6 +259,47 @@
expect(item.content).to include("[A post](/2016/01/01/test.html)")
end
end

context "excludes" do
let(:excludes) do
[
"another-page.md",
"_posts/2016-01-01-test.md",
"_items/some-subdir/another-item.md",
]
end
let(:plugin_config) { { "collections" => true, "exclude" => excludes } }

context "pages" do
it "includes included pages" do
expect(page.content).to include("[Another Page](/another-page.html)")
end

it "excludes excluded pages" do
expect(another_page.content).to include("[Page](page.md)")
end
end

context "posts" do
it "includes included posts" do
expect(subdir_post.content).to include("[Another Page](/another-page.html)")
end

it "excludes excluded posts" do
expect(post.content).to include("[Another Page](../another-page.md)")
end
end

context "collections" do
it "includes included documents" do
expect(item.content).to include("[Another Page](/another-page.html)")
end

it "excludes excluded documents" do
expect(item_2.content).to include("[Another Page](../../another-page.md)")
end
end
end
end
end

Expand Down