diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..db89bdd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: Node Unit Tests +on: + push: + branches-ignore: + - "gh-pages" +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: ["ubuntu-latest", "macos-latest", "windows-latest"] + node: ["18", "20"] + name: Node.js ${{ matrix.node }} on ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Setup node + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + # cache: npm + - run: npm install + - run: npm test +env: + YARN_GPG: no diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 54f9335..0000000 --- a/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: node_js -node_js: - - 6 - - 8 - - 10 - - 12 - - node -script: npm run ci diff --git a/lib/defaultOptions.js b/lib/defaultOptions.js index 24209fd..eee863c 100644 --- a/lib/defaultOptions.js +++ b/lib/defaultOptions.js @@ -2,52 +2,36 @@ const HTTP_EQUIV = "http-equiv"; const REFRESH = "refresh"; -const isHttpEquiv = ({attrs}) => HTTP_EQUIV in attrs && attrs[HTTP_EQUIV].toLowerCase()===REFRESH; - - - -const DEFAULT_OPTIONS = -{ - filter: - { - "*": { itemtype:true }, - a: { href:true, ping:true }, - applet: { archive:true, code:true, codebase:true, object:true, src:true }, - area: { href:true, ping:true }, - audio: { src:true }, - base: { href:true }, - blockquote: { cite:true }, - body: { background:true }, - button: { formaction:true }, - del: { cite:true }, - embed: { src:true }, - form: { action:true }, - frame: { longdesc:true, src:true }, - head: { profile:true }, - html: { manifest:true }, - iframe: { longdesc:true, src:true }, - img: { longdesc:true, src:true, srcset:true }, - input: { formaction:true, src:true }, - ins: { cite:true }, - link: { href:true }, - menuitem: { icon:true }, - meta: { content:isHttpEquiv }, - object: { codebase:true, data:true }, - q: { cite:true }, - script: { src:true }, - source: { src:true, srcset:true }, - table: { background:true }, - tbody: { background:true }, - td: { background:true }, - tfoot: { background:true }, - th: { background:true }, - thead: { background:true }, - tr: { background:true }, - track: { src:true }, - video: { poster:true, src:true } - } +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/lib/index.js b/lib/index.js index 8dfaa04..f9a49f0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -26,7 +26,7 @@ const plugin = options => throw new TypeError("eachURL option must be a function"); } - const allTagsGroup = filter["*"]; + // const tagMatchers = Object.keys(filter).map(tagName => ({ tag: tagName })); // Called by PostHTML return tree => @@ -34,43 +34,44 @@ const plugin = options => const promises = []; tree.walk(node => + // tree.match(tagMatchers, (node) => { - if (node.attrs !== undefined) + if (node.attrs === undefined) { + return node; + } + + const tagGroup = filter[node.tag] || EMPTY_TAG_GROUP; + + Object.keys(node.attrs).forEach(attrName => { - const tagGroup = filter[node.tag] || EMPTY_TAG_GROUP; + const isAcceptedTagAttr = attrName in tagGroup && evaluateValue(tagGroup[attrName], node, attrName); - Object.keys(node.attrs).forEach(attrName => + if (isAcceptedTagAttr) { - const isAcceptedGlobalAttr = attrName in allTagsGroup && evaluateValue(allTagsGroup[attrName], node, attrName); - const isAcceptedTagAttr = attrName in tagGroup && evaluateValue(tagGroup[attrName], node, attrName); - - if (isAcceptedGlobalAttr || isAcceptedTagAttr) + switch (attrName) { - switch (attrName) + case CONTENT_ATTR: + { + promises.push( transformMetaRefresh(node, attrName, eachURL) ); + break; + } + case PING_ATTR: { - 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) ); - } + 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; }); diff --git a/package.json b/package.json index 2d21f63..85fff6b 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,27 @@ { "private": true, - "name": "posthtml-urls", - "description": "PostHTML plugin for transforming URLs.", - "version": "2.0.0-alpha", + "name": "@11ty/posthtml-urls", + "description": "PostHTML plugin for transforming URLs. A fork of posthtml/posthtml-urls.", + "version": "1.0.0", "license": "MIT", - "author": "Steven Vachon (https://svachon.com)", - "repository": "github:posthtml/posthtml-urls", + "author": "Zach Leatherman (https://zachleat.com/)", + "contributors": ["Steven Vachon (https://svachon.com)"], + "repository": "github:11ty/posthtml-urls", "main": "lib", "dependencies": { "evaluate-value": "^2.0.0", "http-equiv-refresh": "^2.0.1", "list-to-array": "^1.1.0", - "object.entries": "^1.1.0", + "object.entries": "^1.1.7", "parse-srcset": "^1.0.2" }, "devDependencies": { "chai": "^4.2.0", "coveralls": "^3.0.6", - "html-tags": "^3.1.0", + "html-tags": "^3.1.1", "mocha": "^6.2.0", "nyc": "^14.1.1", - "posthtml": "~0.11.6" + "posthtml": "~0.16.6" }, "engines": { "node": ">= 6"