Skip to content

Commit afabbcd

Browse files
committed
⚡ Energize
0 parents  commit afabbcd

26 files changed

+8949
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_size = 2
6+
indent_style = space
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: ['https://paypal.me/cossssmin', 'https://www.amazon.co.uk/hz/wishlist/ls/3I8KTU4FLQIZ1']
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
name: "🐛 Bug Report"
3+
about: 'Report a general issue.'
4+
5+
---
6+
7+
## Problem
8+
9+
_Describe your problem._
10+
11+
## Environment
12+
13+
- `posthtml-myplugin` plugin version: #.#.#
14+
- PostHTML version: #.#.#
15+
- Node.js version: #.#.#

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
.nyc_output
3+
*.log

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- latest
4+
- stable

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Cosmin Popovici
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<div align="center">
2+
<img width="150" height="150" title="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
3+
<h1>URL Parameters</h1>
4+
<p>Add parameters to URLs</p>
5+
6+
[![Version][npm-version-shield]][npm]
7+
[![License][license-shield]][license]
8+
[![Build][travis-ci-shield]][travis-ci]
9+
[![Downloads][npm-stats-shield]][npm-stats]
10+
</div>
11+
12+
## About
13+
14+
This is a PostHTML plugin that allows you to add parameters to URLs.
15+
16+
## Install
17+
18+
```
19+
$ npm i posthtml posthtml-url-parameters
20+
```
21+
22+
## Usage
23+
24+
```js
25+
const posthtml = require('posthtml')
26+
const urlParams = require('posthtml-url-parameters')
27+
28+
const parameters = {foo: 'bar', baz: 'qux'}
29+
30+
posthtml([
31+
urlParams({
32+
parameters: parameters
33+
})
34+
])
35+
.process('<a href="https://example.com">Test</div>')
36+
.then(result => console.log(result.html)))
37+
38+
// <a href="https://example.com?baz=qux&foo=bar">Test</div>
39+
```
40+
41+
## Configuration
42+
43+
### `parameters`
44+
45+
Default: `undefined`
46+
47+
Object containing parameter name (key) and its value.
48+
49+
Example:
50+
51+
```js
52+
require('posthtml-url-parameters')({
53+
parameters: {
54+
utm_source: 'Campaign',
55+
'1stDraft': true
56+
}
57+
})
58+
```
59+
60+
### `tags`
61+
62+
Default: `[a]`
63+
64+
Array of tag names to process. Only URLs inside `href=""` attributes of tags in this array will be processed.
65+
66+
Example:
67+
68+
```js
69+
require('posthtml-url-parameters')({
70+
tags: ['a', 'link'],
71+
// ...
72+
})
73+
```
74+
75+
### `options`
76+
77+
Default: `undefined`
78+
79+
Options to pass to `query-string` - see available options [here](https://github.com/sindresorhus/query-string#stringifyobject-options).
80+
81+
For example, you can disable encoding:
82+
83+
```js
84+
const posthtml = require('posthtml')
85+
const urlParams = require('posthtml-url-parameters')
86+
87+
const parameters = {foo: '@Bar@'}
88+
89+
posthtml([
90+
urlParams({
91+
parameters: parameters,
92+
options: {
93+
encode: false
94+
}
95+
})
96+
])
97+
.process('<a href="https://example.com">Test</div>')
98+
.then(result => console.log(result.html)))
99+
100+
// <a href="https://example.com?foo=@Bar@">Test</div>
101+
```
102+
103+
[npm]: https://www.npmjs.com/package/posthtml-url-parameters
104+
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-url-parameters.svg
105+
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-url-parameters
106+
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-url-parameters.svg
107+
[travis-ci]: https://travis-ci.org/cossssmin/posthtml-url-parameters/
108+
[travis-ci-shield]: https://img.shields.io/travis/cossssmin/posthtml-url-parameters/master.svg
109+
[license]: ./LICENSE
110+
[license-shield]: https://img.shields.io/npm/l/posthtml-url-parameters.svg

lib/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const isUrl = require('is-url-superb')
4+
const qs = require('query-string')
5+
6+
module.exports = config => tree => {
7+
const process = node => {
8+
if (!config || !config.parameters) {
9+
return node
10+
}
11+
12+
const tags = config && config.tags ? config.tags : ['a']
13+
14+
if (tags.includes(node.tag) && node.attrs && node.attrs.href) {
15+
const url = node.attrs.href
16+
const parsed = qs.parseUrl(url)
17+
18+
if (!isUrl(parsed.url)) {
19+
return node
20+
}
21+
22+
Object.keys(config.parameters).forEach(item => {
23+
parsed.query[item] = config.parameters[item]
24+
})
25+
26+
node.attrs.href = qs.stringifyUrl(parsed, config.options)
27+
}
28+
29+
return node
30+
}
31+
32+
return new Promise(resolve => {
33+
tree.walk(process)
34+
resolve(tree)
35+
})
36+
}

0 commit comments

Comments
 (0)