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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror draft command to current post command #79

Merged
merged 1 commit into from Oct 31, 2018
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
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