Home / File/ util.ts — astro Source File

util.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 1 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  d9adc373_c064_68f1_79ad_de252369fed2["util.ts"]
  53f1a2ae_11eb_75ec_465a_65ca4d2678e4["./index.js"]
  d9adc373_c064_68f1_79ad_de252369fed2 --> 53f1a2ae_11eb_75ec_465a_65ca4d2678e4
  style d9adc373_c064_68f1_79ad_de252369fed2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { RSSOptions } from './index.js';

/** Normalize URL to its canonical form */
export function createCanonicalURL(
	url: string,
	trailingSlash?: RSSOptions['trailingSlash'],
	base?: string,
): string {
	let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
	if (!getUrlExtension(url)) {
		// add trailing slash if there’s no extension or `trailingSlash` is true
		pathname = pathname.replace(/\/*$/, '/');
	}

	pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)

	const canonicalUrl = new URL(pathname, base).href;
	if (trailingSlash === false) {
		// remove the trailing slash
		return canonicalUrl.replace(/\/*$/, '');
	}
	return canonicalUrl;
}

/** Check if a URL is already valid */
export function isValidURL(url: string): boolean {
	try {
		new URL(url);
		return true;
	} catch {}
	return false;
}

function getUrlExtension(url: string) {
	const lastDot = url.lastIndexOf('.');
	const lastSlash = url.lastIndexOf('/');
	return lastDot > lastSlash ? url.slice(lastDot + 1) : '';
}

Domain

Subdomains

Dependencies

  • ./index.js

Frequently Asked Questions

What does util.ts do?
util.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 util.ts?
util.ts defines 3 function(s): createCanonicalURL, getUrlExtension, isValidURL.
What does util.ts depend on?
util.ts imports 1 module(s): ./index.js.
Where is util.ts in the architecture?
util.ts is located at packages/astro-rss/src/util.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro-rss/src).

Analyze Your Own Codebase

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

Try Supermodel Free