MCPcopy Index your code
hub / github.com/GitbookIO/gitbook / encodePathInSiteContent

Function encodePathInSiteContent

packages/gitbook/src/middleware.ts:548–601  ·  view source on GitHub ↗

* Encode path in a site content. * Special paths are not encoded and passed to be handled by the route handlers.

(rawPathname: string)

Source from the content-addressed store, hash-verified

546 * Special paths are not encoded and passed to be handled by the route handlers.
547 */
548function encodePathInSiteContent(rawPathname: string): {
549 pathname: string;
550 routeType?: 'static' | 'dynamic';
551} {
552 const pathname = removeLeadingSlash(removeTrailingSlash(rawPathname));
553
554 if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) {
555 return { pathname };
556 }
557
558 // If the pathname is a markdown file, we rewrite it to ~gitbook/markdown/:pathname
559 if (pathname.match(/\.md$/)) {
560 const pagePathWithoutMD = pathname.slice(0, -3);
561 return {
562 pathname: `~gitbook/markdown/${encodeURIComponent(pagePathWithoutMD)}`,
563 // The markdown content is always static and doesn't depend on the dynamic parameter (customization, theme, etc)
564 routeType: 'static',
565 };
566 }
567
568 // We skip encoding for paginated llms-full.txt pages (i.e. llms-full.txt/100)
569 if (pathname.match(/^llms-full\.txt\/\d+$/)) {
570 return { pathname, routeType: 'static' };
571 }
572
573 // If the pathname is an embedded page
574 const embedPage = pathname.match(/^~gitbook\/embed\/page\/(\S+)$/);
575 if (embedPage) {
576 return {
577 pathname: `~gitbook/embed/page/${encodeURIComponent(embedPage[1]!)}`,
578 };
579 }
580
581 switch (pathname) {
582 case '~gitbook/embed/assistant':
583 case '~gitbook/icon':
584 return { pathname };
585 case '~gitbook/mcp':
586 case 'llms.txt':
587 case 'sitemap.xml':
588 case 'sitemap-pages.xml':
589 case 'robots.txt':
590 case '~gitbook/embed/script.js':
591 case '~gitbook/embed/demo':
592 // LLMs.txt, sitemap, sitemap-pages and robots.txt are always static
593 // as they only depend on the site structure / pages.
594 return { pathname, routeType: 'static' };
595 case '~gitbook/pdf':
596 // PDF routes are always dynamic as they depend on the search params.
597 return { pathname, routeType: 'dynamic' };
598 default:
599 return { pathname: encodeURIComponent(pathname || '/') };
600 }
601}
602
603/**
604 * Append all the query params from a URL to another URL.

Callers 1

withAPITokenFunction · 0.85

Calls 2

removeLeadingSlashFunction · 0.90
removeTrailingSlashFunction · 0.90

Tested by

no test coverage detected