Skip to content

Commit

Permalink
Override default jekyll-seo-tag template
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed Oct 10, 2018
1 parent 17ec0f8 commit a9ad6e7
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
219 changes: 219 additions & 0 deletions _includes/seo.html
@@ -0,0 +1,219 @@
<!-- Begin Jekyll SEO tag v{{ seo_tag.version }} -->

{% if page.url == "/" or page.url == "/about/" %}
{% assign seo_homepage_or_about = true %}
{% endif %}

{% if site.url %}
{% assign seo_url = site.url | append: site.baseurl %}
{% endif %}
{% assign seo_url = seo_url | default: site.github.url %}
{% assign seo_site_title = site.title | default: site.name %}

{% if page.title %}
{% assign seo_title = page.title %}
{% assign seo_page_title = page.title %}

{% if seo_site_title %}
{% assign seo_title = seo_title | append:" - " | append: seo_site_title %}
{% endif %}
{% elsif seo_site_title %}
{% assign seo_title = seo_site_title %}
{% assign seo_page_title = seo_site_title %}

{% if site.description %}
{% assign seo_title = seo_title | append:" - " | append: site.description %}
{% endif %}
{% endif %}

{% if page.seo and page.seo.name %}
{% assign seo_name = page.seo.name %}
{% elsif seo_homepage_or_about and site.social and site.social.name %}
{% assign seo_name = site.social.name %}
{% elsif seo_homepage_or_about and seo_site_title %}
{% assign seo_name = seo_site_title %}
{% endif %}
{% if seo_name %}
{% assign seo_name = seo_name | smartify | strip_html | strip_newlines | escape_once %}
{% endif %}

{% if seo_title %}
{% assign seo_title = seo_title | smartify | strip_html | strip_newlines | escape_once | escape_once %}
{% endif %}

{% if seo_site_title %}
{% assign seo_site_title = seo_site_title | smartify | strip_html | strip_newlines | escape_once %}
{% endif %}

{% if seo_page_title %}
{% assign seo_page_title = seo_page_title | smartify | strip_html | strip_newlines | escape_once %}
{% endif %}

{% assign seo_description = page.description | default: page.excerpt | default: site.description %}
{% if seo_description %}
{% assign seo_description = seo_description | markdownify | strip_html | strip_newlines | escape_once %}
{% endif %}

{% assign seo_author = page.author | default: page.authors[0] | default: site.author %}
{% if seo_author %}
{% if seo_author.twitter %}
{% assign seo_author_twitter = seo_author.twitter %}
{% else %}
{% if site.data.authors and site.data.authors[seo_author] %}
{% assign seo_author_twitter = site.data.authors[seo_author].twitter %}
{% else %}
{% assign seo_author_twitter = seo_author %}
{% endif %}
{% endif %}
{% assign seo_author_twitter = seo_author_twitter | replace:"@","" %}
{% endif %}

{% if page.seo and page.seo.type %}
{% assign seo_type = page.seo.type %}
{% elsif seo_homepage_or_about %}
{% assign seo_type = "WebSite" %}
{% elsif page.date %}
{% assign seo_type = "BlogPosting" %}
{% else %}
{% assign seo_type = "WebPage" %}
{% endif %}

{% if page.seo and page.seo.links %}
{% assign seo_links = page.seo.links %}
{% elsif seo_homepage_or_about and site.social and site.social.links %}
{% assign seo_links = site.social.links %}
{% endif %}

{% if site.logo %}
{% assign seo_site_logo = site.logo %}
{% unless seo_site_logo contains "://" %}
{% assign seo_site_logo = seo_site_logo | prepend: seo_url %}
{% endunless %}
{% assign seo_site_logo = seo_site_logo | escape %}
{% endif %}

{% if page.image %}
{% assign seo_page_image = page.image.path | default: page.image.facebook | default: page.image %}
{% unless seo_page_image contains "://" %}
{% assign seo_page_image = seo_page_image | prepend: seo_url %}
{% endunless %}
{% assign seo_page_image = seo_page_image | escape %}
{% endif %}

{% if seo_tag.title and seo_title %}
<title>{{ seo_title }}</title>
{% endif %}

{% if seo_page_title %}
<meta property="og:title" content="{{ seo_page_title }}" />
{% endif %}

{% if seo_description %}
<meta name="description" content="{{ seo_description }}" />
<meta property="og:description" content="{{ seo_description }}" />
{% endif %}

{% if seo_url %}
<link rel="canonical" href="{{ page.url | prepend: seo_url | replace:'/index.html','/' }}" />
<meta property="og:url" content="{{ page.url | prepend: seo_url | replace:'/index.html','/' }}" />
{% endif %}

{% if seo_site_title %}
<meta property="og:site_name" content="{{ seo_site_title }}" />
{% endif %}

{% if seo_page_image %}
<meta property="og:image" content="{{ seo_page_image }}" />
{% if page.image.height %}
<meta property="og:image:height" content="{{ page.image.height }}" />
{% endif %}
{% if page.image.width %}
<meta property="og:image:width" content="{{ page.image.width }}" />
{% endif %}
{% endif %}

{% if page.image.twitter %}
<meta name="twitter:image" content="{{ page.image.twitter | prepend: seo_url | escape }}" />
{% endif %}

{% if page.date %}
<meta property="og:type" content="article" />
<meta property="article:published_time" content="{{ page.date | date_to_xmlschema }}" />
{% endif %}

{% if paginator.previous_page %}
<link rel="prev" href="{{ paginator.previous_page_path | prepend: seo_url }}">
{% endif %}
{% if paginator.next_page %}
<link rel="next" href="{{ paginator.next_page_path | prepend: seo_url }}">
{% endif %}

{% if site.twitter %}
<meta name="twitter:card" content="summary" />

<meta name="twitter:site" content="@{{ site.twitter.username | replace:"@","" }}" />

{% if seo_author_twitter %}
<meta name="twitter:creator" content="@{{ seo_author_twitter }}" />
{% endif %}
{% endif %}

{% if site.facebook %}
{% if site.facebook.admins %}
<meta property="fb:admins" content="{{ site.facebook.admins }}" />
{% endif %}

{% if site.facebook.publisher %}
<meta property="article:publisher" content="{{ site.facebook.publisher }}" />
{% endif %}

{% if site.facebook.app_id %}
<meta property="fb:app_id" content="{{ site.facebook.app_id }}" />
{% endif %}
{% endif %}

{% if site.google_site_verification %}
<meta name="google-site-verification" content="{{ site.google_site_verification }}" />
{% endif %}

<script type="application/ld+json">
{
"@context": "http://schema.org",

{% if seo_type %}
"@type": {{ seo_type | jsonify }},
{% endif %}

{% if seo_name %}
"name": {{ seo_name | jsonify }},
{% endif %}

{% if seo_page_title %}
"headline": {{ seo_page_title | jsonify }},
{% endif %}

{% if seo_page_image %}
"image": {{ seo_page_image | jsonify }},
{% endif %}

{% if page.date %}
"datePublished": {{ page.date | date_to_xmlschema | jsonify }},
{% endif %}

{% if seo_description %}
"description": {{ seo_description | jsonify }},
{% endif %}

{% if seo_site_logo %}
"logo": {{ seo_site_logo | jsonify }},
{% endif %}

{% if seo_links %}
"sameAs": {{ seo_links | jsonify }},
{% endif %}

"url": {{ page.url | prepend: seo_url | replace:'/index.html','/' | jsonify }}
}
</script>

<!-- End Jekyll SEO tag -->
15 changes: 15 additions & 0 deletions _plugins/seo-tag.rb
@@ -0,0 +1,15 @@
module Jekyll
class SeoTag < Liquid::Tag
alias_method :old_template_path, :template_path

def template_path
@template_path ||= begin
override = File.expand_path '../_includes/seo.html', File.dirname(__FILE__)
override unless !File.exists? override
end
@template_path ||= old_template_path
end
end
end

Liquid::Template.register_tag('seo', Jekyll::SeoTag)

3 comments on commit a9ad6e7

@NaanProphet
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @infojunkie, I saw your reply on jekyll/jekyll-seo-tag#308 and tried your workaround above. Not sure why, but it throws me the following error.

jekyll 3.8.6 | Error: undefined method `template_path' for class `Jekyll::SeoTag'

@infojunkie
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't been maintaining this code for a couple of years, so I'm not sure how the plugin evolved in the mean time. You'd have to change the code above to match whatever has happened on the plugin side.

@natsupercoach
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This worked with latest:

require "jekyll"
require "jekyll-seo-tag"

module Jekyll
    class SeoTag < Liquid::Tag
        class << self
            alias_method :old_template_path, :template_path

            def template_path
                @template_path ||= begin
                    override = File.expand_path '../_includes/seo.html', File.dirname(__FILE__)
                    override unless !File.exists? override
                end
                @template_path ||= old_template_path
            end
        end
    end
end

Liquid::Template.register_tag("seo", Jekyll::SeoTag)

Please sign in to comment.