Home / File/ generator.ts — astro Source File

generator.ts — astro Source File

Architecture documentation for generator.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 2 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  f66463aa_0743_20f2_0ef3_7ae29013020e["generator.ts"]
  c32d12e2_d85e_28c0_eea7_9b29629857e0["../types/public/config.js"]
  f66463aa_0743_20f2_0ef3_7ae29013020e --> c32d12e2_d85e_28c0_eea7_9b29629857e0
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  f66463aa_0743_20f2_0ef3_7ae29013020e --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  style f66463aa_0743_20f2_0ef3_7ae29013020e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { AstroConfig } from '../../../types/public/config.js';
import type { RoutePart } from '../../../types/public/internal.js';

/**
 * Sanitizes the parameters object by normalizing string values and replacing certain characters with their URL-encoded equivalents.
 * @param {Record<string, string | number>} params - The parameters object to be sanitized.
 * @returns {Record<string, string | number>} The sanitized parameters object.
 */
function sanitizeParams(params: Record<string, string | number>): Record<string, string | number> {
	return Object.fromEntries(
		Object.entries(params).map(([key, value]) => {
			if (typeof value === 'string') {
				return [key, value.normalize().replace(/#/g, '%23').replace(/\?/g, '%3F')];
			}
			return [key, value];
		}),
	);
}

function getParameter(part: RoutePart, params: Record<string, string | number>): string | number {
	if (part.spread) {
		return params[part.content.slice(3)] || '';
	}

	if (part.dynamic) {
		if (!params[part.content]) {
			throw new TypeError(`Missing parameter: ${part.content}`);
		}

		return params[part.content];
	}

	return part.content
		.normalize()
		.replace(/\?/g, '%3F')
		.replace(/#/g, '%23')
		.replace(/%5B/g, '[')
		.replace(/%5D/g, ']');
}

function getSegment(segment: RoutePart[], params: Record<string, string | number>): string {
	const segmentPath = segment.map((part) => getParameter(part, params)).join('');

	return segmentPath ? '/' + segmentPath : '';
}

type RouteGenerator = (data?: any) => string;

export function getRouteGenerator(
	segments: RoutePart[][],
	addTrailingSlash: AstroConfig['trailingSlash'],
): RouteGenerator {
	return (params?: any): string => {
		const sanitizedParams = sanitizeParams(params);

		// Unless trailingSlash config is set to 'always', don't automatically append it.
		let trailing: '/' | '' = '';
		if (addTrailingSlash === 'always' && segments.length) {
			trailing = '/';
		}

		const path =
			segments.map((segment) => getSegment(segment, sanitizedParams)).join('') + trailing;

		return path || '/';
	};
}

Domain

Subdomains

Dependencies

  • ../types/public/config.js
  • ../types/public/internal.js

Frequently Asked Questions

What does generator.ts do?
generator.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in generator.ts?
generator.ts defines 5 function(s): data, getParameter, getRouteGenerator, getSegment, sanitizeParams.
What does generator.ts depend on?
generator.ts imports 2 module(s): ../types/public/config.js, ../types/public/internal.js.
Where is generator.ts in the architecture?
generator.ts is located at packages/astro/src/core/routing/manifest/generator.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/core/routing/manifest).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free