Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(docs): add edit this page links (#1496)

Corentin Thomasset60f055868aa6
diff --git a/apps/website/src/docs/components/DocFooter.astro b/apps/website/src/docs/components/DocFooter.astro
index 643ad863..a173f889 100644
--- a/apps/website/src/docs/components/DocFooter.astro
+++ b/apps/website/src/docs/components/DocFooter.astro
@@ -1,55 +1,81 @@
---
-import { getCollection } from 'astro:content';
-import type { Locale } from '../../i18n/i18n.constants';
-import type { DocsItem } from '../docs.types';
-import { useI18n } from '../../i18n/i18n';
-import { getDocUrl, resolveDoc } from '../docs.models';
+import { getCollection } from "astro:content";
+import type { Locale } from "../../i18n/i18n.constants";
+import type { DocsItem } from "../docs.types";
+import { useI18n } from "../../i18n/i18n";
+import { getDocUrl, resolveDoc } from "../docs.models";
export type Props = {
- previous?: DocsItem;
- next?: DocsItem;
- locale: Locale;
+ editUrl?: string;
+ previous?: DocsItem;
+ next?: DocsItem;
+ locale: Locale;
};
-const { previous, next, locale } = Astro.props;
+const { editUrl, previous, next, locale } = Astro.props;
const { t } = useI18n({ locale });
-const docs = await getCollection('docs');
-const previousDoc = previous ? resolveDoc({ docs, docId: previous.docId, locale }) : undefined;
-const nextDoc = next ? resolveDoc({ docs, docId: next.docId, locale }) : undefined;
-
+const docs = await getCollection("docs");
+const previousDoc = previous
+ ? resolveDoc({ docs, docId: previous.docId, locale })
+ : undefined;
+const nextDoc = next
+ ? resolveDoc({ docs, docId: next.docId, locale })
+ : undefined;
---
-
-
-{(previousDoc || nextDoc) && (
-<div>
-
- <hr class="my-8 border-t border-muted-foreground/10" />
-
- <nav aria-label={t('docs.pagination-label')} class="grid grid-cols-1 md:grid-cols-2 gap-4 pb-32">
- {previousDoc ? (
- <a href={getDocUrl({ docId: previousDoc.docId, locale })} class="flex flex-col gap-1 group transition rounded-lg px-6 py-3 border hover:border-primary ">
- <div class="flex items-center gap-2 text-muted-foreground">
- <div class="i-tabler-arrow-left size-4"></div>
- {t('docs.previous')}
- </div>
- <div class="text-lg transition group-hover:text-primary">{previousDoc.entry.data.title}</div>
+<div class="pb-32">
+ {editUrl && (
+ <div class="mb-8">
+ <a
+ href={editUrl}
+ target="_blank"
+ rel="noopener noreferrer"
+ class="inline-flex items-center gap-2 text-sm text-muted-foreground hover:text-primary transition"
+ >
+ <span class="i-tabler-pencil size-4" aria-hidden="true"></span>
+ {t("docs.edit-this-page")}
</a>
- ): (
- <div ></div>
- )}
+ </div>
+ )}
- {nextDoc && (
- <a href={getDocUrl({ docId: nextDoc.docId, locale })} class="flex flex-col gap-1 group transition text-right rounded-lg px-6 py-3 border hover:border-primary ">
- <div class="flex items-center gap-2 text-muted-foreground justify-end">
- {t('docs.next')}
- <div class="i-tabler-arrow-right size-4"></div>
- </div>
- <div class="text-lg transition group-hover:text-primary">{nextDoc.entry.data.title}</div>
- </a>
- )}
+ <hr class="my-8 border-t border-muted-foreground/10" />
- </nav>
+ {(previousDoc || nextDoc) && (
+ <nav
+ aria-label={t("docs.pagination-label")}
+ class="grid grid-cols-1 md:grid-cols-2 gap-4"
+ >
+ {previousDoc ? (
+ <a
+ href={getDocUrl({ docId: previousDoc.docId, locale })}
+ class="flex flex-col gap-1 group transition rounded-lg px-6 py-3 border hover:border-primary"
+ >
+ <div class="flex items-center gap-2 text-muted-foreground">
+ <div class="i-tabler-arrow-left size-4"></div>
+ {t("docs.previous")}
+ </div>
+ <div class="text-lg transition group-hover:text-primary">
+ {previousDoc.entry.data.title}
+ </div>
+ </a>
+ ) : (
+ <div></div>
+ )}
+ {nextDoc && (
+ <a
+ href={getDocUrl({ docId: nextDoc.docId, locale })}
+ class="flex flex-col gap-1 group transition text-right rounded-lg px-6 py-3 border hover:border-primary"
+ >
+ <div class="flex items-center gap-2 text-muted-foreground justify-end">
+ {t("docs.next")}
+ <div class="i-tabler-arrow-right size-4"></div>
+ </div>
+ <div class="text-lg transition group-hover:text-primary">
+ {nextDoc.entry.data.title}
+ </div>
+ </a>
+ )}
+ </nav>
+ )}
</div>
-)}
diff --git a/apps/website/src/docs/docs.models.test.ts b/apps/website/src/docs/docs.models.test.ts
index ab960a5a..bb2d75b0 100644
--- a/apps/website/src/docs/docs.models.test.ts
+++ b/apps/website/src/docs/docs.models.test.ts
@@ -2,6 +2,7 @@ import type { CollectionEntry } from 'astro:content';
import { describe, expect, test } from 'vitest';
import {
getDocContext,
+ getDocEditUrl,
getDocPagination,
getDocStaticPaths,
getDocUrl,
@@ -9,7 +10,12 @@ import {
} from './docs.models';
function doc(id: string, title = id): CollectionEntry<'docs'> {
- return { id, collection: 'docs', data: { title, description: title } };
+ return {
+ id,
+ collection: 'docs',
+ filePath: `src/docs/content/${id}.mdx`,
+ data: { title, description: title },
+ };
}
const docs = [
@@ -34,6 +40,35 @@ describe('getDocUrl', () => {
});
});
+describe('getDocEditUrl', () => {
+ test.each([
+ ['en', 'self-hosting/installation/docker', 'en/self-hosting/installation/docker'],
+ ['fr', 'self-hosting/getting-started', 'fr/self-hosting/getting-started'],
+ ['fr', 'self-hosting/installation/docker', 'en/self-hosting/installation/docker'],
+ ] as const)('links to the resolved source for %s/%s', (locale, docId, sourceId) => {
+ const { entry } = resolveDoc({ docs, docId, locale });
+
+ expect(getDocEditUrl(entry)).toBe(
+ `https://github.com/papra-hq/papra/edit/main/apps/website/src/docs/content/${sourceId}.mdx`,
+ );
+ });
+
+ test('preserves the source filename and extension and encodes URL-sensitive characters', () => {
+ const entry = {
+ ...doc('en/unlisted'),
+ filePath: 'src/docs/content/en/Unlisted page #1.md',
+ };
+
+ expect(getDocEditUrl(entry)).toBe(
+ 'https://github.com/papra-hq/papra/edit/main/apps/website/src/docs/content/en/Unlisted%20page%20%231.md',
+ );
+ });
+
+ test('omits the link for entries without a source file', () => {
+ expect(getDocEditUrl({ ...doc('en/unlisted'), filePath: undefined })).toBeUndefined();
+ });
+});
+
describe('resolveDoc', () => {
test('resolves English without marking it as a fallback', () => {
expect(resolveDoc({ docs, docId: 'self-hosting/getting-started' })).toEqual({
diff --git a/apps/website/src/docs/docs.models.ts b/apps/website/src/docs/docs.models.ts
index 55058aa6..3f1a9ea1 100644
--- a/apps/website/src/docs/docs.models.ts
+++ b/apps/website/src/docs/docs.models.ts
@@ -3,6 +3,7 @@ import type { Locale } from '../i18n/i18n.constants';
import type { DocsItem } from './docs.types';
import { buildLocalizedPath } from '../i18n/i18n';
import { DEFAULT_LOCALE } from '../i18n/i18n.constants';
+import { GITHUB_REPO_URL } from '../socials';
import { DOCS_LOCALES } from './docs.constants';
import { docCategories } from './docs.navigations';
@@ -52,6 +53,16 @@ export function getDocUrl({
return buildLocalizedPath({ locale, path: docId ? `/docs/${docId}` : '/docs' });
}
+export function getDocEditUrl(entry: CollectionEntry<'docs'>): string | undefined {
+ if (!entry.filePath) {
+ return undefined;
+ }
+
+ // Use the resolved source file, not the route or normalized collection ID.
+ const filePath = entry.filePath.split('/').map(encodeURIComponent).join('/');
+ return `${GITHUB_REPO_URL}/edit/main/apps/website/${filePath}`;
+}
+
// Collection IDs include the locale; navigation and public helpers use logical IDs.
export function resolveDoc({
docs,
diff --git a/apps/website/src/locales/en.ts b/apps/website/src/locales/en.ts
index f2cca710..47d855f5 100644
--- a/apps/website/src/locales/en.ts
+++ b/apps/website/src/locales/en.ts
@@ -368,6 +368,7 @@ export const translations = {
'docs.pagination-label': 'Previous and next pages',
'docs.previous': 'Previous',
'docs.next': 'Next',
+ 'docs.edit-this-page': 'Edit this page',
'docs.translation-fallback':
'This page is not available in your language yet. Showing the English version.',
'docs.search.title': 'Search documentation',
diff --git a/apps/website/src/locales/fr.ts b/apps/website/src/locales/fr.ts
index a10a98db..6bb3fcd5 100644
--- a/apps/website/src/locales/fr.ts
+++ b/apps/website/src/locales/fr.ts
@@ -354,6 +354,9 @@ export const translations: Partial<TranslationsDictionary> = {
'launch-banner.text':
'Dernière chance : {{percentage}}% de réduction à vie - Expire le 31 décembre 2025 !',
+ // Documentation
+ 'docs.edit-this-page': 'Modifier cette page',
+
// Pagination
'pagination.go-to-page': 'Aller à la page {{page}} sur {{total}}',
'pagination.page': 'Page {{current}} sur {{total}}',
diff --git a/apps/website/src/pages/[locale]/docs/[...slug].astro b/apps/website/src/pages/[locale]/docs/[...slug].astro
index 04dda661..8a808740 100644
--- a/apps/website/src/pages/[locale]/docs/[...slug].astro
+++ b/apps/website/src/pages/[locale]/docs/[...slug].astro
@@ -1,7 +1,7 @@
---
import { getCollection, render } from 'astro:content';
import DocFooter from '../../../docs/components/DocFooter.astro';
-import { getDocPagination, getDocStaticPaths } from '../../../docs/docs.models';
+import { getDocEditUrl, getDocPagination, getDocStaticPaths } from '../../../docs/docs.models';
import { useI18n } from '../../../i18n/i18n';
import DocsLayout from '../../../layouts/DocsLayout.astro';
@@ -37,7 +37,7 @@ const { previous, next } = getDocPagination(docId);
<Content />
</div>
- <DocFooter previous={previous} next={next} locale={requestedLocale} />
+ <DocFooter editUrl={getDocEditUrl(entry)} previous={previous} next={next} locale={requestedLocale} />
</DocsLayout>
<style is:global>