Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
Add parameters to URLs with PostHTML.
License
posthtml/posthtml-url-parameters
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a PostHTML plugin that allows you to add query string parameters to URLs.
npm i posthtml posthtml-url-parametersimportposthtmlfrom'posthtml'importurlParamsfrom'posthtml-url-parameters'posthtml([urlParams({parameters:{foo:'bar',baz:'qux'}})]).process('<a href="https://example.com">Test</div>').then(result=>console.log(result.html)))// <a href="https://example.com?baz=qux&foo=bar">Test</div>
Type:Object
Default:undefined
Object containing parameter name (key) and its value.
Example:
importposthtmlfrom'posthtml'importurlParamsfrom'posthtml-url-parameters'posthtml([urlParams({parameters:{utm_source:'Campaign','1stDraft':true}})]).process('<a href="https://example.com">Test</a>').then(result=>console.log(result.html))
Type:Array
Default:['a']
Array of tag names to process.
By default, only URLs insideknown attributes of tags in this array will be processed.
Example:
importposthtmlfrom'posthtml'importurlParamsfrom'posthtml-url-parameters'posthtml([urlParams({tags:['a','img']})]).process(` <a href="https://example.com">Test</a> <img src="https://example.com/image.jpg"> `).then(result=>console.log(result.html))
You may use some CSS selectors when specifying tags:
posthtml([urlParams({tags:['a.button','a[href*="example.com"]''link'],})]).process(/*...*/)
Allposthtml-match-helper selectors are supported.
Type:Array
Default:['src', 'href', 'poster', 'srcset', 'background']
Array of attributes to process for the given tags.
You may override this with your own list of attributes - the plugin will only process URLs inthese attributes.
posthtml([urlParams({parameters:{foo:'bar'},attributes:['data-href']})]).process('<a href="foo.html" data-href="https://example.com">Test</a>').then(result=>console.log(result.html)))// <a href="foo.html" data-href="https://example.com?foo=bar">Test</a>
Type:Boolean
Default:false
By default, the plugin will append query parameters only to valid URLs.
You may disablestrict mode to append parameters to any string:
importposthtmlfrom'posthtml'importurlParamsfrom'posthtml-url-parameters'posthtml([urlParams({parameters:{foo:'bar'},strict:false,})]).process('<a href="https://example.com/campaigns/{{ id }}">Details</a>').then(result=>console.log(result.html)))// <a href="https://example.com/campaigns/{{ id }}?foo=bar">Details</a>
Type:Object
Default:undefined
Options to pass toquery-string - see available optionshere.
For example, you may disable encoding:
importposthtmlfrom'posthtml'importurlParamsfrom'posthtml-url-parameters'posthtml([urlParams({parameters:{foo:'@Bar@'},qs:{encode:false}})]).process('<a href="https://example.com">Test</a>').then(result=>console.log(result.html)))// <a href="https://example.com?foo=@Bar@">Test</a>
About
Add parameters to URLs with PostHTML.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.