Skip to content

Commit c8618d7

Browse files
authored
Merge pull request #1078 from fpsvogel/filter-external-content-source-files
2 parents a0737c8 + 970b184 commit c8618d7

9 files changed

Lines changed: 64 additions & 3 deletions

File tree

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ GEM
9898
faraday (>= 1, < 3)
9999
faraday-net_http (3.4.2)
100100
net-http (~> 0.5)
101+
ffi (1.17.2-arm64-darwin)
101102
ffi (1.17.2-x86_64-linux-gnu)
102103
fiber-annotation (0.2.0)
103104
fiber-local (1.1.0)
@@ -141,8 +142,11 @@ GEM
141142
minitest-stub_any_instance (1.0.3)
142143
net-http (0.8.0)
143144
uri (>= 0.11.1)
145+
nokogiri (1.18.10-arm64-darwin)
146+
racc (~> 1.4)
144147
nokogiri (1.18.10-x86_64-linux-gnu)
145148
racc (~> 1.4)
149+
nokolexbor (0.6.2-arm64-darwin)
146150
nokolexbor (0.6.2-x86_64-linux)
147151
observer (0.1.2)
148152
ostruct (0.6.3)
@@ -267,6 +271,7 @@ GEM
267271
zeitwerk (2.7.3)
268272

269273
PLATFORMS
274+
arm64-darwin-24
270275
x86_64-linux
271276

272277
DEPENDENCIES

bridgetown-core/lib/bridgetown-core/readers/plugin_content_reader.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def read
2323
end
2424
end
2525

26+
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
2627
def read_content_root(collection_name, content_dir)
2728
collection = site.collections[collection_name]
2829
unless collection
@@ -35,17 +36,31 @@ def read_content_root(collection_name, content_dir)
3536

3637
Find.find(content_dir) do |path|
3738
if File.directory?(path)
38-
Find.prune if EntryFilter::SPECIAL_LEADING_CHAR_REGEX.match?(File.basename(path))
39+
if rejected_by_external_sources_filter?(path, collection_name) ||
40+
EntryFilter::SPECIAL_LEADING_CHAR_REGEX.match?(File.basename(path))
41+
Find.prune
42+
end
43+
3944
next
4045
end
4146

47+
next if rejected_by_external_sources_filter?(path, collection_name)
48+
4249
if File.symlink?(path)
4350
Bridgetown.logger.warn "Plugin content reader:", "Ignored symlinked asset: #{path}"
4451
else
4552
read_content_file(content_dir, path, collection)
4653
end
4754
end
4855
end
56+
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
57+
58+
def rejected_by_external_sources_filter?(path, collection_name)
59+
filter = site.config.external_sources_filters&.dig(collection_name)
60+
return false unless filter
61+
62+
!filter.call(File.basename(path), path)
63+
end
4964

5065
# @param collection [Bridgetown::Collection]
5166
def read_content_file(content_dir, path, collection)

bridgetown-core/lib/bridgetown-core/utils/initializers.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414
end
1515

16-
Bridgetown.initializer :external_sources do |config, contents:|
16+
Bridgetown.initializer :external_sources do |config, contents:, filters: nil|
1717
Bridgetown::ExternalSources = Module.new
1818

1919
contents.each do |coll, path|
@@ -35,6 +35,13 @@
3535
contents.each_value do |path|
3636
config.additional_watch_paths << path
3737
end
38+
39+
if filters
40+
config.external_sources_filters = {}
41+
filters.each do |coll, filter|
42+
config.external_sources_filters[coll] = filter
43+
end
44+
end
3845
end
3946

4047
Bridgetown.initializer :parse_routes do |config|
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This content should be filtered out because it's in a folder that's filtered out.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This content should NOT be filtered out.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This content should be filtered out.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This content should NOT be filtered out.

bridgetown-core/test/features/test_external_sources.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ class TestExternalSources < BridgetownFeatureTest
77
describe "external_sources initializer" do
88
it "renders ERB file" do
99
sources_folder = File.expand_path("../external_sources_folder", __dir__)
10+
other_sources_folder = File.expand_path("../external_sources_folder_not_filtered", __dir__)
1011

1112
create_directory "config"
1213
create_file "config/initializers.rb", <<~RUBY
1314
Bridgetown.configure do |config|
15+
collections do
16+
other_pages do
17+
output true
18+
end
19+
end
20+
1421
init :external_sources do
1522
contents do
1623
pages "#{sources_folder}"
24+
other_pages "#{other_sources_folder}"
25+
end
26+
27+
filters do
28+
pages ->(name, path) { !name.start_with?("skipme_") }
1729
end
1830
end
1931
@@ -35,6 +47,10 @@ class TestExternalSources < BridgetownFeatureTest
3547
assert_file_contains "<head><title>Marked Down</title></head><body><h1 id=\"marked-down\">Marked Down</h1>\n\n<p>This is <strong>Markdown</strong> text. It’s as easy as 1, 2, 3!</p>\n",
3648
"output/marked_down/index.html"
3749
refute_exist "output/.ignore"
50+
refute_exist "output/subfolder/skipme_notes/index.html"
51+
assert_file_contains "This content should NOT be filtered out.", "output/subfolder/notes/index.html"
52+
refute_exist "output/skipme_subfolder"
53+
assert_file_contains "This content should NOT be filtered out.", "output/other_pages/skipme_actually_dont/index.html"
3854
end
3955
end
4056
end

bridgetown-website/src/_docs/content/external-sources.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ top_section: Writing Content
55
category: resources
66
---
77

8-
There are times you may want Bridgetown to include resources you've created which live outside of your site project folder hierarchy. In particular, you may have a folder(s) of Markdown files already saved somewhere and would like those to be included in your site build.
8+
There are times you may want Bridgetown to include resources you've created which live outside of your site project folder hierarchy. In particular, you may have a folder(s) of Markdown files already saved somewhere and would like those to be included in your site build.
99

1010
To add a new content source to your project, use the `external_sources` plugin initializer in your `config/initializers.rb` file:
1111

@@ -43,6 +43,20 @@ init :external_sources do
4343
end
4444
```
4545

46+
If you want to include only certain files from an external source, you can do so in a `filters` block. For example, here is how to add a filter to external sources to the `pages` collection, so as to include only files and folders whose name does not start with an underscore:
47+
48+
```ruby
49+
init :external_sources do
50+
contents do
51+
pages "path/to/folder"
52+
end
53+
54+
filters do
55+
pages ->(name, path) { !name.start_with?("_") }
56+
end
57+
end
58+
```
59+
4660
## Loading Markdown Content from Other Applications
4761

4862
Unlike with standard Bridgetown content files within `src` where you must use front matter if you wish Markdown files get processed as resources instead of static files, Markdown files in external sources don't require any front matter. However, in order to ensure those files are rendered with a layout, you'll need to set up [front matter defaults](/docs/content/front-matter-defaults) (as shown in the example above).

0 commit comments

Comments
 (0)