Skip to content

Commit 4d0d75c

Browse files
committed
docs: add support for meta header
1 parent e55d7e3 commit 4d0d75c

File tree

11 files changed

+135
-10
lines changed

11 files changed

+135
-10
lines changed

docs/README.md

+17-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ There are IDE plugins for Prettier that you can use to format code on save:
3434
- [IntelliJ](https://plugins.jetbrains.com/plugin/10456-prettier)
3535
- [VSCode](https://github.com/prettier/prettier-vscode)
3636

37+
### Page Header
38+
39+
Each page usually starts with a description of the content of the page and links
40+
to related content.
41+
42+
This information has to be specified in the front-matter of the page:
43+
44+
```
45+
---
46+
description: This is a description of the page.
47+
related_content:
48+
- name: Related Content
49+
url: /related/content
50+
---
51+
```
52+
3753
### Callouts
3854

3955
For notes, warnings and other callouts, use
@@ -45,12 +61,7 @@ For notes, warnings and other callouts, use
4561

4662
Code examples are titled code blocks with a unique ID.
4763

48-
To use the `CodeExample` component, make sure it is imported at the top of the
49-
document:
50-
51-
```
52-
import CodeExample from '@site/src/components/CodeExample'
53-
```
64+
The `CodeExample` component is available without importing it.
5465

5566
Assign each code example a unique ID and give it a title:
5667

docs/docs/install.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# Install
1+
---
2+
description: Installing Couchbase Lite for Dart
3+
---
24

3-
This guide will help you install Couchbase Lite and verify that everything is
4-
working correctly.
5+
# Install
56

67
:::info
78

docs/docusaurus.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require('ts-node').register()
55
const lightCodeTheme = require('prism-react-renderer/themes/github')
66
const darkCodeTheme = require('prism-react-renderer/themes/dracula')
77
const { exampleLinks } = require('./src/remark/example-links')
8+
const { metaHeader } = require('./src/remark/meta-header')
89

910
/** @type {import('@docusaurus/types').Config} */
1011
const config = {
@@ -31,7 +32,7 @@ const config = {
3132
sidebarPath: require.resolve('./sidebars.js'),
3233
routeBasePath: '/',
3334
editUrl: 'https://github.com/cbl-dart/cbl-dart/tree/main/docs/',
34-
remarkPlugins: [exampleLinks],
35+
remarkPlugins: [exampleLinks, metaHeader],
3536
},
3637
blog: false,
3738
theme: {

docs/package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"react": "^17.0.2",
2727
"react-dom": "^17.0.2",
2828
"ts-node": "^10.9.1",
29+
"unist-util-flatmap": "^1.0.0",
2930
"unist-util-is": "^5.1.1",
3031
"unist-util-visit": "^4.1.1"
3132
},
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { useMDXContent } from '../context'
3+
import styles from './styles.module.css'
4+
5+
export default function MetaHeader() {
6+
const content = useMDXContent()
7+
const frontMatter = content.frontMatter
8+
const description = frontMatter.description
9+
const relatedContent = frontMatter.related_content
10+
11+
if (!description && !relatedContent) {
12+
return <></>
13+
}
14+
15+
return (
16+
<div className={styles.metaHeader}>
17+
{description && (
18+
<div>
19+
Description — <em>{description}</em>
20+
</div>
21+
)}
22+
{relatedContent && (
23+
<div>
24+
Related Content —{' '}
25+
{relatedContent.map(({ name, url }, index) => {
26+
return (
27+
<>
28+
<a href={url}>{name}</a>
29+
{index < relatedContent.length - 1 && ' | '}
30+
</>
31+
)
32+
})}
33+
</div>
34+
)}
35+
</div>
36+
)
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.metaHeader {
2+
margin-bottom: 3rem;
3+
padding-left: 1rem;
4+
font-weight: 300;
5+
border-left: 2px solid var(--ifm-color-emphasis-500);
6+
}

docs/src/components/context.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const React = require('react')
2+
3+
export const MDXContent = React.createContext()
4+
5+
/**
6+
* Returns the MDX content of the current page, including frontmatter.
7+
*/
8+
export function useMDXContent() {
9+
return React.useContext(MDXContent)
10+
}

docs/src/remark/meta-header.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const exampleRegex = /Example ([0-9]+)/
2+
3+
/**
4+
* Remark plugin that inserts the meta header under each level 1 heading.
5+
*/
6+
export function metaHeader() {
7+
return async (ast: any) => {
8+
// Workaround for import ES6 modules.
9+
const is = (await import('unist-util-is')).is
10+
const flatMap = (await import('unist-util-flatmap')).default
11+
12+
flatMap(ast, (node) => {
13+
if (is(node, 'heading') && (node as any).depth == 1) {
14+
return [
15+
node,
16+
{
17+
type: 'html',
18+
value: '<MetaHeader />',
19+
},
20+
]
21+
} else {
22+
return [node]
23+
}
24+
})
25+
}
26+
}

docs/src/theme/DocItem/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import DocItem from '@theme-original/DocItem'
2+
import React from 'react'
3+
4+
import { MDXContent } from '@site/src/components/context'
5+
6+
export default function DocItemWrapper(props) {
7+
return (
8+
<MDXContent.Provider value={props.content}>
9+
<DocItem {...props} />
10+
</MDXContent.Provider>
11+
)
12+
}

docs/src/theme/MDXComponents/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import CodeExample from '@site/src/components/CodeExample'
2+
import MetaHeader from '@site/src/components/MetaHeader'
3+
import MDXComponents from '@theme-original/MDXComponents'
4+
5+
export default {
6+
...MDXComponents,
7+
codeexample: CodeExample,
8+
metaheader: MetaHeader,
9+
}

0 commit comments

Comments
 (0)