项目作者: stackbit

项目描述 :
Gatsby plugin for site menus
高级语言: JavaScript
项目地址: git://github.com/stackbit/gatsby-plugin-menus.git
创建时间: 2019-01-02T18:22:30Z
项目社区:https://github.com/stackbit/gatsby-plugin-menus

开源协议:MIT License

下载


gatsby-plugin-menus

A simple-to-use menu plugin for Gatsby that allows for infinitely nested menus.

Comes with built-in support for extracting menus defined in Markdown, but can also be extended to load menus from any source.

Install

npm install --save @stackbit/gatsby-plugin-menus

How to use

In your gatsby-config.js

  1. // gatsby-config.js
  2. plugins: [
  3. {
  4. resolve: '@stackbit/gatsby-plugin-menus',
  5. options: {
  6. // static definition of menu items (optional)
  7. menus: {
  8. main: // identifier of menu container
  9. [ // array of contained children menu items
  10. {
  11. identifier: 'myId', // identifier for this item (optional)
  12. title: 'Title for page',
  13. url: '/page-1/',
  14. weight: 1
  15. }
  16. ]
  17. },
  18. // Gatsby node types from which we extract menus (optional, see "Advanced usage")
  19. sourceNodeType: 'MarkdownRemark',
  20. // the relative node path where we can find the 'menus' container (optional)
  21. sourceDataPath: 'frontmatter',
  22. // the relative node path where we can find the page's URL (required)
  23. sourceUrlPath: 'fields.url',
  24. // custom menu loading function (optional)
  25. menuLoader: customLoaderFunction,
  26. // the property to use for injecting to the page context (optional, see "Advanced usage")
  27. pageContextProperty: 'menus',
  28. },
  29. },
  30. ],

In your Markdown pages

By default, all Markdown pages can define which menus contain them.

See Advanced usage to learn how to extract menus from other sources.

There are several supported ways to add a page to a menu:

  • Single menu container:
  1. ---
  2. title: Lorem Ipsum
  3. menus: main # identifier of menu container
  4. ---
  • Multiple menu containers:
  1. ---
  2. title: Lorem Ipsum
  3. menus:
  4. - main
  5. - footer
  6. ---
  • Overriding menu item values:
  1. ---
  2. title: Lorem Ipsum
  3. menus:
  4. main:
  5. identifier: myId # identifier for this item (optional)
  6. title: Go to Lorem Ipsum # override title for this item
  7. weight: 1
  8. ---

How to query

A sample GraphQL query to get menus:

  1. {
  2. menus {
  3. main {
  4. identifier
  5. title
  6. url
  7. items {
  8. identifier
  9. title
  10. url
  11. }
  12. }
  13. footer {
  14. identifier
  15. title
  16. url
  17. }
  18. }
  19. }

The field items contains the sub-menu items if available. The query should nest to accommodate the maximum hierarchy needed.

Menu order

Items within a menu are first ordered by their defined weight (both from static and page menu items). If weights are equal, then menu items defined in the plugin options (static) are placed first by their appearance order, followed by page menu items ordered by their page’s creation time (birthTime).

Advanced usage

Source options

The plugin defines the following options to customize where menu information is extracted from:

  • sourceNodeType - The Gatsby node type we want to extract menus from (default: MarkdownRemark)

  • sourceDataPath - The relative path in the node where we expect to find the menus item which contains menu information. In case there is no explicit title defined for the menu item, we attempt to find the default title under ${sourceDataPath}.title. (default: frontmatter)

  • sourceUrlPath - The relative path in the node where we expect to find the page’s URL. This option is always required.

If more flexibility is needed for deciding how menus are loaded, a custom function should be defined.

Custom loader

It is possible to override the default behavior of extracting menu items from Markdown pages by providing your own custom loader function.

  1. Provide custom function in the plugin options:
  1. // gatsby-config.js
  2. plugins: [
  3. {
  4. resolve: '@stackbit/gatsby-plugin-menus',
  5. options: {
  6. ...
  7. // custom menu loading function (optional)
  8. menuLoader: customLoaderFunction,
  9. },
  10. },
  11. ],
  1. Implement custom loader function:
  1. customLoaderFunction = (loaderActions) => {
  2. const { getNodesByType, getNode } = loaderActions
  3. ...
  4. return {
  5. main: [
  6. {
  7. identifier: 'myId',
  8. title: 'Title for page',
  9. url: '/page-1/',
  10. weight: 1,
  11. data: {
  12. ... // custom data that will be made available via graphql
  13. }
  14. }
  15. ]
  16. }
  17. }

Accessing frontmatter

Menu items created from Markdown pages expose additional information about the page they were generated from. We can use that information to include other information that was defined in the Markdown page’s frontmatter.

  • Markdown page:
  1. ---
  2. title: Lorem Ipsum
  3. moreInfo: Additional pieces of information
  4. menus: main
  5. ---
  • GraphQL query:
  1. {
  2. menus {
  3. main {
  4. identifier
  5. title
  6. url
  7. page {
  8. frontmatter {
  9. moreInfo
  10. }
  11. }
  12. }
  13. }
  14. }

Page context

The plugin can be used to inject the menus structure directly into the context of your Gatsby pages.

To enable this, set the pageContextProperty option:

  1. // gatsby-config.js
  2. pageContextProperty: 'menus'

And the menus will be available in the page’s context:

  1. this.props.pageContext.menus