Skip to content

feat: preserve vfile data from unified processor #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@contentlayer/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"remark-rehype": "^10.1.0",
"source-map-support": "^0.5.21",
"type-fest": "^2.12.2",
"unified": "^10.1.2"
"unified": "^10.1.2",
"vfile": "^5.3.4"
},
"devDependencies": {
"@types/source-map-support": "^0.5.4",
Expand Down
4 changes: 4 additions & 0 deletions packages/@contentlayer/core/src/data-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Data } from 'vfile'

export type Document = Record<string, any> & DocumentMeta

export type NestedDocument = Record<string, any> & Omit<DocumentMeta, '_id'>
Expand All @@ -18,6 +20,8 @@ export type Markdown = {
raw: string
/** Generated HTML based on Markdown source */
html: string
/** Information generated by Markdown processor */
data: Data
}

export type MDX = {
Expand Down
7 changes: 4 additions & 3 deletions packages/@contentlayer/core/src/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import remarkFrontmatter from 'remark-frontmatter'
import remarkParse from 'remark-parse'
import remark2rehype from 'remark-rehype'
import { unified } from 'unified'
import { Data } from 'vfile'

import type { RawDocumentData } from '../data-types.js'
import type { MarkdownOptions, MarkdownUnifiedBuilderCallback } from '../plugin.js'
Expand All @@ -19,7 +20,7 @@ export const markdownToHtml = ({
mdString: string
options?: MarkdownOptions | MarkdownUnifiedBuilderCallback
rawDocumentData: RawDocumentData
}): T.Effect<OT.HasTracer & HasConsole, UnexpectedMarkdownError, string> =>
}): T.Effect<OT.HasTracer & HasConsole, UnexpectedMarkdownError, { html: string; data: Data }> =>
pipe(
T.gen(function* ($) {
// const matterResult = matter(mdString)
Expand All @@ -32,7 +33,7 @@ export const markdownToHtml = ({
return yield* $(
T.tryPromise(async () => {
const { parse: parseWasm } = await import('markdown-wasm/dist/markdown.node.js')
return parseWasm(mdString)
return { html: parseWasm(mdString), data: {} }
}),
)
}
Expand Down Expand Up @@ -67,7 +68,7 @@ export const markdownToHtml = ({

const res = yield* $(T.tryPromise(() => builder.process(mdString)))

return res.toString()
return { html: String(res), data: res.data }
}),
T.catchAllDefect(T.fail),
T.mapError((error) => new UnexpectedMarkdownError({ error })),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ const getDataForFieldDef = ({
case 'date':
return new Date(rawFieldData)
case 'markdown':
const html = yield* $(
const { html, data } = yield* $(
core.markdownToHtml({ mdString: rawFieldData, options: options?.markdown, rawDocumentData: {} }),
)
return <core.Markdown>{ raw: rawFieldData, html }
return <core.Markdown>{ raw: rawFieldData, html, data }
// NOTE `mdx` support for Contentful is experimental and not clearly defined
case 'mdx':
const code = yield* $(
Expand Down
8 changes: 4 additions & 4 deletions packages/@contentlayer/source-files/src/fetchData/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,19 @@ const getDataForFieldDef = ({
const rawContent = yield* $(getFromDocumentContext('rawContent'))
if (rawContent.kind !== 'markdown' && rawContent.kind !== 'mdx') return utils.assertNever(rawContent)

const html = yield* $(
const { html, data } = yield* $(
core.markdownToHtml({
mdString: rawContent.rawDocumentContent,
options: options?.markdown,
rawDocumentData,
}),
)
return identity<core.Markdown>({ raw: rawFieldData, html })
return identity<core.Markdown>({ raw: rawFieldData, html, data })
} else {
const html = yield* $(
const { html, data } = yield* $(
core.markdownToHtml({ mdString: rawFieldData, options: options?.markdown, rawDocumentData }),
)
return identity<core.Markdown>({ raw: rawFieldData, html })
return identity<core.Markdown>({ raw: rawFieldData, html, data })
}
}
case 'mdx': {
Expand Down
41 changes: 41 additions & 0 deletions packages/integration-tests/src/markdown/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import rehypeStringify from 'rehype-stringify'
import remarkFrontmatter from 'remark-frontmatter'
import remarkParse from 'remark-parse'
import remark2rehype from 'remark-rehype'
import type { Plugin } from 'unified'
import { expect, test, vi } from 'vitest'

test('markdown builder pattern', async () => {
Expand Down Expand Up @@ -45,3 +46,43 @@ test('markdown builder pattern', async () => {

expect(spyFn).toHaveBeenCalledOnce()
})

test('pass vfile.data from markdown processor', async () => {
const Post = defineDocumentType(() => ({
name: 'Post',
filePathPattern: '**/*.md',
fields: {},
}))

const testDirPath = fileURLToPath(new URL('.', import.meta.url))

await fs.rm(path.join(testDirPath, '.contentlayer'), { recursive: true, force: true })

process.env['INIT_CWD'] = testDirPath

const testMessage = 'Hello, world!'

const testPlugin: Plugin = () => {
return function transformer(tree, file) {
file.data.test = testMessage
}
}

const source = await makeSource({
contentDirPath: path.join(testDirPath, 'posts'),
documentTypes: [Post],
markdown: {
remarkPlugins: [testPlugin],
},
})

await core.runMain({ tracingServiceName: 'test', verbose: false })(
core.generateDotpkg({ config: { source, esbuildHash: 'STATIC_HASH' }, verbose: true }),
)

const generated = JSON.parse(
await fs.readFile(path.join(testDirPath, '.contentlayer', 'generated/Post/post-1.md.json'), { encoding: 'utf-8' }),
)

expect(generated.body.data.test).toBe(testMessage)
})
Loading