cirosantilli.github.io icon indicating copy to clipboard operation
cirosantilli.github.io copied to clipboard

Fix og:url and canonical URLs by creating toplevel_id_clean variable in index.js

Open Copilot opened this issue 6 months ago • 7 comments

This PR fixes URL generation for og:url meta tags and canonical links by creating a new toplevel_id_clean template variable in JavaScript instead of using Liquid filters.

Problem

When toplevel_id contains .html extensions, URLs would be generated as:

  • https://ourbigbook.com/cirosantilli/article-name.html

But OurBigBook.com expects clean URLs without extensions:

  • https://ourbigbook.com/cirosantilli/article-name

The previous approach used Liquid's replace filter, but this was not the preferred solution.

Solution

Created index.js script that patches the OurBigBook library to automatically generate a toplevel_id_clean template variable:

// Patches this line in the library:
options.template_vars.toplevel_id = toplevel_id
// To also include:
options.template_vars.toplevel_id_clean = toplevel_id.replace(/\.html$/, '')

The template now uses the clean variable directly:

<link rel="canonical" href="https://ourbigbook.com/cirosantilli/{{ toplevel_id_clean }}">
<meta property="og:url" content="https://ourbigbook.com/cirosantilli/{{ toplevel_id_clean }}">

Benefits

  • Clean JavaScript solution: Template variables are generated in JavaScript rather than using Liquid filters
  • Automatic processing: The index.js script acts as a wrapper around the ourbigbook CLI
  • Consistent URLs: Both canonical links and Open Graph URLs use clean paths without extensions
  • Maintainable: Single source of truth for URL cleaning logic

Changes

  • index.js (new): Patches OurBigBook library to add toplevel_id_clean variable
  • ourbigbook.liquid.html: Updated to use {{ toplevel_id_clean }} instead of {{ toplevel_id | replace: ".html", "" }}

This ensures proper Open Graph metadata for social media sharing while maintaining clean, consistent URL patterns across the site.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Aug 25 '25 16:08 Copilot