summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/posthtml-urls
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@11ty/posthtml-urls')
-rw-r--r--node_modules/@11ty/posthtml-urls/LICENSE21
-rw-r--r--node_modules/@11ty/posthtml-urls/README.md48
-rw-r--r--node_modules/@11ty/posthtml-urls/lib/defaultOptions.js37
-rw-r--r--node_modules/@11ty/posthtml-urls/lib/index.js142
-rw-r--r--node_modules/@11ty/posthtml-urls/package.json47
5 files changed, 295 insertions, 0 deletions
diff --git a/node_modules/@11ty/posthtml-urls/LICENSE b/node_modules/@11ty/posthtml-urls/LICENSE
new file mode 100644
index 0000000..16736bd
--- /dev/null
+++ b/node_modules/@11ty/posthtml-urls/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2019 Steven Vachon
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/node_modules/@11ty/posthtml-urls/README.md b/node_modules/@11ty/posthtml-urls/README.md
new file mode 100644
index 0000000..559a6ea
--- /dev/null
+++ b/node_modules/@11ty/posthtml-urls/README.md
@@ -0,0 +1,48 @@
+# `@11ty/posthtml-urls`
+
+PostHTML plugin for transforming URLs. This is a fork of [`posthtml/posthtml-urls`](https://github.com/posthtml/posthtml-urls).
+
+
+## Installation
+
+[Node.js](http://nodejs.org) `>= 6` is required. To install, type this at the command line:
+
+```shell
+npm install @11ty/posthtml-urls
+```
+
+
+## Usage
+
+```js
+const posthtml = require('posthtml');
+const urls = require('@11ty/posthtml-urls');
+
+const options = {
+ eachURL: (url, attr, tagName) => `http://domain.com/${url}`
+};
+
+posthtml()
+ .use( urls(options) )
+ .process('<a href="link.html">link</a>')
+ .then(result => console.log(result.html));
+//-> <a href="http://domain.com/link.html">link</a>
+```
+
+
+## Options
+
+### `eachURL`
+Type: `Function`
+Default value: `undefined`
+A callback function ran for each URL value found. You can return either a synchronous value or a `Promise`.
+
+### `filter`
+Type: `Object`
+Default value: [`{…}`](https://github.com/posthtml/posthtml-urls/blob/master/lib/defaultOptions.js)
+The elements and attributes for which to search. An attribute value can optionally be a function, for deeper filtering.
+
+
+## FAQ
+1. **How can I filter `<style>` elements and `style` attributes?**
+Use [posthtml-postcss](https://npmjs.com/posthtml-postcss) and [postcss-url](https://npmjs.com/postcss-url).
diff --git a/node_modules/@11ty/posthtml-urls/lib/defaultOptions.js b/node_modules/@11ty/posthtml-urls/lib/defaultOptions.js
new file mode 100644
index 0000000..eee863c
--- /dev/null
+++ b/node_modules/@11ty/posthtml-urls/lib/defaultOptions.js
@@ -0,0 +1,37 @@
+"use strict";
+const HTTP_EQUIV = "http-equiv";
+const REFRESH = "refresh";
+
+const isHttpEquiv = ({ attrs }) => {
+ return attrs && (HTTP_EQUIV in attrs) && attrs[HTTP_EQUIV].toLowerCase() === REFRESH;
+};
+
+// Fork: pruned some deprecated tag/attribute combos here.
+const DEFAULT_OPTIONS = {
+ filter: {
+ a: { href: true, ping: true },
+ area: { href: true, ping: true },
+ audio: { src: true },
+ base: { href: true },
+ blockquote: { cite: true },
+ button: { formaction: true },
+ del: { cite: true },
+ embed: { src: true },
+ form: { action: true },
+ iframe: { src: true },
+ img: { src: true, srcset: true },
+ input: { formaction: true, src: true },
+ ins: { cite: true },
+ link: { href: true },
+ meta: { content: isHttpEquiv },
+ object: { data: true },
+ q: { cite: true },
+ script: { src: true },
+ source: { src: true, srcset: true },
+ track: { src: true },
+ // video does not yet have srcset: https://scottjehl.com/posts/using-responsive-video/
+ video: { poster: true, src: true },
+ },
+};
+
+module.exports = DEFAULT_OPTIONS;
diff --git a/node_modules/@11ty/posthtml-urls/lib/index.js b/node_modules/@11ty/posthtml-urls/lib/index.js
new file mode 100644
index 0000000..f9a49f0
--- /dev/null
+++ b/node_modules/@11ty/posthtml-urls/lib/index.js
@@ -0,0 +1,142 @@
+"use strict";
+const DEFAULT_OPTIONS = require("./defaultOptions");
+const evaluateValue = require("evaluate-value");
+const list2Array = require("list-to-array");
+const parseMetaRefresh = require("http-equiv-refresh");
+const parseSrcset = require("parse-srcset");
+
+const CONTENT_ATTR = "content";
+const PING_ATTR = "ping";
+const SRCSET_ATTR = "srcset";
+
+const DELIMITER = ",";
+const EMPTY_STRING = "";
+const EMPTY_TAG_GROUP = Object.freeze({});
+const FUNCTION_TYPE = "function";
+const PRETTY_DELIMITER = ", ";
+
+
+
+const plugin = options =>
+{
+ const {eachURL, filter} = Object.assign({}, DEFAULT_OPTIONS, options);
+
+ if (typeof eachURL !== FUNCTION_TYPE)
+ {
+ throw new TypeError("eachURL option must be a function");
+ }
+
+ // const tagMatchers = Object.keys(filter).map(tagName => ({ tag: tagName }));
+
+ // Called by PostHTML
+ return tree =>
+ {
+ const promises = [];
+
+ tree.walk(node =>
+ // tree.match(tagMatchers, (node) =>
+ {
+ if (node.attrs === undefined) {
+ return node;
+ }
+
+ const tagGroup = filter[node.tag] || EMPTY_TAG_GROUP;
+
+ Object.keys(node.attrs).forEach(attrName =>
+ {
+ const isAcceptedTagAttr = attrName in tagGroup && evaluateValue(tagGroup[attrName], node, attrName);
+
+ if (isAcceptedTagAttr)
+ {
+ switch (attrName)
+ {
+ case CONTENT_ATTR:
+ {
+ promises.push( transformMetaRefresh(node, attrName, eachURL) );
+ break;
+ }
+ case PING_ATTR:
+ {
+ promises.push( transformCommaSeparated(node, attrName, eachURL) );
+ break;
+ }
+ case SRCSET_ATTR:
+ {
+ promises.push( transformSrcset(node, attrName, eachURL) );
+ break;
+ }
+ default:
+ {
+ promises.push( transformDefault(node, attrName, eachURL) );
+ }
+ }
+ }
+ });
+
+ return node;
+ });
+
+ return Promise.all(promises).then(() => tree);
+ };
+};
+
+
+
+const transformCommaSeparated = ({attrs, tag}, attrName, transformer) =>
+{
+ const urls = list2Array( attrs[attrName], DELIMITER );
+
+ if (urls.length > 0)
+ {
+ const promises = urls.map(value => Promise.resolve( transformer(value, attrName, tag) ));
+
+ return Promise.all(promises).then(newUrls => attrs[attrName] = newUrls.join(PRETTY_DELIMITER));
+ }
+};
+
+
+
+const transformDefault = ({attrs, tag}, attrName, transformer) =>
+{
+ return Promise.resolve( transformer( attrs[attrName], attrName, tag ) )
+ .then(newUrl => attrs[attrName] = newUrl);
+};
+
+
+
+const transformMetaRefresh = ({attrs, tag}, attrName, transformer) =>
+{
+ const {timeout, url} = parseMetaRefresh( attrs[attrName] );
+
+ if (timeout !== null)
+ {
+ return Promise.resolve( transformer(url || "", attrName, tag) )
+ .then(newUrl => attrs[attrName] = `${timeout}; url=${newUrl}`);
+ }
+};
+
+
+
+const transformSrcset = ({attrs, tag}, attrName, transformer) =>
+{
+ const values = parseSrcset( attrs[attrName] );
+
+ if (values.length > 0)
+ {
+ const promises = values.map(({d, h, url, w}) => Promise.resolve( transformer(url, attrName, tag) )
+ .then(newUrl =>
+ {
+ d = d !== undefined ? ` ${d}x` : EMPTY_STRING;
+ h = h !== undefined ? ` ${h}h` : EMPTY_STRING;
+ w = w !== undefined ? ` ${w}w` : EMPTY_STRING;
+
+ return `${newUrl}${w}${h}${d}`;
+ }));
+
+ return Promise.all(promises).then(newValues => attrs[attrName] = newValues.join(PRETTY_DELIMITER));
+ }
+};
+
+
+
+module.exports = plugin;
diff --git a/node_modules/@11ty/posthtml-urls/package.json b/node_modules/@11ty/posthtml-urls/package.json
new file mode 100644
index 0000000..0c085cb
--- /dev/null
+++ b/node_modules/@11ty/posthtml-urls/package.json
@@ -0,0 +1,47 @@
+{
+ "name": "@11ty/posthtml-urls",
+ "version": "1.0.1",
+ "description": "PostHTML plugin for transforming URLs. A fork of posthtml/posthtml-urls.",
+ "publishConfig": {
+ "access": "public"
+ },
+ "main": "lib",
+ "license": "MIT",
+ "author": "Zach Leatherman <null@zachleat.com> (https://zachleat.com/)",
+ "contributors": ["Steven Vachon <contact@svachon.com> (https://svachon.com)"],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/11ty/eleventy-posthtml-urls.git"
+ },
+ "dependencies": {
+ "evaluate-value": "^2.0.0",
+ "http-equiv-refresh": "^2.0.1",
+ "list-to-array": "^1.1.0",
+ "parse-srcset": "^1.0.2"
+ },
+ "devDependencies": {
+ "chai": "^4.2.0",
+ "coveralls": "^3.0.6",
+ "html-tags": "^3.1.1",
+ "mocha": "^6.2.0",
+ "nyc": "^14.1.1",
+ "posthtml": "~0.16.6"
+ },
+ "engines": {
+ "node": ">= 6"
+ },
+ "scripts": {
+ "ci": "npm test && nyc report --reporter=text-lcov | coveralls",
+ "posttest": "nyc report --reporter=text-summary --reporter=html",
+ "prepublishOnly": "npm test",
+ "test": "nyc --silent mocha test.js --bail --check-leaks"
+ },
+ "files": [
+ "lib"
+ ],
+ "keywords": [
+ "posthtml",
+ "posthtml-plugin",
+ "url"
+ ]
+}