Skip to content

Commit

Permalink
Replace absoluteUrl filter with canonicalUrl filter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 18, 2023
1 parent e160cf6 commit db14db4
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ You’re welcome to use the plugin even if your service isn’t considered part

## Requirements

- [Node.js](https://nodejs.org) v18 or later
- [Node.js](https://nodejs.org) v18.17 or later
- [Eleventy](https://www.11ty.dev) v2 or later

## Installation
Expand Down
2 changes: 1 addition & 1 deletion docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This plugin includes the following features:

## Requirements

- [Node.js](https://nodejs.org) v18 or later
- [Node.js](https://nodejs.org) v18.17 or later
- [Eleventy](https://www.11ty.dev) v2.0.0 or later

[Node version manager](https://github.com/nvm-sh/nvm) is recommended if you are working across multiple projects that use different versions of Node.js.
Expand Down
8 changes: 4 additions & 4 deletions eleventy.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ module.exports = function (eleventyConfig) {
url: 'https://x-govuk.github.io/#projects',
name: 'X-GOVUK projects'
},
url: process.env.GITHUB_ACTIONS
? 'https://x-govuk.github.io/govuk-eleventy-plugin/'
: '/',
url:
process.env.GITHUB_ACTIONS &&
'https://x-govuk.github.io/govuk-eleventy-plugin/',
header: {
logotype: 'x-govuk',
productName: 'Eleventy Plugin',
Expand Down Expand Up @@ -82,6 +82,6 @@ module.exports = function (eleventyConfig) {
input: 'docs',
layouts: '../layouts'
},
pathPrefix: process.env.GITHUB_ACTIONS ? '/govuk-eleventy-plugin/' : '/'
pathPrefix: process.env.GITHUB_ACTIONS && '/govuk-eleventy-plugin/'
}
}
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module.exports = function (eleventyConfig, pluginOptions = {}) {
eleventyConfig.addTemplateFormats('scss')

// Filters
eleventyConfig.addFilter(
'canonicalUrl',
require('./lib/filters/canonical-url.js')
)
eleventyConfig.addFilter('date', require('./lib/filters/date.js'))
eleventyConfig.addFilter('includes', require('./lib/filters/includes.js'))
eleventyConfig.addFilter(
Expand Down
10 changes: 5 additions & 5 deletions layouts/base.njk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "govuk/template.njk" %}

{% set assetUrl = assetPath | absoluteUrl(options.url) %}
{% set assetUrl = assetPath | canonicalUrl %}
{% set themeColor = options.themeColour %}

{# Only show default Open Graph image if document does not provide its own #}
Expand Down Expand Up @@ -41,15 +41,15 @@
{% block head %}
<link rel="stylesheet" href="/assets/govuk.css">
{% for stylesheet in options.stylesheets %}
<link rel="stylesheet" href="{{ stylesheet | absoluteUrl(options.url) }}">
<link rel="stylesheet" href="{{ stylesheet | canonicalUrl }}">
{% endfor %}

<meta property="og:url" content="{{ page.url | absoluteUrl(options.url) }}">
{% if options.url %}<meta property="og:url" content="{{ page.url | canonicalUrl }}">{% endif %}
<meta property="og:title" content="{{ title }}">
{% if description %}<meta property="og:description" name="description" content="{{ description | markdown("inline") | striptags(true) }}">{% endif %}
{% if opengraphImage %}<meta name="twitter:card" content="summary_large_image">{% endif %}
{% if opengraphImage.src %}<meta name="twitter:image" content="{{ opengraphImage.src | absoluteUrl(options.url) }}">
<meta property="og:image" content="{{ opengraphImage.src | absoluteUrl(options.url) }}">{% endif %}
{% if opengraphImage.src %}<meta name="twitter:image" content="{{ opengraphImage.src | canonicalUrl }}">
<meta property="og:image" content="{{ opengraphImage.src | canonicalUrl }}">{% endif %}
{% if opengraphImage.alt %}<meta property="og:image:alt" content="{{ opengraphImage.alt }}">{% endif %}
{% endblock %}

Expand Down
31 changes: 31 additions & 0 deletions lib/filters/canonical-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const path = require('node:path')

/**
* Get canonical site URL with resolved path
* @param {string} string - Path to resolve
* @returns {string} Canonical site URL with resolved path
*/
module.exports = function (string) {
const { url } = this.ctx.options

// If plugin options do not provide a site URL, return the path
if (!URL.canParse(url)) {
return string
}

const siteUrl = new URL(url)

// If incoming string is a URL, return that if it’s an external URL
if (URL.canParse(string)) {
const incomingUrl = new URL(string)

if (siteUrl.hostname !== incomingUrl.hostname) {
return string
}
}

// String is a path, so append it to any path on site URL
siteUrl.pathname = path.join(siteUrl.pathname, string)

return siteUrl.href
}
17 changes: 1 addition & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"dependencies": {
"@11ty/eleventy": "^2.0.0",
"@11ty/eleventy-navigation": "^0.3.2",
"@11ty/eleventy-plugin-rss": "^1.1.2",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@x-govuk/govuk-prototype-components": "^2.2.0",
Expand Down Expand Up @@ -89,6 +88,6 @@
"stylelint-config-gds": "^1.0.0"
},
"engines": {
"node": ">=18"
"node": ">=18.17"
}
}
13 changes: 13 additions & 0 deletions test/lib/filters/canonical-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require('assert/strict')

Check failure on line 1 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (18)

'assert' is assigned a value but never used

Check failure on line 1 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (20)

'assert' is assigned a value but never used
const { describe, it } = require('node:test')

Check failure on line 2 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (18)

'it' is assigned a value but never used

Check failure on line 2 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (20)

'it' is assigned a value but never used
const canonicalURL = require('../../../lib/filters/canonical-url.js')

Check failure on line 3 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (18)

'canonicalURL' is assigned a value but never used

Check failure on line 3 in test/lib/filters/canonical-url.js

View workflow job for this annotation

GitHub Actions / Run tests (20)

'canonicalURL' is assigned a value but never used

// TODO: Work out how to pass Nunjucks `this` context to filter for testing
describe('canonicalURL filter', () => {
// it('Returns path if no site options', () => {
// })
// it('Returns URL if hostname does not match that of site', () => {
// })
// it('Returns URL with path added', () => {
// })
})

0 comments on commit db14db4

Please sign in to comment.