Skip to content

feat: add "Edit this page" link #130

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

Merged
merged 3 commits into from
Jul 12, 2024
Merged
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
34 changes: 34 additions & 0 deletions docs/tutorialkit.dev/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ type I18nText = {
*/
partTemplate?: string,

/**
* Text of the edit page link.
*
* @default 'Edit this page'
*/
editPageText?: string

/**
* Text shown when there are no previews or steps to show in the prepare environment section.
*
Expand Down Expand Up @@ -170,3 +177,30 @@ Navigating to a lesson that specifies `autoReload` will always reload the previe
Specified which folder from the `src/templates/` directory should be used as the basis for the code. See the "[Code templates](/guides/creating-content/#code-templates)" guide for a detailed explainer.
<PropertyTable inherited type="string" />

#### `editPageLink`
Display a link in lesson for editing the page content.
The value is a URL pattern where `${path}` is replaced with the lesson's location relative to the `src/content/tutorial`.

<PropertyTable inherited type="string|false" />

```yaml
editPageLink: https://github.com/stackblitz/tutorialkit/blob/main/packages/template/src/content/tutorial/${path}
```

The inherited value can be disabled in specific parts using `false`.

```yaml
editPageLink: false
```

:::tip
Note that Github will try to automatically render the `.md` files when linked to.
You can instruct Github to show the source code instead by adding `plain=1` query parameter.

```diff
-editPageLink: https://github.com/stackblitz/tutorialkit/blob/main/packages/template/src/content/tutorial/${path}
+editPageLink: https://github.com/stackblitz/tutorialkit/blob/main/packages/template/src/content/tutorial/${path}?plain=1
```

:::

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions docs/tutorialkit.dev/src/content/docs/reference/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,20 @@ The navigation cards are the cards at the bottom of a lesson to navigate to the
| `--tk-elements-navCard-iconColor` | The icon color of the navigation card. |
| `--tk-elements-navCard-iconColorHover` | The icon color of the navigation card when hovering. |

### Edit Page Link

The edit page link is shown above the navigation cards when configured by [`editPageLink` option](/reference/configuration/#editpagelink).

![Edit Page Link](./images/theming-editpagelink.png)

| Token | Description |
| ------------------------------------------- | --------------------------------------------------- |
| `--tk-elements-editPageLink-textColor` | The text color of the edit page link |
| `--tk-elements-editPageLink-textColorHover` | The text color of the edit page link when hovering. |
| `--tk-elements-editPageLink-iconColor` | The icon color of the edit page link |
| `--tk-elements-editPageLink-iconColorHover` | The icon color of the edit page link when hovering |
| `--tk-elements-editPageLink-borderColor` | The border color of the edit page link |

### Breadcrumbs

The breadcrumbs are the navigation elements that show the path of the current lesson. The breadcrumbs are divided into multiple parts.
Expand Down
17 changes: 16 additions & 1 deletion packages/astro/src/default/components/TutorialContent.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ interface Props {
}

const { lesson } = Astro.props;
const { Markdown, prev, next } = lesson;
const { Markdown, editPageLink, prev, next } = lesson;
---

<div class="h-full overflow-auto scrollbar-transparent p-6 sm:p-8">
<div class="markdown-content text-tk-elements-content-textColor">
<Markdown />
</div>

{
editPageLink && (
<div class="pb-4 mt-8 border-b border-tk-elements-editPageLink-borderColor">
<a
href={editPageLink}
class="inline-flex flex-items-center text-tk-elements-editPageLink-textColor hover:text-tk-elements-editPageLink-textColorHover hover:underline"
>
<span class="icon i-ph-note-pencil pointer-events-none h-5 w-5 mr-2 text-tk-elements-editPageLink-iconColor group-hover:text-tk-elements-editPageLink-iconColorHover" />
<span>{lesson.data.i18n!.editPageText}</span>
</a>
</div>
)
}

<div class="grid grid-cols-[1fr_1fr] gap-4 mt-8">
<div class="flex">
{prev && <NavCard lesson={prev} type="prev" />}
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/default/styles/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@
--tk-elements-navCard-iconColor: var(--tk-elements-app-textColor);
--tk-elements-navCard-iconColorHover: var(--tk-text-accent);

/* Edit Page Link */
--tk-elements-editPageLink-textColor: var(--tk-elements-app-textColor);
--tk-elements-editPageLink-textColorHover: var(--tk-text-active);
--tk-elements-editPageLink-iconColor: var(--tk-elements-app-textColor);
--tk-elements-editPageLink-iconColorHover: var(--tk-text-accent);
--tk-elements-editPageLink-borderColor: var(--tk-border-secondary);

/* Breadcrumb > Nav Button */
--tk-elements-breadcrumbs-navButton-iconColor: var(--tk-text-secondary);
--tk-elements-breadcrumbs-navButton-iconColorHover: var(--tk-text-active);
Expand Down
23 changes: 19 additions & 4 deletions packages/astro/src/default/utils/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Tutorial,
TutorialSchema,
} from '@tutorialkit/types';
import { folderPathToFilesRef } from '@tutorialkit/types';
import { folderPathToFilesRef, interpolateString } from '@tutorialkit/types';
import { getCollection } from 'astro:content';
import glob from 'fast-glob';
import path from 'node:path';
Expand Down Expand Up @@ -44,6 +44,7 @@ export async function getTutorial(): Promise<Tutorial> {
partTemplate: 'Part ${index}: ${title}',
noPreviewNorStepsText: 'No preview to run nor steps to show',
startWebContainerText: 'Run this tutorial',
editPageText: 'Edit this page',
} satisfies Lesson['data']['i18n'],
tutorialMetaData.i18n,
);
Expand Down Expand Up @@ -90,6 +91,7 @@ export async function getTutorial(): Promise<Tutorial> {
const lesson: Lesson = {
data,
id: lessonId,
filepath: id,
order: -1,
part: {
id: partId,
Expand Down Expand Up @@ -251,7 +253,18 @@ export async function getTutorial(): Promise<Tutorial> {
...lesson.data,
...squash(
[lesson.data, chapterMetadata, partMetadata, tutorialMetaData],
['mainCommand', 'prepareCommands', 'previews', 'autoReload', 'template', 'terminal', 'editor', 'focus', 'i18n'],
[
'mainCommand',
'prepareCommands',
'previews',
'autoReload',
'template',
'terminal',
'editor',
'focus',
'i18n',
'editPageLink',
],
),
};

Expand All @@ -274,9 +287,11 @@ export async function getTutorial(): Promise<Tutorial> {
href: joinPaths(baseURL, `/${partSlug}/${chapterSlug}/${nextLesson.slug}`),
};
}
}

// console.log(inspect(_tutorial, undefined, Infinity, true));
if (lesson.data.editPageLink && typeof lesson.data.editPageLink === 'string') {
lesson.editPageLink = interpolateString(lesson.data.editPageLink, { path: lesson.filepath });
}
}

return _tutorial;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/components/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
"scripts": {
"build": "node ./scripts/build.js",
"test": "vitest"
"test": "vitest --passWithNoTests"
},
"dependencies": {
"@codemirror/autocomplete": "^6.16.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/components/react/src/Nav.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Lesson, NavItem, NavList } from '@tutorialkit/types';
import { interpolateString, type Lesson, type NavItem, type NavList } from '@tutorialkit/types';
import * as Accordion from '@radix-ui/react-accordion';
import navStyles from './styles/nav.module.css';
import { classNames } from './utils/classnames.js';
import { AnimatePresence, cubicBezier, motion } from 'framer-motion';
import { useCallback, useRef, useState } from 'react';
import { useOutsideClick } from './hooks/useOutsideClick.js';
import { interpolateString } from './utils/interpolation.js';

const dropdownEasing = cubicBezier(0.4, 0, 0.2, 1);

Expand Down
1 change: 0 additions & 1 deletion packages/components/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export * from './Panels/TerminalPanel.js';
export * from './Panels/WorkspacePanel.js';
export type * from './core/types.js';
export * from './utils/classnames.js';
export * from './utils/interpolation.js';
7 changes: 7 additions & 0 deletions packages/theme/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ export const theme = {
iconColor: 'var(--tk-elements-navCard-iconColor)',
iconColorHover: 'var(--tk-elements-navCard-iconColorHover)',
},
editPageLink: {
textColor: 'var(--tk-elements-editPageLink-textColor)',
textColorHover: 'var(--tk-elements-editPageLink-textColorHover)',
iconColor: 'var(--tk-elements-editPageLink-iconColor)',
iconColorHover: 'var(--tk-elements-editPageLink-iconColorHover)',
borderColor: 'var(--tk-elements-editPageLink-borderColor)',
},
breadcrumbs: {
navButton: {
iconColor: 'var(--tk-elements-breadcrumbs-navButton-iconColor)',
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface Lesson<T = unknown> {
part: { id: string; title: string };
chapter: { id: string; title: string };
slug: string;
filepath: string;
editPageLink?: string;
files: FilesRefList;
solution: FilesRefList;
next?: LessonLink;
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type * from './entities/index.js';
export * from './schemas/index.js';
export * from './files-ref.js';
export { interpolateString } from './utils/interpolation.js';
9 changes: 9 additions & 0 deletions packages/types/src/schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ export const webcontainerSchema = commandsSchema.extend({
}),
]),
i18n: i18nSchema.optional(),
editPageLink: z
.union([
// pattern for creating the URL
z.string(),

// `false` for disabling the edit link
z.boolean(),
])
.optional(),
});

export const baseSchema = webcontainerSchema.extend({
Expand Down
7 changes: 7 additions & 0 deletions packages/types/src/schemas/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export const i18nSchema = z.object({
*/
partTemplate: z.string().optional(),

/**
* Text of the edit page link.
*
* @default 'Edit this page'
*/
editPageText: z.string().optional(),

/**
* Text shown when there are no previews or steps to show in the prepare environment section.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ describe('interpolateString', () => {
expect(interpolateString(template, variables)).toBe('Hello, world!');
});

it('should interpolate variables inside URL', () => {
const template =
'https://github.com/stackblitz/tutorialkit/blob/main/packages/template/src/content/tutorial/${path}?plain=1';
const variables = { path: '1-basics/2-foo/1-welcome/content.md' };

expect(interpolateString(template, variables)).toBe(
'https://github.com/stackblitz/tutorialkit/blob/main/packages/template/src/content/tutorial/1-basics/2-foo/1-welcome/content.md?plain=1',
);
});

it('should interpolate multiple variables', () => {
const template = 'Part ${index}: ${title}';
const variables = { index: 5, title: 'Welcome to foo bar' };
Expand Down