Package Exports
- posthtml-url-parameters
- posthtml-url-parameters/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (posthtml-url-parameters) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
About
This is a PostHTML plugin that allows you to add parameters to URLs.
Install
npm i posthtml posthtml-url-parametersUsage
const posthtml = require('posthtml')
const urlParams = require('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>Configuration
parameters
Default: undefined
Object containing parameter name (key) and its value.
Example:
require('posthtml-url-parameters')({
parameters: {
utm_source: 'Campaign',
'1stDraft': true
}
})tags
Default: [a]
Array of tag names to process.
By default, only URLs inside known attributes of tags in this array will be processed.
Example:
require('posthtml-url-parameters')({
tags: ['a', 'link'],
// ...
})You may use some CSS selectors when specifying tags:
require('posthtml-url-parameters')({
tags: ['a.button', 'a[href*="example.com"]' 'link'],
// ...
})All posthtml-match-helper selectors are supported.
attributes
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 in these 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>strict
Default: false
By default, the plugin will append query parameters only to valid URLs.
You may disable strict mode to append parameters to any string:
const posthtml = require('posthtml')
const urlParams = require('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>qs
Default: undefined
Options to pass to query-string - see available options here.
For example, you may disable encoding:
const posthtml = require('posthtml')
const urlParams = require('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>