Skip to content

Commit

Permalink
Merge branch 'master' into daniel/3164
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews authored Dec 22, 2017
2 parents 2d23fb4 + f12d336 commit 8ae1101
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"rules": {
"no-console": "off",
"no-inner-declarations": "off",
"valid-jsdoc": "off",
"require-jsdoc": "off",
"quotes": ["error", "backtick"],
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ root.
## Official plugins

* [gatsby-plugin-catch-links](/packages/gatsby-plugin-catch-links/)
* [gatsby-plugin-client-only-paths](/packages/gatsby-plugin-client-only-paths/)
* [gatsby-plugin-create-client-paths](/packages/gatsby-plugin-create-client-paths/)
* [gatsby-plugin-coffeescript](/packages/gatsby-plugin-coffeescript/)
* [gatsby-plugin-feed](/packages/gatsby-plugin-feed/)
* [gatsby-plugin-glamor](/packages/gatsby-plugin-glamor/)
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-autolink-headers/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-remark-autolink-headers",
"description": "Gatsby plugin to autolink headers in markdown processed by Remark",
"version": "1.4.10",
"version": "1.4.11",
"author": "Kyle Mathews <[email protected]>",
"dependencies": {
"babel-runtime": "^6.26.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-remark-autolink-headers/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const scrollToHash = offsetY => {
// Make sure React has had a change to flush to DOM first.
// Make sure React has had a chance to flush to DOM first.
setTimeout(() => {
const hash = window.location.hash.replace(`#`, ``)
const hash = window.decodeURI(window.location.hash.replace(`#`, ``))
if (hash !== ``) {
const element = document.getElementById(hash)
if (element) {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-autolink-headers/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {

const script = `
document.addEventListener("DOMContentLoaded", function(event) {
var hash = location.hash.replace('#', '')
var hash = window.decodeURI(location.hash.replace('#', ''))
if (hash !== '') {
var element = document.getElementById(hash)
if (element) {
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-remark-custom-blocks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
34 changes: 34 additions & 0 deletions packages/gatsby-remark-custom-blocks/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
31 changes: 31 additions & 0 deletions packages/gatsby-remark-custom-blocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# gatsby-remark-custom-blocks

Adds custom blocks to `MarkdownRemark` using [remark-custom-blocks](https://github.com/zestedesavoir/zmarkdown/tree/master/packages/remark-custom-blocks).

## Install

`npm install --save gatsby-remark-custom-blocks`

## How to use

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: "gatsby-remark-custom-blocks",
options: {
blocks: {
danger: "custom-block-danger",
info: "custom-block-info",
},
},
},
],
},
},
];
```
33 changes: 33 additions & 0 deletions packages/gatsby-remark-custom-blocks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "gatsby-remark-custom-blocks",
"version": "1.0.1",
"description": "Gatsby remark plugin for adding custom blocks in markdown",
"main": "dist/index.js",
"scripts": {
"prebuild": "rimraf dist",
"build": "yarn prebuild && babel --out-dir dist --ignore __tests__ src",
"prepublish": "yarn build"
},
"repository": "https://github.com/AlahmadiQ8/gatsby-remark-custom-blocks",
"keywords": [
"gatsby",
"gatsby-plugin",
"markdown",
"remark"
],
"author": "Mohammad Asad Mohammad <[email protected]>",
"license": "MIT",
"private": false,
"files": [
"dist",
"README.md"
],
"devDependencies": {
"babel-cli": "^6.26.0",
"rimraf": "^2.6.2",
"unist-util-find": "^1.0.1"
},
"dependencies": {
"remark-custom-blocks": "^1.0.6"
}
}
30 changes: 30 additions & 0 deletions packages/gatsby-remark-custom-blocks/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Remark = require(`remark`)
const find = require(`unist-util-find`)
const plugin = require(`../index`)

describe(`gatsby-remark-custom-blocks`, () => {
let remark

beforeEach(() => {
remark = new Remark()
})

it(`errors if missing required plugin options`, () => {
expect(plugin.setParserPlugins).toThrow(`missing required "blocks" option`)
})

it(`creates nodes of blocks given in options`, () => {
const parserPlugins = plugin.setParserPlugins({
blocks: { someType: `test`, anotherType: `another` },
})
const [parser, options] = parserPlugins[0]
remark.use(parser, options)
const markdownAST = remark.parse(`
[[someType]]
| content
[[anotherType]]
| content`)
expect(find(markdownAST, { type: `someTypeCustomBlock` })).toBeTruthy()
expect(find(markdownAST, { type: `anotherTypeCustomBlock` })).toBeTruthy()
})
})
10 changes: 10 additions & 0 deletions packages/gatsby-remark-custom-blocks/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const isNil = require(`lodash/isNil`)
const has = require(`lodash/has`)
const remarkCustomBlocks = require(`remark-custom-blocks`)

module.exports.setParserPlugins = options => {
if (isNil(options) || !has(options, `blocks`)) {
throw Error(`missing required "blocks" option`)
}
return [[remarkCustomBlocks, options.blocks]]
}
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-transformer-remark",
"description": "Gatsby transformer plugin for Markdown using the Remark library and ecosystem",
"version": "1.7.25",
"version": "1.7.26",
"author": "Kyle Mathews <[email protected]>",
"dependencies": {
"babel-runtime": "^6.26.0",
Expand Down
11 changes: 9 additions & 2 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,15 @@ module.exports = (
for (let plugin of pluginOptions.plugins) {
const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (let parserPlugin of requiredPlugin.setParserPlugins()) {
remark = remark.use(parserPlugin)
for (let parserPlugin of requiredPlugin.setParserPlugins(
plugin.pluginOptions
)) {
if (_.isArray(parserPlugin)) {
const [parser, options] = parserPlugin
remark = remark.use(parser, options)
} else {
remark = remark.use(parserPlugin)
}
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions packages/gatsby/cache-dir/__tests__/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import loader from "../loader.js"

describe(`Loader`, () => {
beforeEach(() => {
delete global.__PATH_PREFIX__
delete global.__PREFIX_PATHS__

// Workaround for Node 6 issue: https://github.com/facebook/jest/issues/5159
if (global.hasOwnProperty(`__PATH_PREFIX__`)) global.__PATH_PREFIX__ = undefined
if (global.hasOwnProperty(`__PREFIX_PATHS__`)) global.__PREFIX_PATHS__ = undefined

loader.empty()
loader.addPagesArray([
{
Expand Down Expand Up @@ -68,3 +75,72 @@ describe(`Loader`, () => {
expect(loader.getResources()).toMatchSnapshot()
})
})

describe(`Loader path prefixing`, () => {
let pagesArray

beforeEach(() => {
delete global.__PATH_PREFIX__
delete global.__PREFIX_PATHS__

// Workaround for Node 6 issue: https://github.com/facebook/jest/issues/5159
if (global.hasOwnProperty(`__PATH_PREFIX__`)) global.__PATH_PREFIX__ = undefined
if (global.hasOwnProperty(`__PREFIX_PATHS__`)) global.__PREFIX_PATHS__ = undefined

pagesArray = [
{
path: `/about/`,
componentChunkName: `page-component---src-pages-test-js`,
jsonName: `about.json`,
},
{
path: `/about/me/`,
componentChunkName: `page-component---src-pages-test-js`,
jsonName: `about-me.json`,
},
]

loader.empty()
})

test(`Path prefix present and enabled`, () => {
global.__PATH_PREFIX__ = `/foo`
global.__PREFIX_PATHS__ = true
loader.addPagesArray(pagesArray)
loader.enqueue(`/foo/about/`)
expect(loader.has(`/about/`)).toEqual(true)
expect(loader.has(`/foo/about/`)).toEqual(false)
})

test(`Path prefix present but not enabled`, () => {
global.__PATH_PREFIX__ = `/foo`
delete global.__PREFIX_PATHS__
loader.addPagesArray(pagesArray)

// don't enqueue prefixed paths
loader.enqueue(`/foo/about/`)
expect(loader.has(`/about/`)).toEqual(false)
expect(loader.has(`/foo/about/`)).toEqual(false)

// do enqueue unprefixed paths
loader.enqueue(`/about/`)
expect(loader.has(`/about/`)).toEqual(true)
expect(loader.has(`/foo/about/`)).toEqual(false)
})

test(`Path prefix missing but enabled`, () => {
delete global.__PATH_PREFIX__
global.__PREFIX_PATHS__ = true
loader.addPagesArray(pagesArray)

// don't enqueue prefixed paths
loader.enqueue(`/foo/about/`)
expect(loader.has(`/about/`)).toEqual(false)
expect(loader.has(`/foo/about/`)).toEqual(false)

// do enqueue unprefixed paths
loader.enqueue(`/about/`)
expect(loader.has(`/about/`)).toEqual(true)
expect(loader.has(`/foo/about/`)).toEqual(false)
})
})
23 changes: 23 additions & 0 deletions packages/gatsby/cache-dir/__tests__/strip-prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const stripPrefix = require(`../strip-prefix`).default

describe(`strip-prefix`, () => {
it(`strips a prefix`, () => {
expect(stripPrefix(`/foo/bar/`, `/foo`)).toBe(`/bar/`)
})

it(`strips first instance only`, () => {
expect(stripPrefix(`/foo/foo/bar/`, `/foo`)).toBe(`/foo/bar/`)
})

it(`ignores prefix appearing elsewhere in the string`, () => {
expect(stripPrefix(`/foo/bar/`, `bar`)).toBe(`/foo/bar/`)
})

it(`ignores a non-existent prefix`, () => {
expect(stripPrefix(`/bar`, `/foo`)).toBe(`/bar`)
})

it(`returns input str if no prefix is provided`, () => {
expect(stripPrefix(`/bar`)).toBe(`/bar`)
})
})
4 changes: 3 additions & 1 deletion packages/gatsby/cache-dir/find-page.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// TODO add tests especially for handling prefixed links.
import { matchPath } from "react-router-dom"
import stripPrefix from "./strip-prefix"

const pageCache = {}

module.exports = (pages, pathPrefix = ``) => rawPathname => {
let pathname = decodeURIComponent(rawPathname)

// Remove the pathPrefix from the pathname.
let trimmedPathname = pathname.slice(pathPrefix.length)
let trimmedPathname = stripPrefix(pathname, pathPrefix)

// Remove any hashfragment
if (trimmedPathname.split(`#`).length > 1) {
Expand Down
16 changes: 11 additions & 5 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createElement } from "react"
import pageFinderFactory from "./find-page"
import emitter from "./emitter"
import stripPrefix from "./strip-prefix"
let findPage

let syncRequires = {}
Expand All @@ -14,6 +15,7 @@ let pages = []
// we load all resources for likely-to-be-visited paths.
let pathArray = []
let pathCount = {}
let pathPrefix = ``
let resourcesArray = []
let resourcesCount = {}
const preferDefault = m => (m && m.default) || m
Expand Down Expand Up @@ -153,12 +155,15 @@ const queue = {
resourcesCount = {}
resourcesArray = []
pages = []
pathPrefix = ``
},
addPagesArray: newPages => {
pages = newPages
let pathPrefix = ``
if (typeof __PREFIX_PATHS__ !== `undefined`) {
pathPrefix = __PATH_PREFIX__
if (
typeof __PREFIX_PATHS__ !== `undefined` &&
typeof __PATH_PREFIX__ !== `undefined`
) {
if (__PREFIX_PATHS__ === true) pathPrefix = __PATH_PREFIX__
}
findPage = pageFinderFactory(newPages, pathPrefix)
},
Expand All @@ -168,9 +173,10 @@ const queue = {
addProdRequires: prodRequires => {
asyncRequires = prodRequires
},
dequeue: path => pathArray.pop(),
enqueue: path => {
dequeue: () => pathArray.pop(),
enqueue: rawPath => {
// Check page exists.
const path = stripPrefix(rawPath, pathPrefix)
if (!pages.some(p => p.path === path)) {
return false
}
Expand Down
Loading

0 comments on commit 8ae1101

Please sign in to comment.