From 7206b7f9ef955de3db83e8bcf5404dabe91f7d61 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Wed, 26 Oct 2022 22:06:32 +0530 Subject: [PATCH] Respect user-defined name attribute in documents (#9167) Merge pull request 9167 --- lib/jekyll/drops/document_drop.rb | 5 ++++- lib/jekyll/drops/excerpt_drop.rb | 2 +- test/source/_roles/named.md | 5 +++++ test/source/_roles/unnamed.md | 4 ++++ test/test_document.rb | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/source/_roles/named.md create mode 100644 test/source/_roles/unnamed.md diff --git a/lib/jekyll/drops/document_drop.rb b/lib/jekyll/drops/document_drop.rb index 00e51e20..0cff3741 100644 --- a/lib/jekyll/drops/document_drop.rb +++ b/lib/jekyll/drops/document_drop.rb @@ -12,7 +12,6 @@ module Jekyll 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 @@ -26,6 +25,10 @@ module Jekyll fallback_data["excerpt"].to_s end + def name + fallback_data["name"] || @obj.basename + end + def <=>(other) return nil unless other.is_a? DocumentDrop diff --git a/lib/jekyll/drops/excerpt_drop.rb b/lib/jekyll/drops/excerpt_drop.rb index 64a8aa7d..82d3cdf8 100644 --- a/lib/jekyll/drops/excerpt_drop.rb +++ b/lib/jekyll/drops/excerpt_drop.rb @@ -16,7 +16,7 @@ module Jekyll end def name - @obj.doc.basename + @obj.doc.data["name"] || @obj.doc.basename end end end diff --git a/test/source/_roles/named.md b/test/source/_roles/named.md new file mode 100644 index 00000000..dfb68476 --- /dev/null +++ b/test/source/_roles/named.md @@ -0,0 +1,5 @@ +--- +name: launcher +--- + +`name` defined in front matter. diff --git a/test/source/_roles/unnamed.md b/test/source/_roles/unnamed.md new file mode 100644 index 00000000..c35bf6b3 --- /dev/null +++ b/test/source/_roles/unnamed.md @@ -0,0 +1,4 @@ +--- +--- + +No `name` in front matter. diff --git a/test/test_document.rb b/test/test_document.rb index e980b1f5..f5f5cce6 100644 --- a/test/test_document.rb +++ b/test/test_document.rb @@ -159,6 +159,23 @@ class TestDocument < JekyllUnitTest 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