Giter Club home page Giter Club logo

gatsby-remark-check-links's Introduction

gatsby-remark-check-links

Build Status

A Gatbsy Remark plugin that detects broken links to pages and headings among your website's markdown pages. This is useful if your page slugs or heading IDs are being automatically generated. Heading IDs might be created by gatsby-remark-autolink-headers, for example.

It will provide output about the broken links in the terminal when your site builds and as you make changes to pages. In production, your build will break if there are any broken links.

Installation

$ npm install gatsby-remark-check-links

Usage

Add the plugin to your gatsby-transformer-remark options or the remarkPlugins key in the gatsby-plugin-mdx options, if you're using that.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-transformer-remark',
      options: {
        plugins: [
          'gatsby-remark-autolink-headers',
          'gatsby-remark-check-links'
        ]
      }
    }
  ]
};

If broken links are found, you will see feedback in the console. If you stop seeing console output, you might need to clear your cache. The link checker console output should look something like this:

3 broken links found on /examples/react/
- /foo/bar/
- /intro/#some-hash
- /intro/#some-other-hash
โ €
2 broken links found on /intro/
- /foo/baz/
- /examples/ract/
โ €
5 broken links found

Options

If you need to disable link checking for certain pages, you can supply options to the plugin. There are two options: ignore and exceptions, and while they both expect an array of paths, they work differently.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-transformer-remark',
      options: {
        plugins: [
          'gatsby-remark-autolink-headers',
          {
            resolve: 'gatsby-remark-check-links',
            options: {
              ignore: [
                '/foo/bar',
                '/generated/docs/'
              ],
              exceptions: [
                '/bar/baz/',
                '/dynamic/headings/'
              ]
            }
          }
        ]
      }
    }
  ]
};

ignore

Paths passed to ignore will not have their content checked for broken links. This is useful if you have auto-generated pages where you're certain the links work, but it would be a nusance to correct their formatting every time a new set of pages is generated.

exceptions

Paths passed to exceptions will ensure that any links from other pages to these paths or hashes within them will not count as broken. This is useful if the linked page is created programatically, or if the final rendered version of a markdown page contains headings that aren't available during the MDAST-transforming stage of the build (it could be using some fancy MDX component, for example.)

verbose

Disable logs and warnings in your console by passing false to the verbose option, which is true by default.

Caveats

Once a markdown page has been cached by Gatsby, you won't see any output about its broken links until the file changes or your cache gets cleared. If you want to see link check output for all files every time you run npm start, you can set up a prestart npm script that removes your Gatsby cache directory:

{
  "scripts": {
    "prestart": "gatsby clean"
  }
}

License

MIT

gatsby-remark-check-links's People

Contributors

trevorblades avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gatsby-remark-check-links's Issues

Create replace link similar to ignore or or exceptions

I have a markdown file link, when I build gatsby I want to link to a url but when I use github I want to link to another different prefix url because folder name is different to url name.

Example:
gatsby => ../docs/4.3/my-data/
github => ../guide/4.3/my-data/

Thanks

Doesn't work with gatsby-plugin-mdx

Thanks for this! Works great with gatsby-transformer-remark but not for gatsby-plugin-mdx.

It simply bails as soon as it gets to the .fields existence check (doesn't exist).

To repro:

  1. npm init gatsby
  2. Create src/pages/hello.mdx with [broken](/asdf)
  3. Install gatsby-remark-check-links
  4. Add options: {remarkPlugins: ['gatsby-remark-check-links']} to your gatsby-config.js for the gatsby-plugin-mdx.

Stop develop / build if errors is found

I want to stop the build or develop is the error is found.

Example:

warn 2 broken links found on /4.2/
warn 727:236 ../../../docs/4.2/not-informed-data.html
warn 728:240 ../../../docs/4.2/malformed-data.html

but the build contine.

Thanks.

Does not check Link references

This currently does not work with reference link.

Example

[Broken link][]

[Broken link]: /foo

Expected result

Plugin reports a broken link

Actual

Plugin does not report a broken link.

Proposed solution

Create a new visitor for Definition nodes to test for those locations.

Links to files shown as broken

Hi,

I'm building a blog in which I'm also embedding file downloads using the gatsby-remark-copy-linked-files plugin. This plugin catches the relative links to the file [eenmanszaak-of-bv.xlsx](../../assets/eenmanszaak-of-bv.xlsx), copies the file to the public folder (/assets/) and updates the link in html.

Now your plugin also catches these links and shows them as broken. I tried excluding the assets folder but still all of them are shown as broken.

Folder structure:

|--content
|  |--assets
|  |  |--welke-rechtsvorm.mdx
|  |--eenmanszaak-of-bv.xlsx
|  |  |--welke-rechtsvorm.mdx
|--gatsby-config.js 

gatsby-config.js:

{
  resolve: `gatsby-remark-copy-linked-files`,
  options: {
    destinationDir: `assets`,
    ignoreFileExtensions: [`png`, `jpg`, `jpeg`, `bmp`, `tiff`],
  },
},
{
  resolve: 'gatsby-remark-check-links',
  options: {
    exceptions: [
      '/assets/'
    ]
  }
}

Console output:

warning 1 broken links found on /welke-rechtsvorm/
warning  63:114  ../../assets/eenmanszaak-of-bv.xlsx

Question:
Have I misconfigured the exceptions? Or is this a limitation of your plugin? If so can you add this functionality?

Not always checking links

We have been using this plugin to check links successfully. And it's greatly appreciated. But we ran across an issue where builds work locally (even after gatsby clean) and on branches (without cache), but then broken links break the build after the branch is merged. Any ideas on where the problem may be coming from?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.