Skip to content

Commit b18b574

Browse files
committed
feat: hybrid esm/cjs
1 parent 8418cef commit b18b574

File tree

8 files changed

+6493
-1383
lines changed

8 files changed

+6493
-1383
lines changed

build.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
rollup: {
5+
emitCJS: true,
6+
},
7+
rootDir: './lib',
8+
outDir: '../dist',
9+
entries: ['index.js'],
10+
declaration: true,
11+
})

dist/index.cjs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const qs = require('query-string');
4+
const isUrl = require('is-url-superb');
5+
const matchHelper = require('posthtml-match-helper');
6+
7+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
8+
9+
const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
10+
const isUrl__default = /*#__PURE__*/_interopDefaultCompat(isUrl);
11+
const matchHelper__default = /*#__PURE__*/_interopDefaultCompat(matchHelper);
12+
13+
const plugin = (config = {}) => (tree) => {
14+
config.strict = typeof config.strict === "boolean" ? config.strict : true;
15+
const process = (node) => {
16+
if (!config || !config.parameters) {
17+
return node;
18+
}
19+
const tags = Array.isArray(config.tags) ? config.tags : ["a"];
20+
const knownAttributes = new Set(config.attributes || ["href", "src", "poster", "srcset", "background"]);
21+
tree.match(matchHelper__default(tags.join(",")), (node2) => {
22+
if (!node2.attrs) {
23+
return node2;
24+
}
25+
const matchingAttribute = Object.keys(node2.attrs).find((key) => knownAttributes.has(key));
26+
if (!matchingAttribute) {
27+
return node2;
28+
}
29+
const url = node2.attrs[matchingAttribute];
30+
const parsed = qs__default.parseUrl(url, config.qs);
31+
if (config.strict && !isUrl__default(parsed.url.trim())) {
32+
return node2;
33+
}
34+
for (const item of Object.keys(config.parameters)) {
35+
parsed.query[item] = config.parameters[item];
36+
}
37+
node2.attrs[matchingAttribute] = qs__default.stringifyUrl(parsed, config.qs);
38+
return node2;
39+
});
40+
return node;
41+
};
42+
return new Promise((resolve) => {
43+
tree.walk(process);
44+
resolve(tree);
45+
});
46+
};
47+
48+
module.exports = plugin;

dist/index.d.cts

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { StringifyOptions } from 'query-string';
2+
3+
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+
}
134+
135+
export type { URLParametersConfig };

dist/index.d.mts

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { StringifyOptions } from 'query-string';
2+
3+
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+
}
134+
135+
export type { URLParametersConfig };

0 commit comments

Comments
 (0)