Skip to content

Commit 1978852

Browse files
authored
Merge pull request #50 from magento-devdocs/custom-config-option
Add option to exclude specific directories
2 parents f4a8cab + 7badb83 commit 1978852

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ relative_links:
6363
collections: false
6464
```
6565

66+
### Excluding files
67+
68+
To exclude specific directories and/or files:
69+
70+
```yml
71+
relative_links:
72+
exclude:
73+
- directory
74+
- file.md
75+
```
76+
6677
### Processing Collections
6778

6879
Setting the `collections` option to `true` enables relative links from collection items (including posts).

lib/jekyll-relative-links/generator.rb

+27-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module JekyllRelativeLinks
44
class Generator < Jekyll::Generator
5-
attr_accessor :site
5+
attr_accessor :site, :config
66

77
# Use Jekyll's native relative_url filter
88
include Jekyll::Filters::URLFilters
@@ -16,26 +16,28 @@ class Generator < Jekyll::Generator
1616
CONFIG_KEY = "relative_links"
1717
ENABLED_KEY = "enabled"
1818
COLLECTIONS_KEY = "collections"
19+
LOG_KEY = "Relative Links:"
1920

2021
safe true
2122
priority :lowest
2223

23-
def initialize(site)
24-
@site = site
25-
@context = context
24+
def initialize(config)
25+
@config = config
2626
end
2727

2828
def generate(site)
29+
return if disabled?
30+
2931
@site = site
3032
@context = context
31-
return if disabled?
3233

3334
documents = site.pages
3435
documents = site.pages + site.docs_to_write if collections?
3536

3637
documents.each do |document|
3738
next unless markdown_extension?(document.extname)
3839
next if document.is_a?(Jekyll::StaticFile)
40+
next if excluded?(document)
3941

4042
replace_relative_links!(document)
4143
end
@@ -73,7 +75,7 @@ def link_parts(matches)
7375
end
7476

7577
def context
76-
JekyllRelativeLinks::Context.new(site)
78+
@context ||= JekyllRelativeLinks::Context.new(site)
7779
end
7880

7981
def markdown_extension?(extension)
@@ -122,7 +124,7 @@ def fragment?(string)
122124
end
123125

124126
def option(key)
125-
site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
127+
config[CONFIG_KEY] && config[CONFIG_KEY][key]
126128
end
127129

128130
def disabled?
@@ -132,5 +134,23 @@ def disabled?
132134
def collections?
133135
option(COLLECTIONS_KEY) == true
134136
end
137+
138+
def excluded?(document)
139+
return false unless option("exclude")
140+
141+
entry_filter = if document.respond_to?(:collection)
142+
document.collection.entry_filter
143+
else
144+
global_entry_filter
145+
end
146+
147+
entry_filter.glob_include?(option("exclude"), document.relative_path).tap do |excluded|
148+
Jekyll.logger.debug(LOG_KEY, "excluded #{document.relative_path}") if excluded
149+
end
150+
end
151+
152+
def global_entry_filter
153+
@global_entry_filter ||= Jekyll::EntryFilter.new(site)
154+
end
135155
end
136156
end

spec/fixtures/site/another-page.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
---
33

44
# Another page
5+
6+
[Page](page.md)

spec/jekyll-relative-links/generator_spec.rb

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# frozen_string_literal: true
22

33
RSpec.describe JekyllRelativeLinks::Generator do
4+
let(:site_config) do
5+
overrides["relative_links"] = plugin_config if plugin_config
6+
overrides
7+
end
48
let(:overrides) { {} }
5-
let(:site) { fixture_site("site", overrides) }
9+
let(:plugin_config) { nil }
10+
let(:site) { fixture_site("site", site_config) }
611
let(:page) { page_by_path(site, "page.md") }
712
let(:html_page) { page_by_path(site, "html-page.html") }
813
let(:another_page) { page_by_path(site, "another-page.md") }
@@ -12,18 +17,20 @@
1217
let(:item) { doc_by_path(site, "_items/some-item.md") }
1318
let(:item_2) { doc_by_path(site, "_items/some-subdir/another-item.md") }
1419

15-
subject { described_class.new(site) }
20+
subject { described_class.new(site.config) }
1621

1722
before(:each) do
1823
site.reset
1924
site.read
2025
end
2126

22-
it "saves the site" do
23-
expect(subject.site).to eql(site)
27+
it "saves the config" do
28+
expect(subject.config).to eql(site.config)
2429
end
2530

2631
context "detecting markdown" do
32+
before { subject.instance_variable_set "@site", site }
33+
2734
it "knows when an extension is markdown" do
2835
expect(subject.send(:markdown_extension?, ".md")).to eql(true)
2936
end
@@ -173,20 +180,18 @@
173180
end
174181

175182
context "disabled" do
176-
let(:overrides) { { "relative_links" => { "enabled" => false } } }
183+
let(:plugin_config) { { "enabled" => false } }
177184

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

183190
context "collections" do
191+
let(:plugin_config) { { "collections" => true } }
184192
let(:overrides) do
185193
{
186-
"relative_links" => {
187-
"collections" => true,
188-
},
189-
"collections" => {
194+
"collections" => {
190195
"items" => {
191196
"permalink" => "/items/:name/",
192197
"output" => true,
@@ -254,6 +259,47 @@
254259
expect(item.content).to include("[A post](/2016/01/01/test.html)")
255260
end
256261
end
262+
263+
context "excludes" do
264+
let(:excludes) do
265+
[
266+
"another-page.md",
267+
"_posts/2016-01-01-test.md",
268+
"_items/some-subdir/another-item.md",
269+
]
270+
end
271+
let(:plugin_config) { { "collections" => true, "exclude" => excludes } }
272+
273+
context "pages" do
274+
it "includes included pages" do
275+
expect(page.content).to include("[Another Page](/another-page.html)")
276+
end
277+
278+
it "excludes excluded pages" do
279+
expect(another_page.content).to include("[Page](page.md)")
280+
end
281+
end
282+
283+
context "posts" do
284+
it "includes included posts" do
285+
expect(subdir_post.content).to include("[Another Page](/another-page.html)")
286+
end
287+
288+
it "excludes excluded posts" do
289+
expect(post.content).to include("[Another Page](../another-page.md)")
290+
end
291+
end
292+
293+
context "collections" do
294+
it "includes included documents" do
295+
expect(item.content).to include("[Another Page](/another-page.html)")
296+
end
297+
298+
it "excludes excluded documents" do
299+
expect(item_2.content).to include("[Another Page](../../another-page.md)")
300+
end
301+
end
302+
end
257303
end
258304
end
259305

0 commit comments

Comments
 (0)