Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: access current lesson from Astro.locals #398

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions e2e/src/components/CurrentEntry.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
import file from '@content/tutorial/tests/file-tree/allow-edits-disabled/_files/first-level/file';
import { getCollection } from 'astro:content';
import { getEntry, getEntries } from 'astro:content';

if (!Astro.locals.tk) {
throw new Error('Not in the context of a lesson, Astro.locals.tk is not defined');
}
if (!Astro.locals.tk.lesson) {
throw new Error('Lesson not set in tutorialkit Astro.locals.tk context');
}

const { entrySlug } = Astro.locals.tk.lesson;

const currentEntry = await getEntry('tutorial', entrySlug);
if (!currentEntry) {
throw new Error(`Entry not found for slug: ${entrySlug}.`);
}
---

<div>
<h2>Lesson</h2>
{
// @ts-ignore
JSON.stringify(Astro.locals.tk.lesson, null, 2)
}
<h2>Entry</h2>
{JSON.stringify(currentEntry, null, 2)}
</div>
10 changes: 10 additions & 0 deletions e2e/src/content/tutorial/tests/current-entry/basic/content.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
type: lesson
title: Basic
terminal:
panels: terminal
---

import CurrentEntry from "@components/CurrentEntry.astro"

<CurrentEntry />
4 changes: 4 additions & 0 deletions e2e/src/content/tutorial/tests/current-entry/meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
type: chapter
title: Current Entry
---
10 changes: 10 additions & 0 deletions e2e/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference types="@tutorialkit/astro/types" />
/// <reference types="astro/client" />

// copied from packages/astro/src/default/env-default.d.ts
// TODO: should probably be exposed by astro/types instead?
declare namespace App {
interface Locals {
tk: {
lesson: import('@tutorialkit/types').Lesson<any>;
};
}
}
13 changes: 13 additions & 0 deletions e2e/test/current-entry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test, expect } from '@playwright/test';

const BASE_URL = '/tests/current-entry';

test('developer can access current lesson and collection entry from Astro.locals', async ({ page }) => {
await page.goto(`${BASE_URL}/basic`);

// lesson id
await expect(page.getByText('"id": "basic"')).toBeVisible();

// astro collection entry id
await expect(page.getByText('"id": "tests/current-entry/basic/content.mdx"')).toBeVisible();
});
8 changes: 8 additions & 0 deletions packages/astro/src/default/env-default.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ declare module 'tutorialkit:override-components' {

declare const __ENTERPRISE__: boolean;
declare const __WC_CONFIG__: WebContainerConfig | undefined;

declare namespace App {
interface Locals {
tk: {
lesson: import('@tutorialkit/types').Lesson<any>;
};
}
}
1 change: 1 addition & 0 deletions packages/astro/src/default/pages/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const meta = lesson.data?.meta ?? {};
// use lesson's default title and a default description for SEO metadata
meta.title ??= title;
meta.description ??= 'A TutorialKit interactive lesson';
Astro.locals.tk = { lesson };
---

<Layout title={title} meta={meta}>
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/default/utils/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getTutorial(): Promise<Tutorial> {
let lessons: Lesson[] = [];

for (const entry of collection) {
const { id, data } = entry;
const { id, data, slug: entrySlug } = entry;
const { type } = data;

const [partId, chapterId, lessonId] = id.split('/');
Expand Down Expand Up @@ -74,6 +74,7 @@ export async function getTutorial(): Promise<Tutorial> {
data,
id: lessonId,
filepath: id,
entrySlug,
order: -1,
part: {
id: partId,
Expand Down
3 changes: 3 additions & 0 deletions packages/types/src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export interface Lesson<T = unknown> {
part: { id: string; title: string };
chapter: { id: string; title: string };
slug: string;

// slug to pass to astro:content `getEntry`
entrySlug: string;
filepath: string;
editPageLink?: string;
files: FilesRefList;
Expand Down
Loading