Skip to content

Commit 98d6abc

Browse files
authored
Merge pull request #69 from posthtml/refactor-tags
2 parents 034a892 + 31dd647 commit 98d6abc

21 files changed

+125
-62
lines changed

lib/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
'use strict'
2-
3-
const isUrl = require('is-url-superb')
41
const qs = require('query-string')
2+
const isUrl = require('is-url-superb')
3+
const matchHelper = require('posthtml-match-helper')
4+
5+
module.exports = (config = {}) => tree => {
6+
config.strict = typeof config.strict === 'boolean' ? config.strict : true
57

6-
module.exports = config => tree => {
78
const process = node => {
89
if (!config || !config.parameters) {
910
return node
1011
}
1112

1213
const tags = config && config.tags ? config.tags : ['a']
1314

14-
if (tags.includes(node.tag) && node.attrs && node.attrs.href) {
15+
tree.match(matchHelper(tags.join(',')), node => {
1516
const url = node.attrs.href
16-
const parsed = qs.parseUrl(url)
17+
const parsed = qs.parseUrl(url, config.qs)
1718

18-
if (isUrl(parsed.url.trim()) === false) {
19+
if (config.strict && !isUrl(parsed.url.trim())) {
1920
return node
2021
}
2122

@@ -24,7 +25,9 @@ module.exports = config => tree => {
2425
})
2526

2627
node.attrs.href = qs.stringifyUrl(parsed, config.qs)
27-
}
28+
29+
return node
30+
})
2831

2932
return node
3033
}

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"is-url-superb": "^5.0.0",
3131
"posthtml": "^0.16.6",
32+
"posthtml-match-helper": "^1.0.3",
3233
"query-string": "^7.1.1"
3334
},
3435
"devDependencies": {

readme.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ require('posthtml-url-parameters')({
5959

6060
Default: `[a]`
6161

62-
Array of tag names to process. Only URLs inside `href=""` attributes of tags in this array will be processed.
62+
Array of tag names to process.
63+
64+
By default, only URLs inside `href=""` attributes of tags in this array will be processed.
6365

6466
Example:
6567

@@ -70,13 +72,48 @@ require('posthtml-url-parameters')({
7072
})
7173
```
7274

75+
You may use some CSS selectors when specifying tags:
76+
77+
```js
78+
require('posthtml-url-parameters')({
79+
tags: ['a.button', 'a[href*="example.com"]' 'link'],
80+
// ...
81+
})
82+
```
83+
84+
All [`posthtml-match-helper` selectors](https://github.com/posthtml/posthtml-match-helper) are supported.
85+
86+
### `strict`
87+
88+
Default: `false`
89+
90+
By default, the plugin will append query parameters only to valid URLs.
91+
92+
You may disable `strict` mode to append parameters to any string:
93+
94+
```js
95+
const posthtml = require('posthtml')
96+
const urlParams = require('posthtml-url-parameters')
97+
98+
posthtml([
99+
urlParams({
100+
parameters: { foo: 'bar' },
101+
strict: false,
102+
})
103+
])
104+
.process('<a href="https://example.com/campaigns/{{ id }}">Details</div>')
105+
.then(result => console.log(result.html)))
106+
107+
// <a href="https://example.com/campaigns/{{ id }}?foo=bar">Details</div>
108+
```
109+
73110
### `qs`
74111

75112
Default: `undefined`
76113

77114
Options to pass to `query-string` - see available options [here](https://github.com/sindresorhus/query-string#stringifyobject-options).
78115

79-
For example, you can disable encoding:
116+
For example, you may disable encoding:
80117

81118
```js
82119
const posthtml = require('posthtml')

test/expected/appends-existing.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/basic.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/invalid-url.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-config.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-encode.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-sort.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)