diff --git a/packages/astro/snowpack-plugin.cjs b/packages/astro/snowpack-plugin.cjs index 3e1edecc7b86..40d0c420a44f 100644 --- a/packages/astro/snowpack-plugin.cjs +++ b/packages/astro/snowpack-plugin.cjs @@ -35,6 +35,7 @@ module.exports = (snowpackConfig, options = {}) => { input: ['.astro', '.md'], output: ['.js', '.css'], }, + exclude: ["**/default.astro"], async transform({contents, id, fileExt}) { if(configManager.isConfigModule(fileExt, id)) { configManager.configModuleId = id; diff --git a/packages/astro/src/compiler/index.ts b/packages/astro/src/compiler/index.ts index ff9c47680187..c7ae4ca488c2 100644 --- a/packages/astro/src/compiler/index.ts +++ b/packages/astro/src/compiler/index.ts @@ -1,5 +1,6 @@ import type { CompileResult, TransformResult } from '../@types/astro'; import type { CompileOptions } from '../@types/compiler.js'; +import findUp from 'find-up'; import path from 'path'; import { MarkdownRenderingOptions, renderMarkdownWithFrontmatter } from '@astrojs/markdown-support'; @@ -59,6 +60,13 @@ export async function convertMdToAstroSource(contents: string, { filename }: { f // Break it up here so that the HTML parser won't detect it. const stringifiedSetupContext = JSON.stringify(contentData).replace(/\<\/script\>/g, ``); + if (!layout) { + const defaultTemplate = await findUp('default.astro', { cwd: filename }); + if(defaultTemplate) { + layout = "./" + path.relative(path.dirname(filename), defaultTemplate) + } + } + return `--- ${layout ? `import {__renderPage as __layout} from '${layout}';` : 'const __layout = undefined;'} export const __content = ${stringifiedSetupContext}; diff --git a/packages/astro/test/fixtures/plain-markdown/src/layouts/content.astro b/packages/astro/test/fixtures/plain-markdown/src/layouts/content.astro index 925a243a9368..7e09ec582602 100644 --- a/packages/astro/test/fixtures/plain-markdown/src/layouts/content.astro +++ b/packages/astro/test/fixtures/plain-markdown/src/layouts/content.astro @@ -3,6 +3,7 @@ +
content
diff --git a/packages/astro/test/fixtures/plain-markdown/src/pages/default.astro b/packages/astro/test/fixtures/plain-markdown/src/pages/default.astro new file mode 100644 index 000000000000..ab577ee6fa88 --- /dev/null +++ b/packages/astro/test/fixtures/plain-markdown/src/pages/default.astro @@ -0,0 +1,11 @@ + + + + + +
default
+
+ +
+ + diff --git a/packages/astro/test/plain-markdown.test.js b/packages/astro/test/plain-markdown.test.js index 3adc904f589a..d294e5b8ef09 100644 --- a/packages/astro/test/plain-markdown.test.js +++ b/packages/astro/test/plain-markdown.test.js @@ -15,6 +15,7 @@ Markdown('Can load a simple markdown page with Astro', async ({ runtime }) => { const $ = doc(result.contents); + assert.equal($('#layout').text(), "content"); assert.equal($('p').first().text(), 'Hello world!'); assert.equal($('#first').text(), 'Some content'); assert.equal($('#interesting-topic').text(), 'Interesting Topic'); @@ -27,6 +28,7 @@ Markdown('Can load a realworld markdown page with Astro', async ({ runtime }) => assert.equal(result.statusCode, 200); const $ = doc(result.contents); + assert.equal($('#layout').text(), "default"); assert.equal($('pre').length, 7); });