From 853caf1227f7c72ce33a833748ca49e5af29351a Mon Sep 17 00:00:00 2001 From: Mo Frank Hu Date: Sun, 5 Mar 2017 21:17:11 +0800 Subject: [PATCH] Add draft and post front matter support. able to add draft and post front matter in _config.yml in Jekyll directory. adopted from toshimaru's PR (https://github.com/jekyll/jekyll-compose/pull/41/) --- lib/jekyll-compose/file_info.rb | 4 ++-- lib/jekyll/commands/draft.rb | 5 +++++ lib/jekyll/commands/post.rb | 5 +++++ spec/draft_spec.rb | 2 ++ spec/fixtures/_config.yml | 3 +++ spec/post_spec.rb | 8 ++++++++ 6 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/_config.yml diff --git a/lib/jekyll-compose/file_info.rb b/lib/jekyll-compose/file_info.rb index a0d941c..d21712e 100644 --- a/lib/jekyll-compose/file_info.rb +++ b/lib/jekyll-compose/file_info.rb @@ -9,11 +9,11 @@ def file_name "#{name}.#{params.type}" end - def content + def content(addtional_front_matter = {}) front_matter = YAML.dump({ 'layout' => params.layout, 'title' => params.title, - }) + }.merge(addtional_front_matter)) front_matter + "---\n" end diff --git a/lib/jekyll/commands/draft.rb b/lib/jekyll/commands/draft.rb index 19f7100..d979f64 100644 --- a/lib/jekyll/commands/draft.rb +++ b/lib/jekyll/commands/draft.rb @@ -40,6 +40,11 @@ def resource_type def path "_drafts/#{file_name}" end + + def content + post_front_matter = Jekyll.configuration["post_front_matter"] + post_front_matter ? super(post_front_matter) : super + end end end end diff --git a/lib/jekyll/commands/post.rb b/lib/jekyll/commands/post.rb index dd25667..af5bd01 100644 --- a/lib/jekyll/commands/post.rb +++ b/lib/jekyll/commands/post.rb @@ -55,6 +55,11 @@ def file_name def _date_stamp @params.date.strftime '%Y-%m-%d' end + + def content + post_front_matter = Jekyll.configuration["post_front_matter"] + post_front_matter ? super(post_front_matter) : super + end end end end diff --git a/spec/draft_spec.rb b/spec/draft_spec.rb index 51ff696..c9b0804 100644 --- a/spec/draft_spec.rb +++ b/spec/draft_spec.rb @@ -50,6 +50,8 @@ it 'creates a new draft with the specified layout' do capture_stdout { described_class.process(args, 'layout' => 'other-layout') } expect(File.read(path)).to match(/layout: other-layout/) + expect(File.read(path)).to match(/description: my description/) + expect(File.read(path)).to mathc(/category: /) end context 'when the draft already exists' do diff --git a/spec/fixtures/_config.yml b/spec/fixtures/_config.yml new file mode 100644 index 0000000..c50bb72 --- /dev/null +++ b/spec/fixtures/_config.yml @@ -0,0 +1,3 @@ +post_front_matter: + description: my description + category: \ No newline at end of file diff --git a/spec/post_spec.rb b/spec/post_spec.rb index c7b572c..864628e 100644 --- a/spec/post_spec.rb +++ b/spec/post_spec.rb @@ -5,6 +5,7 @@ let(:datestamp) { Time.now.strftime('%Y-%m-%d') } let(:filename) { "#{datestamp}-a-test-post.md" } let(:path) { posts_dir.join(filename) } + let(:jekyll_config) {YAML.load_file('../fixtures/_config.yml')} before(:all) do FileUtils.mkdir_p source_dir unless File.directory? source_dir @@ -44,6 +45,13 @@ expect(File.read(path)).to match(/layout: other-layout/) end + it 'creates a new page with the specified config' do + expect(Jekyll).to receive(:configuration).and_return(jekyll_config) + capture_stdout { described_class.process(args) } + expect(File.read(path)).to match(/description: my description/) + expect(File.read(path)).to match(/category: /) + end + it 'should write a helpful message when successful' do output = capture_stdout { described_class.process(args) } expect(output).to eql("New post created at _posts/#{filename}.\n")