Skip to content

Commit

Permalink
feat: support object type in template schema
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 24, 2024
1 parent bc0fde2 commit fee4b29
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/default/utils/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export async function getTutorial(): Promise<Tutorial> {
const partMetadata = _tutorial.parts[lesson.part.id].data;
const chapterMetadata = _tutorial.parts[lesson.part.id].chapters[lesson.chapter.id].data;

// now we inherit options from upper levels
lesson.data = {
...lesson.data,
...squash(
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/remark/import-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getTemplateName(file: string) {
);

if (meta.attributes.template) {
return meta.attributes.template;
return typeof meta.attributes.template === 'string' ? meta.attributes.template : meta.attributes.template.name;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime/src/lesson-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class LessonFilesFetcher {
}

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

if (this._map.has(templatePathname)) {
return this._map.get(templatePathname)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ prepareCommands:
- ['node -e setTimeout(()=>{process.exit(1)},5000)', 'This is going to fail']
terminal:
panels: ['terminal', 'output']
template:
name: default
---

# Kitchen Sink [Heading 1]
Expand Down
11 changes: 10 additions & 1 deletion packages/types/src/schemas/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,22 @@ describe('webcontainerSchema', () => {
}).not.toThrow();
});

it('should allow specifying the template', () => {
it('should allow specifying the template by name', () => {
expect(() => {
webcontainerSchema.parse({
template: 'default',
});
}).not.toThrow();
});
it('should allow specifying the template by object type', () => {
expect(() => {
webcontainerSchema.parse({
template: {
name: 'default',
},
});
}).not.toThrow();
});
it('should allow specifying the terminal', () => {
expect(() => {
webcontainerSchema.parse({
Expand Down
10 changes: 9 additions & 1 deletion packages/types/src/schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ export type TerminalSchema = z.infer<typeof terminalSchema>;
export const webcontainerSchema = commandsSchema.extend({
previews: previewSchema.optional(),
autoReload: z.boolean().optional(),
template: z.string().optional(),
template: z.union([
// name of the template
z.string().optional(),

z.strictObject({
// name of the template
name: z.string(),
}),
]),
terminal: terminalSchema.optional(),
focus: z.string().optional(),
editor: z.union([
Expand Down

0 comments on commit fee4b29

Please sign in to comment.