Home / File/ utils.ts — astro Source File

utils.ts — astro Source File

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

File typescript CoreAstro RoutingSystem 6 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  579be117_6aa8_f4e4_0e48_c4a41ab4204a["utils.ts"]
  82f345a2_2234_43f1_c3c4_eea191acdca8["markdown-remark"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> 82f345a2_2234_43f1_c3c4_eea191acdca8
  f3a5dffb_9909_fe63_a648_5703ef367803["acorn"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> f3a5dffb_9909_fe63_a648_5703ef367803
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> f16d8c76_2866_6150_bd14_0347b59abfe9
  350bf1c9_161e_6d98_d477_5ac7e492812d["mdast-util-mdx"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> 350bf1c9_161e_6d98_d477_5ac7e492812d
  10250468_0e83_bd69_43e9_3bcef2294a91["piccolore"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> 10250468_0e83_bd69_43e9_3bcef2294a91
  54ce55cc_9dc1_4ebd_28dd_358c1e22d4e7["unified"]
  579be117_6aa8_f4e4_0e48_c4a41ab4204a --> 54ce55cc_9dc1_4ebd_28dd_358c1e22d4e7
  style 579be117_6aa8_f4e4_0e48_c4a41ab4204a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { parseFrontmatter } from '@astrojs/markdown-remark';
import type { Options as AcornOpts } from 'acorn';
import { parse } from 'acorn';
import type { AstroConfig, AstroIntegrationLogger, SSRError } from 'astro';
import type { MdxjsEsm } from 'mdast-util-mdx';
import colors from 'piccolore';
import type { PluggableList } from 'unified';

function appendForwardSlash(path: string) {
	return path.endsWith('/') ? path : path + '/';
}

export interface FileInfo {
	fileId: string;
	fileUrl: string;
}

/** @see 'vite-plugin-utils' for source */
export function getFileInfo(id: string, config: AstroConfig): FileInfo {
	const sitePathname = appendForwardSlash(
		config.site ? new URL(config.base, config.site).pathname : config.base,
	);

	// Try to grab the file's actual URL
	let url: URL | undefined = undefined;
	try {
		url = new URL(`file://${id}`);
	} catch {}

	const fileId = id.split('?')[0];
	let fileUrl: string;
	const isPage = fileId.includes('/pages/');
	if (isPage) {
		fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(?:\/index)?\.mdx$/, '');
	} else if (url?.pathname.startsWith(config.root.pathname)) {
		fileUrl = url.pathname.slice(config.root.pathname.length);
	} else {
		fileUrl = fileId;
	}

	if (fileUrl && config.trailingSlash === 'always') {
		fileUrl = appendForwardSlash(fileUrl);
	}
	return { fileId, fileUrl };
}

/**
 * Match YAML exception handling from Astro core errors
 * @see 'astro/src/core/errors.ts'
 */
export function safeParseFrontmatter(code: string, id: string) {
	try {
		return parseFrontmatter(code, { frontmatter: 'empty-with-spaces' });
	} catch (e: any) {
		if (e.name === 'YAMLException') {
			const err: SSRError = e;
			err.id = id;
			err.loc = { file: e.id, line: e.mark.line + 1, column: e.mark.column };
			err.message = e.reason;
			throw err;
		} else {
			throw e;
		}
	}
}

export function jsToTreeNode(
	jsString: string,
	acornOpts: AcornOpts = {
		ecmaVersion: 'latest',
		sourceType: 'module',
	},
): MdxjsEsm {
	return {
		type: 'mdxjsEsm',
		value: '',
		data: {
			// @ts-expect-error `parse` return types is incompatible but it should work in runtime
			estree: {
				...parse(jsString, acornOpts),
				type: 'Program',
				sourceType: 'module',
			},
		},
	};
}

export function ignoreStringPlugins(plugins: any[], logger: AstroIntegrationLogger): PluggableList {
	let validPlugins: PluggableList = [];
	let hasInvalidPlugin = false;
	for (const plugin of plugins) {
		if (typeof plugin === 'string') {
			logger.warn(`${colors.bold(plugin)} not applied.`);
			hasInvalidPlugin = true;
		} else if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
			logger.warn(`${colors.bold(plugin[0])} not applied.`);
			hasInvalidPlugin = true;
		} else {
			validPlugins.push(plugin);
		}
	}
	if (hasInvalidPlugin) {
		logger.warn(
			`To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins`,
		);
	}
	return validPlugins;
}

Domain

Subdomains

Types

Dependencies

  • acorn
  • astro
  • markdown-remark
  • mdast-util-mdx
  • piccolore
  • unified

Frequently Asked Questions

What does utils.ts do?
utils.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in utils.ts?
utils.ts defines 5 function(s): appendForwardSlash, getFileInfo, ignoreStringPlugins, jsToTreeNode, safeParseFrontmatter.
What does utils.ts depend on?
utils.ts imports 6 module(s): acorn, astro, markdown-remark, mdast-util-mdx, piccolore, unified.
Where is utils.ts in the architecture?
utils.ts is located at packages/integrations/mdx/src/utils.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/mdx/src).

Analyze Your Own Codebase

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

Try Supermodel Free