contentlayerdev / contentlayerdev/contentlayer

[suggestion] allow mdx processing without mdx-bundler

未关闭
#247 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
improvement needs-research topic: markdown/mdx
主要语言
TypeScript
星标
3.5k
派生
192
PR 合并指标
30 天内没有已合并 PR

描述

`mdx-bundler` is pretty opinionated and i would like to be able to use a simpler setup closer to the official [`MDX on demand`](https://mdxjs.com/guides/mdx-on-demand/) guide.

it would be cool if something like the following could work with `contentlayer`:

```ts
import { compile } from '@mdx-js/mdx'
import withToc from '@stefanprobst/rehype-extract-toc'
import type { RawDocumentData } from 'contentlayer/source-files'
import { defineDocumentType, makeSource } from 'contentlayer/source-files'
import withSlug from 'rehype-slug'
import withFrontmatter from 'remark-frontmatter'
import withGfm from 'remark-gfm'
import { VFile } from 'vfile'
import { matter } from 'vfile-matter'

import { getTranslations } from '@/app/i18n'

const Post = defineDocumentType(() => {
return {
name: 'Post',
description: 'Post',
filePathPattern: `posts/**/*.mdx`,
contentType: 'mdx',
fields: {
title: { description: 'Title', type: 'string', required: true },
locale: { description: 'Locale', type: 'enum', options: ['en', 'de'], required: true },
},
}
})

export default makeSource({
contentDirPath: 'content',
documentTypes: [Post],
async mdx(raw: string, sourceFilePath?: string) {
const input = new VFile({ value: raw, path: sourceFilePath })

matter(input)
const { locale } = input.data['matter']
const t = await getTranslations(locale)

const vfile = await compile(input, {
remarkPlugins: [withFrontmatter, withGfm],
remarkRehypeOptions: {
footnoteBackLabel: t('footnote-back-label'),
footnoteLabel: t('footnote-label'),
},
rehypePlugins: [withSlug, withToc],
})

return { code: String(vfile), data: vfile.data }
},
})
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。