First iteration of blog
This commit is contained in:
61
faq/custom-mdx-component.md
Normal file
61
faq/custom-mdx-component.md
Normal file
@ -0,0 +1,61 @@
|
||||
# How can I add a custom MDX component?
|
||||
|
||||
Here's an example on how to create a donut chart from Chart.js (assuming you already have the dependencies installed) and use it in MDX posts. First, create a new `DonutChart.tsx` component in `components`:
|
||||
|
||||
```tsx
|
||||
'use client'
|
||||
|
||||
import { Doughnut } from 'react-chartjs-2'
|
||||
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
|
||||
|
||||
ChartJS.register(ArcElement, Tooltip, Legend)
|
||||
|
||||
const DonutChart = ({ data }) => {
|
||||
return <Doughnut data={data} />
|
||||
}
|
||||
|
||||
export default Doughnut
|
||||
```
|
||||
|
||||
Since the underlying `Doughnut` component uses React hooks, we add the `'use client'` directive to specify that it is a client side component. Also, there is an existing issue which prevents named components from being used, so we need to export the component as the default export.
|
||||
|
||||
Next, add the component to `MDXComponents.tsx`:
|
||||
|
||||
```diff
|
||||
...
|
||||
+ import DonutChart from './DonutChart'
|
||||
|
||||
export const components: MDXComponents = {
|
||||
Image,
|
||||
TOCInline,
|
||||
a: CustomLink,
|
||||
pre: Pre,
|
||||
+ DonutChart,
|
||||
BlogNewsletterForm,
|
||||
}
|
||||
```
|
||||
|
||||
You can now use the component in `.mdx` files:
|
||||
|
||||
```mdx
|
||||
## Example Donut Chart
|
||||
|
||||
export const data = {
|
||||
labels: ['Red', 'Blue', 'Yellow'],
|
||||
datasets: [
|
||||
{
|
||||
label: '# of Votes',
|
||||
data: [12, 19, 3],
|
||||
backgroundColor: [
|
||||
'rgba(255, 99, 132, 0.2)',
|
||||
'rgba(54, 162, 235, 0.2)',
|
||||
'rgba(255, 206, 86, 0.2)',
|
||||
],
|
||||
borderColor: ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)'],
|
||||
borderWidth: 1,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
<DonutChart data={data} />
|
||||
```
|
91
faq/customize-kbar-search.md
Normal file
91
faq/customize-kbar-search.md
Normal file
@ -0,0 +1,91 @@
|
||||
# How can I customize the `kbar` search?
|
||||
|
||||
Add a `SearchProvider` component such as the one shown below and use it in place of the default `SearchProvider` component in `app/layout.tsx`.
|
||||
|
||||
`defaultActions` are the initial list of actions.
|
||||
|
||||
`onSearchDocumentsLoad` is a callback function that is called when the documents specified by `searchDocumentsPath` are loaded. Set `searchDocumentsPath` to `false` to disable the dynamically loaded search feature.
|
||||
|
||||
```tsx
|
||||
'use client'
|
||||
|
||||
import { KBarSearchProvider } from 'pliny/search/KBar'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { CoreContent } from 'pliny/utils/contentlayer'
|
||||
import { Blog } from 'contentlayer/generated'
|
||||
|
||||
export const SearchProvider = ({ children }) => {
|
||||
const router = useRouter()
|
||||
return (
|
||||
<KBarSearchProvider
|
||||
kbarConfig={{
|
||||
searchDocumentsPath: 'search.json',
|
||||
defaultActions: [
|
||||
{
|
||||
id: 'homepage',
|
||||
name: 'Homepage',
|
||||
keywords: '',
|
||||
shortcut: ['h', 'h'],
|
||||
section: 'Home',
|
||||
perform: () => router.push('/'),
|
||||
},
|
||||
{
|
||||
id: 'projects',
|
||||
name: 'Projects',
|
||||
keywords: '',
|
||||
shortcut: ['p'],
|
||||
section: 'Home',
|
||||
perform: () => router.push('/projects'),
|
||||
},
|
||||
],
|
||||
onSearchDocumentsLoad(json) {
|
||||
return json.map((post: CoreContent<Blog>) => ({
|
||||
id: post.path,
|
||||
name: post.title,
|
||||
keywords: post?.summary || '',
|
||||
section: 'Blog',
|
||||
subtitle: post.tags.join(', '),
|
||||
perform: () => router.push('/' + post.path),
|
||||
}))
|
||||
},
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</KBarSearchProvider>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
You can even choose to do a full text search over the entire generated blog content though this would come at the expense of a larger search index file by modifying the `createSearchIndex` function in `contentlayer.config.ts` to:
|
||||
|
||||
```tsx
|
||||
function createSearchIndex(allBlogs) {
|
||||
if (
|
||||
siteMetadata?.search?.provider === 'kbar' &&
|
||||
siteMetadata.search.kbarConfig.searchDocumentsPath
|
||||
) {
|
||||
writeFileSync(
|
||||
`public/${siteMetadata.search.kbarConfig.searchDocumentsPath}`,
|
||||
JSON.stringify((sortPosts(allBlogs)))
|
||||
)
|
||||
console.log('Local search index generated...')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Note the change from `JSON.stringify(allCoreContent(sortPosts(allBlogs)))` to `JSON.stringify((sortPosts(allBlogs)))`.
|
||||
|
||||
Next, in the modified `SearchProvider`, dump the raw content to the `keywords` field in the `onSearchDocumentsLoad` prop:
|
||||
|
||||
```tsx
|
||||
onSearchDocumentsLoad(json) {
|
||||
return json.map((post: Blog) => ({
|
||||
id: post.path,
|
||||
name: post.title,
|
||||
keywords: post.body.raw,
|
||||
section: 'Blog',
|
||||
subtitle: post.tags.join(', '),
|
||||
perform: () => router.push('/' + post.path),
|
||||
}))
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user