Not Providing Milkshakes

    TypeScript icon, indicating that this package has built-in type declarations

    2.0.2 • Public • Published

    Rehype URL Inspector

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

    Cross-Platform Compatibility Build Status

    Coverage Status Dependencies

    npm License Buy us a tree


    • Inspect every URL on an HTML page and do whatever you want to, such as:

      • Normalize URLs
      • Check for broken links
      • Replace URLs with different URLs
      • Add attributes (like target="blank") to certain links
    • Finds all types of URLs by default, such as:

      • <a href="">
      • <img src="img/logo.png">
      • <link rel="stylesheet" href="/css/main.css">
      • <link rel="manifest" href="/site.manifest">
      • <meta rel="canonical" content="">
      • <meta property="og:image" content="img/logo.png">
      • <script src="//">
      • <script type="application/ld+json">{"url": ""}</script>
      • <style>body { background: url("/img/background.png"); }</style>
    • You can remove the built-in URL rules

    • You can add your own custom URL rules

    • You can abort the URL search at any time


    This HTML file contains many different types of URLs:

        <link rel="canonical" href="">
        <link rel="manifest" href="/site.webmanifest">
        <link rel="icon" type="image/png" sizes="32x32" href="/img/favicon.png">
        <link rel="stylesheet" type="text/css" href="/css/main.css?v=5">
        <meta name="twitter:url" content="">
        <meta name="twitter:image" content="">
        <script type="application/ld+json">
            "@context": "",
            "headline": "Hello, World!",
            "url": "",
            "image": ""
          body {
            background: #ffffff url("img/background.png") center center no-repeat;
          <a href="/">
            <img src="/img/logo.png"> Hello World
          <a href="//" target="_blank">Lorem ipsum</a> dolor sit amet,
          non dignissim dolor. Sed diam tellus, <a href="some-page.html">malesuada, dictum nulla</a>.
        <script src="//"></script>

    This script reads the example.html file above and finds all the URLs in it. The script uses unified, rehype-parse, rehype-stringify, and to-vfile.

    const unified = require("unified");
    const parse = require("rehype-parse");
    const inspectUrls = require("@jsdevtools/rehype-url-inspector");
    const stringify = require("rehype-stringify");
    const toVFile = require("to-vfile");
    async function example() {
      // Create a Rehype processor with the inspectUrls plugin
      const processor = unified()
        .use(inspectUrls, {
          inspectEach({ url }) {
            // Log each URL
      // Read the example HTML file
      let file = await"example.html");
      // Crawl the HTML file and find all the URLs
      await processor.process(file);

    Running this script produces the following output:


    You can install Rehype URL Inspector via npm.

    npm install @jsdevtools/rehype-url-inspector

    You'll probably want to install unified, rehype-parse, rehype-stringify, and to-vfile as well.

    npm install unified rehype-parse rehype-stringify to-vfile


    Using the URL Inspector plugin requires an understanding of how to use Unified and Rehype. Here is an excelleng guide to learn the basics.

    The URL Inspector plugin works just like any other Rehype plugin. Pass it to the .use() method with an options object.

    const unified = require("unified");
    const inspectUrls = require("@jsdevtools/rehype-url-inspector");
    // Use the Rehype URL Inspector plugin with custom options
    unified().use(inspectUrls, {
      inspect(urls) { ... },      // This function is called once with ALL of the URLs
      inspectEach(url) { ... },   // This function is called for each URL as it's found
      selectors: [
        "a[href]",                // Only search for links, not other types of URLs
        "div[data-image]"         // CSS selectors for custom URL attributes


    Rehype URL Inspector supports the following options:

    Option Type Default Description
    selectors array of strings, objects, and/or functions built-in selectors Selectors indicate where to look for URLs in the document. Each selector can be a CSS attribute selector string, like a[href] or img[src], or a function that accepts a HAST node and returns its URL(s). See extractors.ts for examples.
    keepDefaultSelectors boolean false Whether to keep the default selectors in addition to any custom ones.
    inspect function no-op A function that is called once and receives an array containing all the URLs in the document
    inspectEach function no-op A function that is called for each URL in the document as it's found. Return false to abort the search and skip the rest of the document.

    URL Objects

    The inspectEach() function receives a UrlMatch object. The inspect() function receves an array of UrlMatch objects. Each object has the following properties:

    Property Type Description
    url string The URL that was found
    propertyName string or undefined The name of the HAST node property where the URL was found, such as "src" or "href". If the URL was found in the text content of the node, then propertyName is undefined.
    node object The HAST Element node where the URL was found. You can make changes to this node, such as re-writing the URL, adding additional attributes, etc.
    root object The HAST Root node. This gives you access to the whole document if you need it.
    file object The File object that gives you information about the HTML file itself, such as the path and file name.


    Contributions, enhancements, and bug-fixes are welcome! Open an issue on GitHub and submit a pull request.


    To build the project locally on your computer:

    1. Clone this repo
      git clone

    2. Install dependencies
      npm install

    3. Build the code
      npm run build

    4. Run the tests
      npm test


    Rehype URL Inspector is 100% free and open-source, under the MIT license. Use it however you want.

    This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

    Big Thanks To

    Thanks to these awesome companies for their support of Open Source developers

    Travis CI SauceLabs Coveralls


    npm i @jsdevtools/rehype-url-inspector

    DownloadsWeekly Downloads






    Unpacked Size

    34 kB

    Total Files


    Last publish


    • philsturgeon
    • jamesmessinger