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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect user-defined name attribute in documents #9167

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/jekyll/drops/document_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class DocumentDrop < Drop
mutable false

delegate_method_as :relative_path, :path
delegate_method_as :basename, :name
private delegate_method_as :data, :fallback_data

delegate_methods :id, :output, :content, :to_s, :relative_path, :url, :date
Expand All @@ -26,6 +25,10 @@ def excerpt
fallback_data["excerpt"].to_s
end

def name
fallback_data["name"] || @obj.basename
end

def <=>(other)
return nil unless other.is_a? DocumentDrop

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/drops/excerpt_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def excerpt
end

def name
@obj.doc.basename
@obj.doc.data["name"] || @obj.doc.basename
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/source/_roles/named.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: launcher
---

`name` defined in front matter.
4 changes: 4 additions & 0 deletions test/source/_roles/unnamed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

No `name` in front matter.
17 changes: 17 additions & 0 deletions test/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ def setup_document_with_dates(filename)
should "output its relative path as path in Liquid" do
assert_equal "_methods/configuration.md", @document.to_liquid["path"]
end

context "when rendered with Liquid" do
should "respect the front matter definition" do
site = fixture_site("collections" => ["roles"]).tap(&:process)
docs = site.collections["roles"].docs

# Ruby context: doc.basename is aliased as doc.to_liquid["name"] by default.

document = docs.detect { |d| d.relative_path == "_roles/unnamed.md" }
assert_equal "unnamed.md", document.basename
assert_equal "unnamed.md", document.to_liquid["name"]

document = docs.detect { |d| d.relative_path == "_roles/named.md" }
assert_equal "named.md", document.basename
assert_equal "launcher", document.to_liquid["name"]
end
end
end

context "a document as part of a collection with front matter defaults" do
Expand Down