Skip to content

Commit

Permalink
Allow remark headings to be queried by level (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds authored and KyleAMathews committed Mar 21, 2017
1 parent 41c5ce6 commit b61b05c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby-typegen-remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0",
"excerpt-html": "^1.2.0",
"fs-extra": "^2.1.2",
"graphql": "^0.8.2",
"hast-util-to-html": "^3.0.0",
"is-relative-url": "^2.0.0",
Expand All @@ -25,6 +27,7 @@
"remark": "^6.2.0",
"remark-html": "^5.0.1",
"sanitize-html": "^1.13.0",
"underscore.string": "^3.3.4",
"unist-util-inspect": "^4.0.0",
"unist-util-select": "^1.5.0",
"unist-util-visit": "^1.1.1"
Expand Down
28 changes: 26 additions & 2 deletions packages/gatsby-typegen-remark/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
GraphQLList,
GraphQLString,
GraphQLInt,
GraphQLEnumType,
} = require("graphql")
const Remark = require("remark")
const select = require("unist-util-select")
Expand Down Expand Up @@ -180,6 +181,18 @@ exports.extendNodeType = ({ args, pluginOptions }) =>
},
})

const HeadingLevels = new GraphQLEnumType({
name: 'HeadingLevels',
values: {
'h1': { value: 1 },
'h2': { value: 2 },
'h3': { value: 3 },
'h4': { value: 4 },
'h5': { value: 5 },
'h6': { value: 6 },
}
});

return resolve({
html: {
type: GraphQLString,
Expand Down Expand Up @@ -208,8 +221,19 @@ exports.extendNodeType = ({ args, pluginOptions }) =>
},
headings: {
type: new GraphQLList(HeadingType),
resolve(markdownNode) {
return getHeadings(markdownNode).then(node => node.headings)
args: {
depth: {
type: HeadingLevels,
},
},
resolve(markdownNode, { depth }) {
return getHeadings(markdownNode).then(node => {
let headings = node.headings;
if (typeof depth === 'number') {
headings = headings.filter(heading => heading.depth === depth);
}
return headings;
})
},
},
timeToRead: {
Expand Down
32 changes: 30 additions & 2 deletions packages/gatsby-typegen-remark/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

bluebird@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c"

boolbase@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
Expand Down Expand Up @@ -645,6 +649,13 @@ form-data@~2.1.1:
combined-stream "^1.0.5"
mime-types "^2.1.12"

fs-extra@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-2.1.2.tgz#046c70163cef9aad46b0e4a7fa467fb22d71de35"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^2.1.0"

fs-readdir-recursive@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560"
Expand Down Expand Up @@ -755,7 +766,7 @@ good-listener@^1.2.0:
dependencies:
delegate "^3.1.1"

graceful-fs@^4.1.2, graceful-fs@^4.1.4:
graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

Expand Down Expand Up @@ -1119,6 +1130,12 @@ json5@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
optionalDependencies:
graceful-fs "^4.1.6"

jsonpointer@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.0.tgz#6661e161d2fc445f19f98430231343722e1fcbd5"
Expand Down Expand Up @@ -1724,6 +1741,10 @@ space-separated-tokens@^1.0.0:
dependencies:
trim "0.0.1"

sprintf-js@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"

sshpk@^1.7.0:
version "1.10.1"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.10.1.tgz#30e1a5d329244974a1af61511339d595af6638b0"
Expand Down Expand Up @@ -1861,6 +1882,13 @@ uid-number@~0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81"

underscore.string@^3.3.4:
version "3.3.4"
resolved "https://registry.yarnpkg.com/underscore.string/-/underscore.string-3.3.4.tgz#2c2a3f9f83e64762fdc45e6ceac65142864213db"
dependencies:
sprintf-js "^1.0.3"
util-deprecate "^1.0.2"

unherit@^1.0.4:
version "1.1.0"
resolved "https://registry.yarnpkg.com/unherit/-/unherit-1.1.0.tgz#6b9aaedfbf73df1756ad9e316dd981885840cd7d"
Expand Down Expand Up @@ -1949,7 +1977,7 @@ user-home@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/user-home/-/user-home-1.1.1.tgz#2b5be23a32b63a7c9deb8d0f28d485724a3df190"

util-deprecate@~1.0.1:
util-deprecate@^1.0.2, util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

Expand Down

0 comments on commit b61b05c

Please sign in to comment.