Skip to content
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

Standalone compile() function now returns headmatter data too. #239

Merged
merged 1 commit into from
Apr 29, 2021
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tiny-pants-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mdsvex': patch
---

Standalone compile() function not returning headmatter attributes. Also export all types.
7 changes: 6 additions & 1 deletion packages/mdsvex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
UnifiedPlugins,
LayoutMode,
} from './types';
export * from './types';

import { join } from 'path';
import fs from 'fs';
Expand Down Expand Up @@ -285,7 +286,11 @@ export const mdsvex = (options: MdsvexOptions = defaults): Preprocessor => {
if (!extensionsParts.includes(filename.split('.').pop())) return;

const parsed = await parser.process({ contents: content, filename });
return { code: parsed.contents as string, map: '' };
return {
code: parsed.contents as string,
data: parsed.data as Record<string, unknown>,
map: '',
};
},
};
};
Expand Down
8 changes: 5 additions & 3 deletions packages/mdsvex/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,11 @@ export interface MdsvexCompileOptions extends MdsvexOptions {
filename?: string;
}

export type PreprocessorReturn = Promise<
{ code: string; map?: string } | undefined
>;
export type PreprocessorReturn = Promise<{
code: string;
data?: Record<string, unknown>;
map?: string
} | undefined>;

export interface Preprocessor {
markup: (args: { content: string; filename: string }) => PreprocessorReturn;
Expand Down
26 changes: 26 additions & 0 deletions packages/mdsvex/test/it/mdsvex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,32 @@ mdsvex_it('compile, no options', async () => {
assert.equal(
{
code: '\n<h1>Hello world</h1>\n',
data: {},
map: '',
},
output
);
});

mdsvex_it('compile, gets headmatter attributes', async () => {
const output = await compile(
`
---
title: Yo
---

# Hello world
`
);

assert.equal(
{
code: '<script context=\"module\">\n\texport const metadata = {\"title\":\"Yo\"};\n\tconst { title } = metadata;\n</script>\n\n<h1>Hello world</h1>\n',
data: {
fm: {
title: 'Yo'
},
},
map: '',
},
output
Expand Down