Skip to content

Commit

Permalink
feat(gatsby-plugin-catch-links): Add ability to exclude links (#15416)
Browse files Browse the repository at this point in the history
* WIP: Catch-links with option to exlude path

refs #14835

* Fix tests
  • Loading branch information
fboechats authored and GatsbyJS Bot committed Aug 13, 2019
1 parent 43f29d9 commit 46f514c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 10 deletions.
8 changes: 7 additions & 1 deletion examples/using-remark/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ module.exports = {
},
`gatsby-transformer-yaml`,
`gatsby-plugin-sharp`,
`gatsby-plugin-catch-links`,
{
resolve: `gatsby-plugin-catch-links`,
options: {
// Links are relative to this directory
excludeRegex: /excluded-link/,
},
},
`gatsby-plugin-glamor`,
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ article using a relative path.

Also, let's link to "[Rendering math equations with KaTeX](https://using-remark.gatsbyjs.org/katex/)" article using an absolute path.

And this [link will be excluded](/excluded-link).

[1]: https://www.gatsbyjs.org/packages/gatsby-remark-copy-linked-files/
[2]: https://www.gatsbyjs.org/packages/gatsby-plugin-catch-links/
15 changes: 15 additions & 0 deletions packages/gatsby-plugin-catch-links/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ npm install --save gatsby-plugin-catch-links
plugins: [`gatsby-plugin-catch-links`]
```

## Plugin Options

**`excludeRegex`** [Regular Expression][optional]

Regular expression for paths to be excluded from being handled by this plugin.

```javascript
{
resolve: `gatsby-plugin-catch-links`,
options: {
excludePattern: /(excluded-link|external)/,
},
},
```

## Examples

- Check out this live example [_Using Remark_](https://using-remark.gatsbyjs.org/copy-linked-files-intercepting-local-links/#intercepting-local-links) to see this plugin in action. The full source code for this example can be found here [here](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark).
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe(`navigation is routed through gatsby if the destination href`, () => {

beforeAll(() => {
hrefHandler = jest.fn()
eventDestroyer = catchLinks.default(window, hrefHandler)
eventDestroyer = catchLinks.default(window, {}, hrefHandler)
})

afterAll(() => {
Expand Down Expand Up @@ -364,7 +364,7 @@ describe(`navigation is routed through browser if resources have failed and the

beforeAll(() => {
hrefHandler = jest.fn()
eventDestroyer = catchLinks.default(window, hrefHandler)
eventDestroyer = catchLinks.default(window, {}, hrefHandler)
global.___failedResources = true
})

Expand Down Expand Up @@ -408,7 +408,7 @@ describe(`pathPrefix is handled if catched link to ${pathPrefix}/article navigat

beforeAll(() => {
hrefHandler = jest.fn()
eventDestroyer = catchLinks.default(window, hrefHandler)
eventDestroyer = catchLinks.default(window, {}, hrefHandler)
})

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe(`gatsby-plugin-catch-links`, () => {
})
it(`calls catchLinks in gatsby-browser's onClientEntry API`, () => {
onClientEntry()
expect(mockedCatchLinks).toHaveBeenCalledWith(window, expect.any(Function))
expect(mockedCatchLinks).toHaveBeenCalledWith(
window,
{},
expect.any(Function)
)
})
})
19 changes: 16 additions & 3 deletions packages/gatsby-plugin-catch-links/src/catch-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ export const hashShouldBeFollowed = (origin, destination) =>
/* Don't catch links pointed to the same page but with a hash. */
destination.pathname === origin.pathname)

export const routeThroughBrowserOrApp = hrefHandler => event => {
export const routeThroughBrowserOrApp = (
hrefHandler,
pluginOptions
) => event => {
if (window.___failedResources) return true

if (userIsForcingNavigation(event)) return true
Expand Down Expand Up @@ -143,6 +146,16 @@ export const routeThroughBrowserOrApp = hrefHandler => event => {

if (hashShouldBeFollowed(origin, destination)) return true

if (pluginOptions.excludeRegex) {
console.log(
`pluginOptions.excludeRegex.test(destination.pathname)`,
pluginOptions.excludeRegex.test(destination.pathname)
)
if (pluginOptions.excludeRegex.test(destination.pathname)) {
return true
}
}

event.preventDefault()

// See issue #8907: destination.pathname already includes pathPrefix added
Expand All @@ -157,8 +170,8 @@ export const routeThroughBrowserOrApp = hrefHandler => event => {
return false
}

export default function(root, cb) {
const clickHandler = routeThroughBrowserOrApp(cb)
export default function(root, pluginOptions, cb) {
const clickHandler = routeThroughBrowserOrApp(cb, pluginOptions)

root.addEventListener(`click`, clickHandler)

Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-catch-links/src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { navigate } from "gatsby"

import catchLinks from "./catch-links"

exports.onClientEntry = () => {
catchLinks(window, href => {
exports.onClientEntry = (_, pluginOptions = {}) => {
catchLinks(window, pluginOptions, href => {
navigate(href)
})
}

0 comments on commit 46f514c

Please sign in to comment.