Home / Function/ yaml2ts() — astro Function Reference

yaml2ts() — astro Function Reference

Architecture documentation for the yaml2ts() function in yaml2ts.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d1b5034c_ca40_a568_a4b7_7c68e3a1d887["yaml2ts()"]
  049255e0_f9ac_b050_4b5b_56c85d17f188["yaml2ts.ts"]
  d1b5034c_ca40_a568_a4b7_7c68e3a1d887 -->|defined in| 049255e0_f9ac_b050_4b5b_56c85d17f188
  style d1b5034c_ca40_a568_a4b7_7c68e3a1d887 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/language-tools/yaml2ts/src/yaml2ts.ts lines 21–154

export function yaml2ts(frontmatter: string, collection: string): YAML2TSResult {
	const frontmatterMappings: CodeMapping[] = [];
	const frontmatterContent = parseDocument(frontmatter, {
		keepSourceTokens: true,
		customTags: ['timestamp'], // Handle YAML timestamps
		// Those two options prevent `yaml` from throwing errors when it encounters parsing errors, which is useful for handling incomplete content
		strict: false,
		logLevel: 'silent',
	});

	let fullResult = 'import type { InferEntrySchema } from "astro:content";\n\n(\n';
	let objectContent = frontmatter.trim().length > 0 ? '' : '{}'; // If there's no content, provide an empty object so that there's no syntax error

	YAML.visit(frontmatterContent, {
		Value(key, value) {
			if (isCollection(value)) {
				if (isMap(value)) {
					mapMap(value, key);
				}

				if (isSeq(value)) {
					mapSeq(value);
				}
			}

			// If we didn't hit any of the above, we have a scalar value which in almost all cases is a Pair that's just not fully written yet
			if (isScalar(value)) {
				const itemKey = mapScalarKey(value);
				// We don't care about values, just keys, since we're only interested in the structure
				objectContent += `${itemKey}: null\n`;
			}

			return YAML.visit.REMOVE;
		},
	});

	function mapMap(map: YAMLMap, key?: string | number | null) {
		objectContent += '{\n';

		// Go through all the items in the map
		map.items.forEach((item) => {
			// Pairs keys are not always scalars (they can even be totally arbitrary nodes), but in practice, it's really rare for them to be anything other than scalars
			// Anyway, Zod does not support non-scalar keys, so it's fine to just not handle anything other than scalars
			if (isScalar(item.key)) {
				const itemKey = mapScalarKey(item.key);

				if (isScalar(item.value) || item.value === null) {
					objectContent += `${itemKey}: null,\n`; // Don't care about value, just key
				}

				if (isMap(item.value)) {
					objectContent += `${itemKey}: `;
					mapMap(item.value);
				}

				if (isSeq(item.value)) {
					objectContent += `${itemKey}: `;
					mapSeq(item.value);
				}
			}

			return YAML.visit.REMOVE;
		});

		objectContent += '}';

		if (key !== null) {
			objectContent += ',';
		}

		objectContent += '\n';

		return YAML.visit.REMOVE;
	}

	function mapSeq(seq: YAMLSeq) {
		objectContent += '[';

		seq.items.forEach((item) => {
			if (isScalar(item)) {
				objectContent += `null,`;

Domain

Subdomains

Frequently Asked Questions

What does yaml2ts() do?
yaml2ts() is a function in the astro codebase, defined in packages/language-tools/yaml2ts/src/yaml2ts.ts.
Where is yaml2ts() defined?
yaml2ts() is defined in packages/language-tools/yaml2ts/src/yaml2ts.ts at line 21.

Analyze Your Own Codebase

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

Try Supermodel Free