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

Add default layout look up using findUp #732

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions packages/astro/snowpack-plugin.cjs
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions packages/astro/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -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, `</scrip" + "t>`);

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};
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
<!-- Head Stuff -->
</head>
<body>
<div id="layout">content</div>
<div class="container">
<slot></slot>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<!-- Head Stuff -->
</head>
<body>
<div id="layout">default</div>
<div class="container">
<slot></slot>
</div>
</body>
</html>
2 changes: 2 additions & 0 deletions packages/astro/test/plain-markdown.test.js
Original file line number Diff line number Diff line change
@@ -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);
});