Search code examples
pelican

How can I get Pelican to use the save_as meta to also populate the canonical og:url head info?


Using current Pelican and the pelican-bootstrap3 theme.

Currently I use save_as to set the article which should be the current landing page. When I do that Facebook throws an error because the canonical url isn't there.

So if the normal URL would be https://some.site/my_page.html but the save_as yields https://some.site/index.html

My .rst file meta

:save_as: index.html

The header contains.

<head>
    <title>My Page</title>
    <!-- Using the latest rendering mode for IE -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">



<link rel="canonical" href="/my_page.html">

        <meta name="author" content="me" />
        <meta name="keywords" content="my_page" />
        <meta name="description" content="Stuff" />

        <meta property="og:site_name" content="Stuff" />
        <meta property="og:type" content="article"/>
        <meta property="og:title" content="my_pagee"/>
        <meta property="og:url" content="/my_page.html"/>
        <meta property="og:description" content="Stuff"/>
        <meta property="article:published_time" content="2020-03-20" />
            <meta property="article:section" content="covid-19" />
            <meta property="article:tag" content="Stuff" />
            <meta property="article:author" content="me" />



    <!-- Bootstrap -->
        <link rel="stylesheet" href="/theme/css/bootstrap.min.css" type="text/css"/>
    <link href="/theme/css/font-awesome.min.css" rel="stylesheet">

    <link href="/theme/css/pygments/native.css" rel="stylesheet">
    <link rel="stylesheet" href="/theme/css/style.css" type="text/css"/>
        <link href="/css/hpt.css" rel="stylesheet">

</head>

Ideally pelican would save the page to the "normal" URL and to the save_as, or change the canonical URL to be the save_as


Solution

  • You have to go to the template which has the og:url meta and set a jinja2 statement like this:

    {% if page.save_as %}
    <meta property="og:url" content="/{{ page.save_as }}"/>
    {% else %}
    <meta property="og:url" content="/{# the previous statement of the theme #}"/>
    {% endif %}