Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace absoluteUrl filter #230

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -54,7 +54,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": "^3.0.0",
Expand Down Expand Up @@ -94,6 +93,6 @@
"stylelint-order": "^6.0.0"
},
"engines": {
"node": ">=18"
"node": ">=18.17"
}
}