Skip to content

Commit ca4d723

Browse files
committed
feat: support object type in template schema
1 parent d21d5e9 commit ca4d723

File tree

6 files changed

+25
-5
lines changed

6 files changed

+25
-5
lines changed

packages/astro/src/default/utils/content.ts

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export async function getTutorial(): Promise<Tutorial> {
242242
const partMetadata = _tutorial.parts[lesson.part.id].data;
243243
const chapterMetadata = _tutorial.parts[lesson.part.id].chapters[lesson.chapter.id].data;
244244

245+
// now we inherit options from upper levels
245246
lesson.data = {
246247
...lesson.data,
247248
...squash(

packages/astro/src/remark/import-file.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function getTemplateName(file: string) {
7878
);
7979

8080
if (meta.attributes.template) {
81-
return meta.attributes.template;
81+
return typeof meta.attributes.template === 'string' ? meta.attributes.template : meta.attributes.template.name;
8282
}
8383

8484
/**

packages/runtime/src/lesson-files.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class LessonFilesFetcher {
4242
}
4343

4444
async getLessonTemplate(lesson: Lesson): Promise<Files> {
45-
const templatePathname = `template-${lesson.data.template}.json`;
45+
const templateName = typeof lesson.data.template === 'string' ? lesson.data.template : lesson.data.template?.name;
46+
const templatePathname = `template-${templateName}.json`;
4647

4748
if (this._map.has(templatePathname)) {
4849
return this._map.get(templatePathname)!;

packages/template/src/content/tutorial/1-basics/1-introduction/1-welcome/content.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ prepareCommands:
1111
- ['node -e setTimeout(()=>{process.exit(1)},5000)', 'This is going to fail']
1212
terminal:
1313
panels: ['terminal', 'output']
14+
template:
15+
name: default
1416
---
1517

1618
# Kitchen Sink [Heading 1]

packages/types/src/schemas/common.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,22 @@ describe('webcontainerSchema', () => {
350350
}).not.toThrow();
351351
});
352352

353-
it('should allow specifying the template', () => {
353+
it('should allow specifying the template by name', () => {
354354
expect(() => {
355355
webcontainerSchema.parse({
356356
template: 'default',
357357
});
358358
}).not.toThrow();
359359
});
360+
it('should allow specifying the template by object type', () => {
361+
expect(() => {
362+
webcontainerSchema.parse({
363+
template: {
364+
name: 'default',
365+
},
366+
});
367+
}).not.toThrow();
368+
});
360369
it('should allow specifying the terminal', () => {
361370
expect(() => {
362371
webcontainerSchema.parse({

packages/types/src/schemas/common.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,15 @@ export const webcontainerSchema = commandsSchema.extend({
170170
'Navigating to a lesson that specifies autoReload will always reload the preview. This is typically only needed if your server does not support HMR.',
171171
),
172172
template: z
173-
.string()
174-
.optional()
173+
.union([
174+
// name of the template
175+
z.string().optional(),
176+
177+
z.strictObject({
178+
// name of the template
179+
name: z.string(),
180+
}),
181+
])
175182
.describe(
176183
'Specifies which folder from the `src/templates/` directory should be used as the basis for the code. See the "Code templates" guide for a detailed explainer.',
177184
),

0 commit comments

Comments
 (0)