Skip to content

Commit

Permalink
Support serializing all VFile-compatible values (#311)
Browse files Browse the repository at this point in the history
Closes #309
  • Loading branch information
nwalters512 authored Nov 7, 2022
1 parent f5b0e74 commit f68d370
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .jest/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import React from 'react'
import { MDXRemote, MDXRemoteProps } from '../src/index'
import { serialize } from '../src/serialize'
import { SerializeOptions } from '../src/types'
import { VFileCompatible } from 'vfile'

export async function renderStatic(
mdx: string,
mdx: VFileCompatible,
{
components,
scope,
Expand Down
11 changes: 11 additions & 0 deletions __tests__/serialize.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react'
import ReactDOMServer from 'react-dom/server'
import { paragraphCustomAlerts } from '@hashicorp/remark-plugins'
import * as MDX from '@mdx-js/react'
import { VFile } from 'vfile'

import { MDXRemote } from '../src/index'
import { serialize } from '../src/serialize'
Expand Down Expand Up @@ -163,4 +164,14 @@ hello: world
`)
}
})

test('supports VFile', async () => {
const result = await renderStatic(new VFile('foo **bar**'))
expect(result).toMatchInlineSnapshot(`"<p>foo <strong>bar</strong></p>"`)
})

test('supports Buffer', async () => {
const result = await renderStatic(Buffer.from('foo **bar**'))
expect(result).toMatchInlineSnapshot(`"<p>foo <strong>bar</strong></p>"`)
})
})
7 changes: 3 additions & 4 deletions src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { compile, CompileOptions } from '@mdx-js/mdx'
import { VFile } from 'vfile'
import { VFile, VFileCompatible } from 'vfile'
import { matter } from 'vfile-matter'

import { createFormattedMDXError } from './format-mdx-error'
Expand Down Expand Up @@ -32,15 +32,14 @@ function getCompileOptions(
* Parses and compiles the provided MDX string. Returns a result which can be passed into <MDXRemote /> to be rendered.
*/
export async function serialize(
/** Raw MDX contents as a string. */
source: string,
source: VFileCompatible,
{
scope = {},
mdxOptions = {},
parseFrontmatter = false,
}: SerializeOptions = {}
): Promise<MDXRemoteSerializeResult> {
const vfile = new VFile({ value: source })
const vfile = new VFile(source)

// makes frontmatter available via vfile.data.matter
if (parseFrontmatter) {
Expand Down

0 comments on commit f68d370

Please sign in to comment.