From 1af5f6c357e3eae95ee76ee47dad72bcd0e90680 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 25 Nov 2015 08:18:37 -0500 Subject: [PATCH 1/3] properly escape quotes in titles and descriptions --- lib/template.html | 4 ++-- spec/jekyll_seo_tag_spec.rb | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/template.html b/lib/template.html index 1366740..5d92d0b 100644 --- a/lib/template.html +++ b/lib/template.html @@ -18,7 +18,7 @@ {% endif %} {% endif %} {% if seo_title %} - {% assign seo_title = seo_title | markdownify | strip_html | strip_newlines | escape_once %} + {% assign seo_title = seo_title | replace:'"','"' | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} {% endif %} {% if page.description %} @@ -27,7 +27,7 @@ {% assign seo_description = site.description %} {% endif %} {% if seo_description %} - {% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %} + {% assign seo_description = seo_description | replace:'"','"' | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} {% endif %} {% if seo_title %} diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index 9f9d6fa..a351859 100644 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -35,7 +35,14 @@ describe Jekyll::SeoTag do it "escapes titles" do site = site({"title" => 'Jekyll & "Hyde"'}) context = context({ :site => site }) - expect(subject.render(context)).to match(/Jekyll & “Hyde”<\/title>/) + expect(subject.render(context)).to match(/<title>Jekyll & "Hyde"<\/title>/) + end + + it "escapes descriptions" do + site = site({"description" => 'Jekyll & "Hyde"'}) + context = context({ :site => site }) + expected = /<meta name="description" content="Jekyll & "Hyde"" \/>/ + expect(subject.render(context)).to match(expected) end it "uses the page description" do From c255b5398ddac2f2172d674c209007416a36f017 Mon Sep 17 00:00:00 2001 From: Ben Balter <ben.balter@github.com> Date: Wed, 25 Nov 2015 12:55:12 -0600 Subject: [PATCH 2/3] use escape --- lib/template.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/template.html b/lib/template.html index 5d92d0b..d251bf3 100644 --- a/lib/template.html +++ b/lib/template.html @@ -18,7 +18,7 @@ {% endif %} {% endif %} {% if seo_title %} - {% assign seo_title = seo_title | replace:'"','"' | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} + {% assign seo_title = seo_title | escape | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} {% endif %} {% if page.description %} @@ -27,7 +27,7 @@ {% assign seo_description = site.description %} {% endif %} {% if seo_description %} - {% assign seo_description = seo_description | replace:'"','"' | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} + {% assign seo_description = seo_description | escape | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} {% endif %} {% if seo_title %} From 1567a53d4e7cb3ddcf9b8af50d7705bc0c19f93c Mon Sep 17 00:00:00 2001 From: Ben Balter <ben.balter@github.com> Date: Wed, 25 Nov 2015 15:23:59 -0600 Subject: [PATCH 3/3] properly escape smart quotes in title and description --- lib/jekyll-seo-tag.rb | 11 ++++++++++- lib/template.html | 4 ++-- spec/jekyll_seo_tag_spec.rb | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/jekyll-seo-tag.rb b/lib/jekyll-seo-tag.rb index 8b09c1b..f638ed3 100644 --- a/lib/jekyll-seo-tag.rb +++ b/lib/jekyll-seo-tag.rb @@ -5,7 +5,16 @@ module Jekyll def render(context) @context = context - Liquid::Template.parse(template_contents).render!(payload, info).gsub(/[\n\s]{2,}/, "\n") + output = Liquid::Template.parse(template_contents).render!(payload, info) + + # Minify + output.gsub!(/[\n\s]{2,}/, "\n") + + # Encode smart quotes. See https://github.com/benbalter/jekyll-seo-tag/pull/6 + output.gsub!("\u201c", "“") + output.gsub!("\u201d", "”") + + output end private diff --git a/lib/template.html b/lib/template.html index d251bf3..1366740 100644 --- a/lib/template.html +++ b/lib/template.html @@ -18,7 +18,7 @@ {% endif %} {% endif %} {% if seo_title %} - {% assign seo_title = seo_title | escape | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} + {% assign seo_title = seo_title | markdownify | strip_html | strip_newlines | escape_once %} {% endif %} {% if page.description %} @@ -27,7 +27,7 @@ {% assign seo_description = site.description %} {% endif %} {% if seo_description %} - {% assign seo_description = seo_description | escape | markdownify | strip_html | strip_newlines | replace:'"','"' | escape_once %} + {% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %} {% endif %} {% if seo_title %} diff --git a/spec/jekyll_seo_tag_spec.rb b/spec/jekyll_seo_tag_spec.rb index a351859..6b6fbba 100644 --- a/spec/jekyll_seo_tag_spec.rb +++ b/spec/jekyll_seo_tag_spec.rb @@ -35,13 +35,13 @@ describe Jekyll::SeoTag do it "escapes titles" do site = site({"title" => 'Jekyll & "Hyde"'}) context = context({ :site => site }) - expect(subject.render(context)).to match(/<title>Jekyll & "Hyde"<\/title>/) + expect(subject.render(context)).to match(/<title>Jekyll & “Hyde”<\/title>/) end it "escapes descriptions" do site = site({"description" => 'Jekyll & "Hyde"'}) context = context({ :site => site }) - expected = /<meta name="description" content="Jekyll & "Hyde"" \/>/ + expected = /<meta name="description" content="Jekyll & “Hyde”" \/>/ expect(subject.render(context)).to match(expected) end