Skip to content

Commit b3e656c

Browse files
committed
Ensure colons do not break titles
When titles have colons in them, it breaks the liquid template. This encapsulates the titles in string literals if the user wants a title with a colon in it. Closes #38
1 parent 4c32bfe commit b3e656c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/jekyll-compose/file_info.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ def content
1313
<<-CONTENT.gsub /^\s+/, ''
1414
---
1515
layout: #{params.layout}
16-
title: #{params.title}
16+
title: #{yaml_clean_title}
1717
---
1818
CONTENT
1919
end
20+
21+
private
22+
23+
def yaml_clean_title
24+
if params.title.include? ':'
25+
'"' + params.title + '"'
26+
else
27+
params.title
28+
end
29+
end
2030
end

spec/file_info_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
RSpec.describe(Jekyll::Compose::FileInfo) do
2+
describe '#content' do
3+
context 'with a title of only words' do
4+
let(:expected_result) {<<-CONTENT.gsub(/^\s+/, '')
5+
---
6+
layout: post
7+
title: A test arg parser
8+
---
9+
CONTENT
10+
}
11+
12+
let(:parsed_args) { Jekyll::Compose::ArgParser.new(
13+
['A test arg parser'],
14+
{}
15+
)
16+
}
17+
18+
it 'does not wrap the title in quotes' do
19+
file_info = described_class.new parsed_args
20+
expect(file_info.content).to eq(expected_result)
21+
end
22+
end
23+
24+
context 'with a title that includes a colon' do
25+
let(:expected_result) {<<-CONTENT.gsub(/^\s+/, '')
26+
---
27+
layout: post
28+
title: "A test: arg parser"
29+
---
30+
CONTENT
31+
}
32+
33+
let(:parsed_args) { Jekyll::Compose::ArgParser.new(
34+
['A test: arg parser'],
35+
{}
36+
)
37+
}
38+
39+
it 'does wrap the title in quotes' do
40+
file_info = described_class.new parsed_args
41+
expect(file_info.content).to eq(expected_result)
42+
end
43+
end
44+
end
45+
end
46+

0 commit comments

Comments
 (0)