mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add link to generate links in PDF mode
This commit is contained in:
+58
-2
@@ -1,6 +1,15 @@
|
||||
import 'server-only';
|
||||
|
||||
import { headers } from 'next/headers';
|
||||
import { RevisionPageDocument } from '@gitbook/api';
|
||||
|
||||
export interface PageHrefContext {
|
||||
/**
|
||||
* If defined, we are generating a PDF of the specific page IDs,
|
||||
* and these pages will be rendered in the same HTML output.
|
||||
*/
|
||||
pdf?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base path for the current request.
|
||||
@@ -10,9 +19,56 @@ export function basePath(): string {
|
||||
return headersList.get('x-gitbook-basepath') ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an absolute href in the current content.
|
||||
*/
|
||||
export function absoluteHref(href: string): string {
|
||||
return `${basePath()}/${href.startsWith('/') ? href.slice(1) : href}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a link to a page path in the current space.
|
||||
*/
|
||||
export function pageHref(pagePath: string): string {
|
||||
return `${basePath()}/${pagePath.startsWith('/') ? pagePath.slice(1) : pagePath}`;
|
||||
export function pageHref(
|
||||
page: RevisionPageDocument,
|
||||
context: PageHrefContext = {},
|
||||
/** Anchor to link to in the page. */
|
||||
anchor?: string,
|
||||
): string {
|
||||
const { pdf } = context;
|
||||
|
||||
if (pdf) {
|
||||
if (pdf.includes(page.id)) {
|
||||
return '#' + pagePDFContainerId(page, anchor);
|
||||
} else {
|
||||
// Use an absolute URL to the page
|
||||
// TODO: we need to extend RevisionPageDocument with "urls"
|
||||
return page.urls?.published || '/todo';
|
||||
}
|
||||
}
|
||||
|
||||
return absoluteHref(page.path) + (anchor ? '#' + anchor : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the HTML ID for the container of a page during a PDF rendering.
|
||||
*/
|
||||
export function pagePDFContainerId(page: RevisionPageDocument, anchor?: string): string {
|
||||
return `pdf-page-${page.id}` + (anchor ? `-${anchor}` : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTML ID for a block in a page.
|
||||
* It ensures the ID is unique in the entire HTML page (in case we are generating a PDF with multiple pages).
|
||||
*/
|
||||
export function pageLocalId(
|
||||
page: RevisionPageDocument,
|
||||
localId: string,
|
||||
context: PageHrefContext,
|
||||
): string {
|
||||
if (!context.pdf?.length) {
|
||||
return localId;
|
||||
}
|
||||
|
||||
return pagePDFContainerId(page, localId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ContentRef, Revision, RevisionPageDocument, Space } from '@gitbook/api';
|
||||
import { resolvePageId } from './pages';
|
||||
import { pageHref } from './links';
|
||||
import { pageHref, PageHrefContext } from './links';
|
||||
|
||||
export interface ResolvedContentRef {
|
||||
/** Text to render in the content ref */
|
||||
@@ -9,7 +9,7 @@ export interface ResolvedContentRef {
|
||||
href: string;
|
||||
}
|
||||
|
||||
export interface ContentRefContext {
|
||||
export interface ContentRefContext extends PageHrefContext {
|
||||
space: Space;
|
||||
revision: Revision;
|
||||
page: RevisionPageDocument;
|
||||
@@ -20,7 +20,7 @@ export interface ContentRefContext {
|
||||
*/
|
||||
export async function resolveContentRef(
|
||||
contentRef: ContentRef,
|
||||
{ space, revision, page: activePage }: ContentRefContext,
|
||||
{ space, revision, page: activePage, ...linksContext }: ContentRefContext,
|
||||
): Promise<ResolvedContentRef | null> {
|
||||
// Try to resolve a local ref in the current space
|
||||
if (contentRef.kind === 'url') {
|
||||
@@ -49,13 +49,13 @@ export async function resolveContentRef(
|
||||
|
||||
if (contentRef.kind === 'page') {
|
||||
return {
|
||||
href: pageHref(page.path),
|
||||
href: pageHref(page, linksContext),
|
||||
text: page.title,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
href: pageHref(page.path) + '#' + contentRef.anchor,
|
||||
href: pageHref(page, linksContext, contentRef.anchor),
|
||||
text: page.title + '#' + contentRef.anchor,
|
||||
};
|
||||
} else if (contentRef.kind === 'space' && contentRef.space === space.id) {
|
||||
|
||||
Reference in New Issue
Block a user