|
| 1 | +# docusaurus-theme-frontmatter |
| 2 | + |
| 3 | +This package enhances the Docusaurus classic theme by exposing the [docs](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs#markdown-frontmatter), [blog](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-blog#markdown-frontmatter), and [pages](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-pages) front matter to the following components and their children: |
| 4 | + |
| 5 | +* [BlogPostItem](https://github.com/facebook/docusaurus/tree/main/packages/docusaurus-theme-classic/src/theme/BlogPostItem) |
| 6 | +* [DocItem](https://github.com/facebook/docusaurus/tree/main/packages/docusaurus-theme-classic/src/theme/DocItem) |
| 7 | +* [MDXPage](https://github.com/facebook/docusaurus/tree/main/packages/docusaurus-theme-classic/src/theme/MDXPage) |
| 8 | + |
| 9 | +Furthermore, this allows you to define and access ***custom*** front matter. |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```sh |
| 14 | +yarn add docusaurus-theme-frontmatter |
| 15 | +``` |
| 16 | + |
| 17 | +Then, include the plugin in your `docusaurus.config.js` file. |
| 18 | + |
| 19 | +```diff |
| 20 | +// docusaurus.config.js |
| 21 | +module.exports = { |
| 22 | + ... |
| 23 | ++ themes: ['docusaurus-theme-frontmatter'], |
| 24 | + ... |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### TypeScript support |
| 29 | + |
| 30 | +To enable TypeScript support, the TypeScript configuration should be updated to add the `docusaurus-theme-frontmatter` type definitions. This can be done in the `tsconfig.json` file: |
| 31 | + |
| 32 | +```diff |
| 33 | +{ |
| 34 | + "extends": "@tsconfig/docusaurus/tsconfig.json", |
| 35 | + "compilerOptions": { |
| 36 | + ... |
| 37 | ++ "types": ["docusaurus-theme-frontmatter"] |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## How to use |
| 43 | + |
| 44 | +The `useFrontMatter()` hook is made available to any of your components through the `@theme/useFrontMatter` import. For example, you might have a component that creates a gallery of images. |
| 45 | + |
| 46 | +```mdx |
| 47 | +--- |
| 48 | +title: Miniature fairy doors of NYC |
| 49 | +hide_table_of_contents: true |
| 50 | +gallery: |
| 51 | + - /images/117-first-avenue.jpg |
| 52 | + - /images/lower-east-side.jpg |
| 53 | + - /images/my-guinness.jpg |
| 54 | + - /images/hundred-years.jpg |
| 55 | +--- |
| 56 | +import Galley from '@theme/Galley'; |
| 57 | + |
| 58 | +<Galley /> |
| 59 | +``` |
| 60 | + |
| 61 | +```jsx |
| 62 | +import React from 'react'; |
| 63 | +import ImageGallery from 'react-image-gallery'; |
| 64 | +import useFrontMatter from '@theme/useFrontMatter'; |
| 65 | + |
| 66 | +export default function GalleyComponent () { |
| 67 | + const { gallery } = useFrontMatter(); |
| 68 | + |
| 69 | + if (Array.isArray(gallery)) { |
| 70 | + const images = gallery.map((image) => ({ |
| 71 | + original: image |
| 72 | + })); |
| 73 | + |
| 74 | + return <ImageGallery items={images} />; |
| 75 | + } |
| 76 | + |
| 77 | + return null; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Public API |
| 82 | + |
| 83 | +### `useFrontMatter<T extends FrontMatter>()` |
| 84 | + |
| 85 | +Returns the front matter for the current context. |
| 86 | + |
| 87 | +```ts |
| 88 | +import useFrontMatter from '@theme/useFrontMatter'; |
| 89 | + |
| 90 | +interface CustomFrontMatter { |
| 91 | + gallery?: string[]; |
| 92 | +} |
| 93 | + |
| 94 | +const MyComponent = () => { |
| 95 | + const { gallery } = useFrontMatter<CustomFrontMatter>(); |
| 96 | + return (<... />); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### `Context` |
| 101 | + |
| 102 | +The current front matter context. |
| 103 | + |
| 104 | +Generally, this is something to be left alone and operates behind the scenes. This is how it is used to [enhance DocItem](https://github.com/roydukkey/docusaurus-theme-frontmatter/blob/master/src/theme/DocItem.tsx) scaffolding the hook: |
| 105 | + |
| 106 | +```js |
| 107 | +import { Context } from '@theme/useFrontMatter'; |
| 108 | +import DocItem from '@theme-init/DocItem'; |
| 109 | +import React from 'react'; |
| 110 | + |
| 111 | +export default (props) => <Context.Provider value={props.content.frontMatter}> |
| 112 | + <DocItem {...props} /> |
| 113 | +</Context.Provider>; |
| 114 | +``` |
| 115 | + |
| 116 | +### `FrontMatter`, `DocItemFrontMatter`, `BlogPostItemFrontMatter`, `MDXPageFrontMatter` |
| 117 | + |
| 118 | +These types are provided to assist in describing the values returned by the `useFrontMatter()` hook. |
| 119 | + |
| 120 | +```ts |
| 121 | +import useFrontMatter from '@theme/useFrontMatter'; |
| 122 | +import type { DocItemFrontMatter } from '@theme/useFrontMatter'; |
| 123 | + |
| 124 | +const MyComponent = () => { |
| 125 | + const { id, title, keywords } = useFrontMatter<DocItemFrontMatter>(); |
| 126 | + return (<... />); |
| 127 | +} |
| 128 | +``` |
0 commit comments