summaryrefslogtreecommitdiff
path: root/src/compiler/index.ts
diff options
context:
space:
mode:
authorGravatar Matt Mulder <muldmatt@gmail.com> 2021-04-13 13:04:01 -0400
committerGravatar GitHub <noreply@github.com> 2021-04-13 13:04:01 -0400
commitac22d94e1170bce5f1f50abfdf46052369a0edf8 (patch)
tree26aed00d93b2244f1e3b02fbb4aca46a2623709a /src/compiler/index.ts
parentb5885813969fcc4d6bffff84609cb53ffd84bac7 (diff)
downloadastro-ac22d94e1170bce5f1f50abfdf46052369a0edf8.tar.gz
astro-ac22d94e1170bce5f1f50abfdf46052369a0edf8.tar.zst
astro-ac22d94e1170bce5f1f50abfdf46052369a0edf8.zip
fix: bundle client-side code for components used in .md pages (#78)
Diffstat (limited to 'src/compiler/index.ts')
-rw-r--r--src/compiler/index.ts21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/compiler/index.ts b/src/compiler/index.ts
index c20596f65..d2d7bff08 100644
--- a/src/compiler/index.ts
+++ b/src/compiler/index.ts
@@ -48,13 +48,9 @@ async function convertAstroToJsx(template: string, opts: ConvertAstroOptions): P
}
/**
- * .md -> .jsx
- * Core function processing Markdown, but along the way also calls convertAstroToJsx().
+ * .md -> .astro source
*/
-async function convertMdToJsx(
- contents: string,
- { compileOptions, filename, fileID }: { compileOptions: CompileOptions; filename: string; fileID: string }
-): Promise<TransformResult> {
+export async function convertMdToAstroSource(contents: string): Promise<string> {
const { data: frontmatterData, content } = matter(contents);
const { headers, headersExtension } = createMarkdownHeadersCollector();
const { htmlAstro, mdAstro } = encodeAstroMdx();
@@ -80,15 +76,24 @@ async function convertMdToJsx(
// Break it up here so that the HTML parser won't detect it.
const stringifiedSetupContext = JSON.stringify(contentData).replace(/\<\/script\>/g, `</scrip" + "t>`);
- const raw = `---
+ return `---
${imports}
${frontmatterData.layout ? `import {__renderPage as __layout} from '${frontmatterData.layout}';` : 'const __layout = undefined;'}
export const __content = ${stringifiedSetupContext};
---
<section>${mdHtml}</section>`;
+}
+/**
+ * .md -> .jsx
+ * Core function processing Markdown, but along the way also calls convertAstroToJsx().
+ */
+async function convertMdToJsx(
+ contents: string,
+ { compileOptions, filename, fileID }: { compileOptions: CompileOptions; filename: string; fileID: string }
+): Promise<TransformResult> {
+ const raw = await convertMdToAstroSource(contents);
const convertOptions = { compileOptions, filename, fileID };
-
return await convertAstroToJsx(raw, convertOptions);
}