Skip to content

Instantly share code, notes, and snippets.

@jpedroschmitz
Created September 9, 2021 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpedroschmitz/a4dd94a25e533c9f8838e916e1b48ffa to your computer and use it in GitHub Desktop.
Save jpedroschmitz/a4dd94a25e533c9f8838e916e1b48ffa to your computer and use it in GitHub Desktop.
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