Skip to content

Commit

Permalink
Add TOC to GraphQL Schema (gatsbyjs#48)
Browse files Browse the repository at this point in the history
Added
------

* `table-of-contents` to graphql schema
* example using `toc` to kitchen-sink
  • Loading branch information
youngboy authored and ChristopherBiscardi committed Jun 27, 2019
1 parent 3e532ad commit ecf1c59
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/gatsby-plugin-mdx/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const retext = require("retext");
const visit = require("unist-util-visit");
const remove = require("unist-util-remove");
const toString = require("mdast-util-to-string");
const generateTOC = require("mdast-util-toc");
const stripMarkdown = require("strip-markdown");
const grayMatter = require("gray-matter");
const { createMdxAstCompiler } = require("@mdx-js/mdx");
const prune = require("underscore.string/prune");
const mdx = require("./utils/mdx");
const getTableOfContents = require("./utils/get-table-of-content");
const defaultOptions = require("./utils/default-options");

const stripFrontmatter = source => grayMatter(source).content;
Expand Down Expand Up @@ -146,6 +148,21 @@ ${code}`;
return headings;
}
},
tableOfContents: {
type: GraphQLJSON,
args: {
maxDepth: {
type: GraphQLInt,
default: 6
}
},
async resolve(mdxNode, { maxDepth }) {
const ast = await getAST(mdxNode);
const toc = generateTOC(ast, maxDepth);

return getTableOfContents(toc.map, {});
}
},
timeToRead: {
type: GraphQLInt,
async resolve(mdxNode) {
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"json5": "^1.0.1",
"lodash": "^4.17.10",
"mdast-util-to-string": "^1.0.4",
"mdast-util-toc": "^2.0.1",
"remark": "^9.0.0",
"retext": "^5.0.0",
"strip-markdown": "^3.0.1",
Expand Down
50 changes: 50 additions & 0 deletions packages/gatsby-plugin-mdx/utils/get-table-of-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const visit = require("unist-util-visit");

// parse mdast-utils-toc object to JSON object:
//
// {"items": [{
// "url": "#something-if",
// "title": "Something if",
// "items": [
// {
// "url": "#something-else",
// "title": "Something else"
// },
// {
// "url": "#something-elsefi",
// "title": "Something elsefi"
// }
// ]},
// {
// "url": "#something-iffi",
// "title": "Something iffi"
// }]}
//
// TODO: fully test it with different options, tight=True
//
function getItems(node, current) {
if (node.type === "paragraph") {
visit(node, item => {
if (item.type === "link") {
current.url = item.url;
}
if (item.type === "text") {
current.title = item.value;
}
});
return current;
} else {
if (node.type === "list") {
current.items = node.children.map(i => getItems(i, {}));
return current;
} else if (node.type === "listItem") {
const heading = getItems(node.children[0], {});
if (node.children.length > 1) {
getItems(node.children[1], heading);
}
return heading;
}
}
return {};
}
module.exports = getItems;

0 comments on commit ecf1c59

Please sign in to comment.