From f7f1f0a01b9cdc8f391d0511b808643347cf6c13 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sat, 13 Jul 2024 13:05:07 +0300 Subject: [PATCH 1/2] refactor: join relative paths --- lib/index.js | 10 ++++++++-- package-lock.json | 7 +++---- package.json | 3 ++- test/expected/joins.html | 4 ++++ test/fixtures/joins.html | 4 ++++ test/test.js | 14 ++++++++++++++ 6 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 test/expected/joins.html create mode 100644 test/fixtures/joins.html diff --git a/lib/index.js b/lib/index.js index cdcd6ca..ffa7c2b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +import path from 'pathe' import postcss from 'postcss' import isUrl from 'is-url-superb' import { defu as merge } from 'defu' @@ -148,13 +149,18 @@ const plugin = (options = {}) => tree => { } const handleSingleValueAttributes = (node, attribute, value) => { + // Don't prepend to URLs if (isUrl(node.attrs[attribute])) { return node } node.attrs[attribute] = typeof value === 'boolean' - ? options.url + node.attrs[attribute] - : value + node.attrs[attribute] + ? isUrl(options.url) + ? options.url + node.attrs[attribute] + : path.join(options.url, node.attrs[attribute]) + : isUrl(value) + ? value + node.attrs[attribute] + : path.join(value, node.attrs[attribute]) } const prependUrl = (value, url) => { diff --git a/package-lock.json b/package-lock.json index 7868627..d5c8328 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "defu": "^6.1.4", "is-url-superb": "^6.1.0", + "pathe": "^1.1.2", "postcss": "^8.4.35", "postcss-safe-parser": "^7.0.0", "srcset": "^5.0.1" @@ -3351,8 +3352,7 @@ "node_modules/pathe": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz", - "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==", - "dev": true + "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==" }, "node_modules/pathval": { "version": "2.0.0", @@ -7399,8 +7399,7 @@ "pathe": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz", - "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==", - "dev": true + "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==" }, "pathval": { "version": "2.0.0", diff --git a/package.json b/package.json index 216d99b..bdc3021 100644 --- a/package.json +++ b/package.json @@ -24,12 +24,12 @@ "node": ">=18" }, "scripts": { - "dev": "vitest", "build": "unbuild", "release": "npx np", "prepack": "unbuild", "pretest": "npm run lint", "lint": "biome lint ./lib", + "dev": "vitest --coverage", "test": "vitest run --coverage" }, "keywords": [ @@ -48,6 +48,7 @@ "dependencies": { "defu": "^6.1.4", "is-url-superb": "^6.1.0", + "pathe": "^1.1.2", "postcss": "^8.4.35", "postcss-safe-parser": "^7.0.0", "srcset": "^5.0.1" diff --git a/test/expected/joins.html b/test/expected/joins.html new file mode 100644 index 0000000..6423e22 --- /dev/null +++ b/test/expected/joins.html @@ -0,0 +1,4 @@ + + + + diff --git a/test/fixtures/joins.html b/test/fixtures/joins.html new file mode 100644 index 0000000..45d8a1b --- /dev/null +++ b/test/fixtures/joins.html @@ -0,0 +1,4 @@ + + + + diff --git a/test/test.js b/test/test.js index ebe7ac3..f087361 100644 --- a/test/test.js +++ b/test/test.js @@ -112,3 +112,17 @@ test('user-defined tags (array)', t => { tags: ['img', 'script'], }) }) + +test('joins relative paths', t => { + return process(t, 'joins', { + url: 'images', + tags: { + img: { + src: true, + }, + script: { + src: 'assets', + }, + }, + }) +}) From c2e054742ce438e7d709018379561f1d675dda40 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sat, 13 Jul 2024 13:09:42 +0300 Subject: [PATCH 2/2] chore: update readme.md --- README.md | 446 +++++++++++++++++++++++++++--------------------------- 1 file changed, 226 insertions(+), 220 deletions(-) diff --git a/README.md b/README.md index 117d83d..80a5358 100644 --- a/README.md +++ b/README.md @@ -1,220 +1,226 @@ -
- PostHTML -

Base URL

-

Prepend a string to source paths in your HTML

- - [![Version][npm-version-shield]][npm] - [![Build][github-ci-shield]][github-ci] - [![License][license-shield]][license] - [![Downloads][npm-stats-shield]][npm-stats] -
- -## Introduction - -This PostHTML plugin can prepend a string to various HTML attribute values and CSS property values. - -Input: - -```html - -``` - -Output: - -```html - -``` - -Works on the following attributes: - -- `src=""` -- `href=""` -- `srcset=""` -- `poster=""` -- `background=""` - -... and the following CSS properties: - -- `background: url()` -- `background-image: url()` -- `@font-face { src: url() }` - -Both `