UNPKG

@jsdevtools/rehype-url-inspector

Version:

A rehype plugin to inspect, validate, or rewrite URLs anywhere in an HTML document

48 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.crawl = exports.inspectUrls = void 0; const find_urls_1 = require("./find-urls"); const options_1 = require("./options"); /** * This is a rehype plugin to validate and rewrite URLs anywhere in an HTML document. */ function inspectUrls(opts) { let options = new options_1.NormalizedOptions(opts); return function transformer(root, file) { let { inspect, inspectEach } = options; let urls = []; let keepGoing; for (let node of crawl(root)) { for (let url of find_urls_1.findUrls({ node, root, file }, options)) { if (inspectEach) { keepGoing = inspectEach(url); if (keepGoing !== undefined && !keepGoing) { // Stop crawling and immediately return return root; } } urls.push(url); } } if (inspect) { inspect(urls); } return root; }; } exports.inspectUrls = inspectUrls; /** * Crawls all descendant nodes and returns each one that matches one of the specified tag names. */ function* crawl(node) { if (node.type === "element") { yield node; } if (node.children) { for (let child of node.children) { yield* crawl(child); } } } exports.crawl = crawl; //# sourceMappingURL=rehype-url-inspector.js.map