Search code examples
pugejsgithub-pagesstatic-sitehexo

How to add canonical link in hexo blog?


I have setup a hexo blog and configure two top level domain pointing to same instance e.g. domain.com and domain.org

I want to set canonical URL for the entire site. There is plugin for this but I'm not able to understand what to do as I don't know jade or ejs.

Is there any way I can do this with or without modifying the themes?


Solution

  • No, there is no way to this without modifying the theme (or with a theme that already support it).

    Theme is a kind of template for your pages, during site generation the theme is processed and your content is inserted, the result is saved is a .html file. It is easy to customize a theme for just include canonical link.

    Since I don´t know what theme are you using, I will pick the tranquilpeak as example. In the theme source find out layout/_partial/head.ejs, this file is responsible for generate the <head> section of every HTML file in your blog. In this file, after <head> tag you will put the plugin helper <%- autoCanonical(config, page) %>.

    EJS here, is a template language. What is put inside tag <% %> will be processed during site generation and its result will be put into final HTML file. The plugin you mentioned has a helper function called autoCanonical that will be evaluated by Hexo´s EJS pre processor and whatever it returns will go to the HTML.

    EDIT:

    Using theme https://github.com/tufu9441/maupassant-hexo (Jade templates)

    This theme it is based on Jade templates, you shall add the plugin helper somewhere around Line 26 of base.jade file and also a similar modification to this place on base-without-sidebar.jade file

    | !{ autoCanonical(config, page) }
    

    will do the trick.