Skip to content

Commit

Permalink
Mirror draft command to current post command
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmaroli committed Oct 22, 2018
1 parent 2aa4079 commit 54b9911
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
17 changes: 16 additions & 1 deletion lib/jekyll/commands/draft.rb
Expand Up @@ -30,7 +30,9 @@ def self.process(args = [], options = {})

draft = DraftFileInfo.new params

Compose::FileCreator.new(draft, params.force?, params.source).create!
file_creator = Compose::FileCreator.new(draft, params.force?, params.source)
file_creator.create!
Compose::FileEditor.open_editor(file_creator.file_path)
end

class DraftFileInfo < Compose::FileInfo
Expand All @@ -41,6 +43,19 @@ def resource_type
def path
"_drafts/#{file_name}"
end

def content(custom_front_matter = {})
default_front_matter = compose_config["draft_default_front_matter"]
custom_front_matter.merge!(default_front_matter) if default_front_matter.is_a?(Hash)

super(custom_front_matter)
end

private

def compose_config
@compose_config ||= Jekyll.configuration["jekyll_compose"] || {}
end
end
end
end
Expand Down
48 changes: 45 additions & 3 deletions spec/draft_spec.rb
Expand Up @@ -13,6 +13,7 @@

before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
allow(Jekyll::Compose::FileEditor).to receive(:system)
end

after(:each) do
Expand Down Expand Up @@ -79,12 +80,15 @@
context "when a configuration file exists" do
let(:config) { source_dir("_config.yml") }
let(:drafts_dir) { Pathname.new source_dir(File.join("site", "_drafts")) }
let(:config_data) do
%(
source: site
)
end

before(:each) do
File.open(config, "w") do |f|
f.write(%(
source: site
))
f.write(config_data)
end
end

Expand All @@ -97,6 +101,44 @@
capture_stdout { described_class.process(args) }
expect(path).to exist
end

context "configuration is set" do
let(:drafts_dir) { Pathname.new source_dir("_drafts") }
let(:config_data) do
%(
jekyll_compose:
auto_open: true
draft_default_front_matter:
description: my description
category:
)
end

it "creates post with the specified config" do
capture_stdout { described_class.process(args) }
post = File.read(path)
expect(post).to match(%r!description: my description!)
expect(post).to match(%r!category: !)
end

context "env variable EDITOR is set up" do
before { ENV["EDITOR"] = "nano" }

it "opens post in default editor" do
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s)
capture_stdout { described_class.process(args) }
end

context "env variable JEKYLL_EDITOR is set up" do
before { ENV["JEKYLL_EDITOR"] = "nano" }

it "opens post in jekyll editor" do
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s)
capture_stdout { described_class.process(args) }
end
end
end
end
end

context "when source option is set" do
Expand Down
4 changes: 2 additions & 2 deletions spec/post_spec.rb
Expand Up @@ -133,10 +133,10 @@
end

context "env variable EDITOR is set up" do
before { ENV["EDITOR"] = "vim" }
before { ENV["EDITOR"] = "nano" }

it "opens post in default editor" do
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("vim", path.to_s)
expect(Jekyll::Compose::FileEditor).to receive(:run_editor).with("nano", path.to_s)
capture_stdout { described_class.process(args) }
end

Expand Down

0 comments on commit 54b9911

Please sign in to comment.