Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import slugify from '@sindresorhus/slugify';
import { defineDocumentType, makeSource } from 'contentlayer/source-files';
import type { DocumentTypes } from '.contentlayer/types';
const commonFields = {
title: {
type: `string`,
required: true,
},
meta_description: {
type: `string`,
required: false,
},
meta_title: {
type: `string`,
required: false,
},
disableTOC: { type: `boolean`, required: false },
disableFeedback: { type: `boolean`, required: false },
} as const;
interface Heading {
depth: number;
value: string;
slug: string;
}
const computedFields = {
category: {
type: `string`,
resolve: (doc: DocumentTypes) => doc._id.split(`/`)[0],
},
slug: {
type: 'string',
resolve: (doc: DocumentTypes) => doc._id.replace('.mdx', ''),
},
headings: {
type: `json`,
resolve: (doc: DocumentTypes) => {
let headings: Array<Heading> = [];
doc?.content?.raw.split('\n').forEach((str: string) => {
if (str.startsWith('## ')) {
const value = str.split('## ')[1];
headings.push({
depth: 2,
value,
slug: slugify(value, { lowercase: true }),
});
} else if (str.startsWith('### ')) {
const value = str.split('### ')[1];
headings.push({
depth: 3,
value,
slug: slugify(value, { lowercase: true }),
});
}
});
return headings;
},
},
} as const;
export const APIReference = defineDocumentType(() => ({
name: `APIReference`,
filePathPattern: `api-reference/**/*.mdx`,
fileType: `mdx`,
fields: {
...commonFields,
},
computedFields,
}));
export const UIExtension = defineDocumentType(() => ({
name: `UIExtension`,
filePathPattern: `ui-extensions/**/*.mdx`,
fileType: `mdx`,
fields: {
...commonFields,
},
computedFields,
}));
export const Guide = defineDocumentType(() => ({
name: `Guide`,
filePathPattern: `guides/**/*.mdx`,
fileType: `mdx`,
fields: {
...commonFields,
},
computedFields,
}));
export const Quickstart = defineDocumentType(() => ({
name: `Quickstart`,
filePathPattern: `quickstarts/**/*.mdx`,
fileType: `mdx`,
fields: {
...commonFields,
},
computedFields,
}));
export default makeSource({
contentDirPath: `docs`,
documentTypes: [APIReference, UIExtension, Guide, Quickstart],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment