Skip to content

Commit a26419b

Browse files
committed
access current lesson from Astro.locals
1 parent a4bbe78 commit a26419b

File tree

9 files changed

+80
-1
lines changed

9 files changed

+80
-1
lines changed

Diff for: e2e/src/components/CurrentEntry.astro

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
import file from '@content/tutorial/tests/file-tree/allow-edits-disabled/_files/first-level/file';
3+
import { getCollection } from 'astro:content';
4+
import { getEntry, getEntries } from 'astro:content';
5+
6+
if (!Astro.locals.tk) {
7+
throw new Error('Not in the context of a lesson, Astro.locals.tk is not defined');
8+
}
9+
if (!Astro.locals.tk.lesson) {
10+
throw new Error('Lesson not set in tutorialkit Astro.locals.tk context');
11+
}
12+
13+
const { entrySlug } = Astro.locals.tk.lesson;
14+
15+
const currentEntry = await getEntry('tutorial', entrySlug);
16+
if (!currentEntry) {
17+
throw new Error(`Entry not found for slug: ${entrySlug}.`);
18+
}
19+
---
20+
21+
<div>
22+
<h2>Lesson</h2>
23+
{
24+
// @ts-ignore
25+
JSON.stringify(Astro.locals.tk.lesson, null, 2)
26+
}
27+
<h2>Entry</h2>
28+
{JSON.stringify(currentEntry, null, 2)}
29+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
type: lesson
3+
title: Basic
4+
terminal:
5+
panels: terminal
6+
---
7+
8+
import CurrentEntry from "@components/CurrentEntry.astro"
9+
10+
<CurrentEntry />

Diff for: e2e/src/content/tutorial/tests/current-entry/meta.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
type: chapter
3+
title: Current Entry
4+
---

Diff for: e2e/src/env.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
/// <reference path="../.astro/types.d.ts" />
22
/// <reference types="@tutorialkit/astro/types" />
33
/// <reference types="astro/client" />
4+
5+
// copied from packages/astro/src/default/env-default.d.ts
6+
// TODO: should probably be exposed by astro/types instead?
7+
declare namespace App {
8+
interface Locals {
9+
tk: {
10+
lesson: import('@tutorialkit/types').Lesson<any>;
11+
};
12+
}
13+
}

Diff for: e2e/test/current-entry.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
const BASE_URL = '/tests/current-entry';
4+
5+
test('developer can access current lesson and collection entry from Astro.locals', async ({ page }) => {
6+
await page.goto(`${BASE_URL}/basic`);
7+
8+
// lesson id
9+
await expect(page.getByText('"id": "basic"')).toBeVisible();
10+
11+
// astro collection entry id
12+
await expect(page.getByText('"id": "tests/current-entry/basic/content.mdx"')).toBeVisible();
13+
});

Diff for: packages/astro/src/default/env-default.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ declare module 'tutorialkit:override-components' {
1717

1818
declare const __ENTERPRISE__: boolean;
1919
declare const __WC_CONFIG__: WebContainerConfig | undefined;
20+
21+
declare namespace App {
22+
interface Locals {
23+
tk: {
24+
lesson: import('@tutorialkit/types').Lesson<any>;
25+
};
26+
}
27+
}

Diff for: packages/astro/src/default/pages/[...slug].astro

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const meta = lesson.data?.meta ?? {};
2020
// use lesson's default title and a default description for SEO metadata
2121
meta.title ??= title;
2222
meta.description ??= 'A TutorialKit interactive lesson';
23+
Astro.locals.tk = { lesson };
2324
---
2425

2526
<Layout title={title} meta={meta}>

Diff for: packages/astro/src/default/utils/content.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function getTutorial(): Promise<Tutorial> {
1818
let lessons: Lesson[] = [];
1919

2020
for (const entry of collection) {
21-
const { id, data } = entry;
21+
const { id, data, slug: entrySlug } = entry;
2222
const { type } = data;
2323

2424
const [partId, chapterId, lessonId] = id.split('/');
@@ -74,6 +74,7 @@ export async function getTutorial(): Promise<Tutorial> {
7474
data,
7575
id: lessonId,
7676
filepath: id,
77+
entrySlug,
7778
order: -1,
7879
part: {
7980
id: partId,

Diff for: packages/types/src/entities/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export interface Lesson<T = unknown> {
4545
part: { id: string; title: string };
4646
chapter: { id: string; title: string };
4747
slug: string;
48+
49+
// slug to pass to astro:content `getEntry`
50+
entrySlug: string;
4851
filepath: string;
4952
editPageLink?: string;
5053
files: FilesRefList;

0 commit comments

Comments
 (0)