File tree Expand file tree Collapse file tree 9 files changed +80
-1
lines changed
content/tutorial/tests/current-entry Expand file tree Collapse file tree 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 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+ }
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' {
1717
1818declare const __ENTERPRISE__ : boolean ;
1919declare 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 ?? {};
2020// use lesson's default title and a default description for SEO metadata
2121meta .title ?? = title ;
2222meta .description ?? = ' A TutorialKit interactive lesson' ;
23+ Astro .locals .tk = { lesson };
2324---
2425
2526<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> {
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 ,
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments