File tree 9 files changed +80
-1
lines changed
content/tutorial/tests/current-entry
9 files changed +80
-1
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ ---
2
+ type : lesson
3
+ title : Basic
4
+ terminal :
5
+ panels : terminal
6
+ ---
7
+
8
+ import CurrentEntry from " @components/CurrentEntry.astro"
9
+
10
+ <CurrentEntry />
Original file line number Diff line number Diff line change
1
+ ---
2
+ type : chapter
3
+ title : Current Entry
4
+ ---
Original file line number Diff line number Diff line change 1
1
/// <reference path="../.astro/types.d.ts" />
2
2
/// <reference types="@tutorialkit/astro/types" />
3
3
/// <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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change @@ -17,3 +17,11 @@ declare module 'tutorialkit:override-components' {
17
17
18
18
declare const __ENTERPRISE__ : boolean ;
19
19
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
+ }
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ const meta = lesson.data?.meta ?? {};
20
20
// use lesson's default title and a default description for SEO metadata
21
21
meta .title ?? = title ;
22
22
meta .description ?? = ' A TutorialKit interactive lesson' ;
23
+ Astro .locals .tk = { lesson };
23
24
---
24
25
25
26
<Layout title ={ title } meta ={ meta } >
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ export async function getTutorial(): Promise<Tutorial> {
18
18
let lessons : Lesson [ ] = [ ] ;
19
19
20
20
for ( const entry of collection ) {
21
- const { id, data } = entry ;
21
+ const { id, data, slug : entrySlug } = entry ;
22
22
const { type } = data ;
23
23
24
24
const [ partId , chapterId , lessonId ] = id . split ( '/' ) ;
@@ -74,6 +74,7 @@ export async function getTutorial(): Promise<Tutorial> {
74
74
data,
75
75
id : lessonId ,
76
76
filepath : id ,
77
+ entrySlug,
77
78
order : - 1 ,
78
79
part : {
79
80
id : partId ,
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ export interface Lesson<T = unknown> {
45
45
part : { id : string ; title : string } ;
46
46
chapter : { id : string ; title : string } ;
47
47
slug : string ;
48
+
49
+ // slug to pass to astro:content `getEntry`
50
+ entrySlug : string ;
48
51
filepath : string ;
49
52
editPageLink ?: string ;
50
53
files : FilesRefList ;
You can’t perform that action at this time.
0 commit comments