Skip to content

Commit bffc31b

Browse files
authored
Merge pull request #110 from posthtml/feat/esm
2 parents 8493708 + 7721afd commit bffc31b

File tree

12 files changed

+7003
-18183
lines changed

12 files changed

+7003
-18183
lines changed

.eslintrc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module",
4+
"ecmaVersion": 2023
5+
},
6+
"plugins": ["@typescript-eslint"],
7+
"rules": {
8+
"indent": [2, 2, {"SwitchCase": 1}],
9+
"quotes": [2, "single"],
10+
"linebreak-style": [2, "unix"],
11+
"camelcase": [2, {"properties": "always"}],
12+
"brace-style": [2, "1tbs", {"allowSingleLine": true}]
13+
},
14+
"env": {
15+
"es6": true,
16+
"node": true,
17+
"browser": false
18+
},
19+
"extends": [
20+
"eslint:recommended",
21+
"plugin:@typescript-eslint/recommended"
22+
]
23+
}

.github/dependabot.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ updates:
33
- package-ecosystem: npm
44
directory: "/"
55
schedule:
6-
interval: monthly
7-
time: "22:00"
8-
open-pull-requests-limit: 10
6+
interval: weekly

.github/workflows/nodejs.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ on:
77
push:
88
branches: [main]
99
pull_request:
10-
branches: [main]
1110

1211
jobs:
1312
build:
1413
runs-on: ubuntu-latest
1514

1615
strategy:
1716
matrix:
18-
node-version: [14, 16, 18]
17+
node-version: [18, 20]
1918

2019
steps:
21-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v4
2221
- name: Use Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v1
22+
uses: actions/setup-node@v4
2423
with:
2524
node-version: ${{ matrix.node-version }}
26-
- run: npm install
25+
- run: npm ci
2726
- run: npm test
2827
env:
2928
CI: true

license renamed to LICENSE

File renamed without changes.

readme.md renamed to README.md

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## About
1313

14-
This is a PostHTML plugin that allows you to add parameters to URLs.
14+
This is a PostHTML plugin that allows you to add query string parameters to URLs.
1515

1616
## Install
1717

@@ -22,18 +22,18 @@ npm i posthtml posthtml-url-parameters
2222
## Usage
2323

2424
```js
25-
const posthtml = require('posthtml')
26-
const urlParams = require('posthtml-url-parameters')
25+
import posthtml from 'posthtml'
26+
import urlParams from 'posthtml-url-parameters'
2727

2828
posthtml([
29-
urlParams({
30-
parameters: { foo: 'bar', baz: 'qux' }
31-
})
32-
])
29+
urlParams({
30+
parameters: { foo: 'bar', baz: 'qux' }
31+
})
32+
])
3333
.process('<a href="https://example.com">Test</div>')
3434
.then(result => console.log(result.html)))
3535

36-
// <a href="https://example.com?baz=qux&foo=bar">Test</div>
36+
// <a href="https://example.com?baz=qux&foo=bar">Test</div>
3737
```
3838

3939
## Configuration
@@ -47,12 +47,19 @@ Object containing parameter name (key) and its value.
4747
Example:
4848

4949
```js
50-
require('posthtml-url-parameters')({
51-
parameters: {
52-
utm_source: 'Campaign',
53-
'1stDraft': true
54-
}
55-
})
50+
import posthtml from 'posthtml'
51+
import urlParams from 'posthtml-url-parameters'
52+
53+
posthtml([
54+
urlParams({
55+
parameters: {
56+
utm_source: 'Campaign',
57+
'1stDraft': true
58+
}
59+
})
60+
])
61+
.process('<a href="https://example.com">Test</a>')
62+
.then(result => console.log(result.html))
5663
```
5764

5865
### `tags`
@@ -66,19 +73,30 @@ By default, only URLs inside [known attributes](#attributes) of tags in this arr
6673
Example:
6774

6875
```js
69-
require('posthtml-url-parameters')({
70-
tags: ['a', 'link'],
71-
// ...
72-
})
76+
import posthtml from 'posthtml'
77+
import urlParams from 'posthtml-url-parameters'
78+
79+
posthtml([
80+
urlParams({
81+
tags: ['a', 'img']
82+
})
83+
])
84+
.process(`
85+
<a href="https://example.com">Test</a>
86+
<img src="https://example.com/image.jpg">
87+
`)
88+
.then(result => console.log(result.html))
7389
```
7490

7591
You may use some CSS selectors when specifying tags:
7692

7793
```js
78-
require('posthtml-url-parameters')({
79-
tags: ['a.button', 'a[href*="example.com"]' 'link'],
80-
// ...
81-
})
94+
posthtml([
95+
urlParams({
96+
tags: ['a.button', 'a[href*="example.com"]' 'link'],
97+
})
98+
])
99+
.process(/*...*/)
82100
```
83101

84102
All [`posthtml-match-helper` selectors](https://github.com/posthtml/posthtml-match-helper) are supported.
@@ -114,8 +132,8 @@ By default, the plugin will append query parameters only to valid URLs.
114132
You may disable `strict` mode to append parameters to any string:
115133

116134
```js
117-
const posthtml = require('posthtml')
118-
const urlParams = require('posthtml-url-parameters')
135+
import posthtml from 'posthtml'
136+
import urlParams from 'posthtml-url-parameters'
119137

120138
posthtml([
121139
urlParams({
@@ -138,8 +156,8 @@ Options to pass to `query-string` - see available options [here](https://github.
138156
For example, you may disable encoding:
139157

140158
```js
141-
const posthtml = require('posthtml')
142-
const urlParams = require('posthtml-url-parameters')
159+
import posthtml from 'posthtml'
160+
import urlParams from 'posthtml-url-parameters'
143161

144162
posthtml([
145163
urlParams({
@@ -152,7 +170,7 @@ posthtml([
152170
.process('<a href="https://example.com">Test</a>')
153171
.then(result => console.log(result.html)))
154172

155-
// <a href="https://example.com?foo=@Bar@">Test</a>
173+
// <a href="https://example.com?foo=@Bar@">Test</a>
156174
```
157175

158176
[npm]: https://www.npmjs.com/package/posthtml-url-parameters
@@ -161,5 +179,5 @@ posthtml([
161179
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-url-parameters.svg
162180
[github-ci]: https://github.com/posthtml/posthtml-url-parameters/actions
163181
[github-ci-shield]: https://github.com/posthtml/posthtml-url-parameters/actions/workflows/nodejs.yml/badge.svg
164-
[license]: ./license
182+
[license]: ./LICENSE
165183
[license-shield]: https://img.shields.io/npm/l/posthtml-url-parameters.svg

lib/index.d.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import type {StringifyOptions} from 'query-string';
2+
3+
export type URLParametersConfig = {
4+
/**
5+
Object containing parameter name (key) and its value.
6+
7+
@default undefined
8+
9+
@example
10+
```
11+
import posthtml from 'posthtml'
12+
import urlParams from 'posthtml-url-parameters'
13+
14+
posthtml([
15+
urlParams({
16+
parameters: {
17+
foo: 'bar'
18+
}
19+
})
20+
])
21+
.process('<a href="https://example.com">Test</a>')
22+
.then(result => result.html)
23+
```
24+
*/
25+
parameters: Record<string, string>;
26+
27+
/**
28+
Array of tag names to process.
29+
30+
By default, only URLs inside known attributes of tags in this array will be processed.
31+
32+
@default ['a']
33+
34+
@example
35+
```
36+
import posthtml from 'posthtml'
37+
import urlParams from 'posthtml-url-parameters'
38+
39+
posthtml([
40+
urlParams({
41+
parameters: {
42+
foo: 'bar'
43+
},
44+
tags: ['a', 'img']
45+
})
46+
])
47+
.process(`
48+
<a href="https://example.com">Test</a>
49+
<img src="https://example.com/image.jpg">
50+
`)
51+
.then(result => result.html)
52+
```
53+
*/
54+
tags?: string[];
55+
56+
/**
57+
Array of attributes to process for the given tags.
58+
59+
You may override this with your own list of attributes - the plugin will only process URLs in _these_ attributes.
60+
61+
@default ['href', 'src', 'poster', 'srcset', 'background']
62+
63+
@example
64+
```
65+
import posthtml from 'posthtml'
66+
import urlParams from 'posthtml-url-parameters'
67+
68+
posthtml([
69+
urlParams({
70+
parameters: {
71+
foo: 'bar'
72+
},
73+
attributes: ['data-href']
74+
})
75+
])
76+
.process('<a href="foo.html" data-href="https://example.com">Test</a>')
77+
.then(result => result.html)
78+
```
79+
*/
80+
attributes?: string[];
81+
82+
/**
83+
By default, query parameters are appended only to valid URLs.
84+
85+
Disable strict mode to append parameters to any string.
86+
87+
@default true
88+
89+
@example
90+
```
91+
import posthtml from 'posthtml'
92+
import urlParams from 'posthtml-url-parameters'
93+
94+
posthtml([
95+
urlParams({
96+
parameters: {
97+
foo: 'bar'
98+
},
99+
strict: false
100+
})
101+
])
102+
.process('<a href="example.html">Test</a>')
103+
.then(result => result.html)
104+
```
105+
*/
106+
strict?: boolean;
107+
108+
/**
109+
Options to pass to the `query-string` library.
110+
111+
@default undefined
112+
113+
@example
114+
```
115+
import posthtml from 'posthtml'
116+
import urlParams from 'posthtml-url-parameters'
117+
118+
posthtml([
119+
urlParams({
120+
parameters: {
121+
foo: '@Bar@'
122+
},
123+
qs: {
124+
encode: false
125+
}
126+
})
127+
])
128+
.process('<a href="https://example.com">Test</a>')
129+
.then(result => result.html)
130+
```
131+
*/
132+
qs?: StringifyOptions;
133+
}

lib/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const qs = require('query-string')
2-
const isUrl = require('is-url-superb')
3-
const matchHelper = require('posthtml-match-helper')
1+
import qs from 'query-string'
2+
import isUrl from 'is-url-superb'
3+
import matchHelper from 'posthtml-match-helper'
44

5-
module.exports = (config = {}) => tree => {
5+
const plugin = (config = {}) => tree => {
66
config.strict = typeof config.strict === 'boolean' ? config.strict : true
77

88
const process = node => {
@@ -49,3 +49,5 @@ module.exports = (config = {}) => tree => {
4949
resolve(tree)
5050
})
5151
}
52+
53+
export default plugin

0 commit comments

Comments
 (0)