Home / File/ parseCSS.ts — astro Source File

parseCSS.ts — astro Source File

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

File typescript CoreAstro CoreMiddleware 4 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  f6152f93_2879_07e4_0f31_4adb7af86607["parseCSS.ts"]
  40ca87c8_5a7b_e314_333e_57fb72b09780["../buildMappings.js"]
  f6152f93_2879_07e4_0f31_4adb7af86607 --> 40ca87c8_5a7b_e314_333e_57fb72b09780
  81a20ac1_6143_16be_33ec_872bb8d3a54b["types"]
  f6152f93_2879_07e4_0f31_4adb7af86607 --> 81a20ac1_6143_16be_33ec_872bb8d3a54b
  040ca79b_dadf_4383_efd2_c0b13744e9f1["language-core"]
  f6152f93_2879_07e4_0f31_4adb7af86607 --> 040ca79b_dadf_4383_efd2_c0b13744e9f1
  240c179d_8195_bda1_51ea_09ccf72ff044["muggle-string"]
  f6152f93_2879_07e4_0f31_4adb7af86607 --> 240c179d_8195_bda1_51ea_09ccf72ff044
  style f6152f93_2879_07e4_0f31_4adb7af86607 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { TSXExtractedStyle } from '@astrojs/compiler/types';
import type { CodeInformation, VirtualCode } from '@volar/language-core';
import type { Segment } from 'muggle-string';
import { toString } from 'muggle-string';
import { buildMappings } from '../buildMappings.js';

const SUPPORTED_LANGUAGES = ['css', 'scss', 'less'] as const;
type SupportedLanguages = (typeof SUPPORTED_LANGUAGES)[number];

function isSupportedLanguage(lang: string): lang is SupportedLanguages {
	return SUPPORTED_LANGUAGES.includes(lang as SupportedLanguages);
}

export function extractStylesheets(styles: TSXExtractedStyle[]): VirtualCode[] {
	return mergeCSSContextsByLanguage(styles);
}

function mergeCSSContextsByLanguage(inlineStyles: TSXExtractedStyle[]): VirtualCode[] {
	const codes: Record<SupportedLanguages, Segment<CodeInformation>[]> = {
		css: [],
		scss: [],
		less: [],
	};

	for (const cssContext of inlineStyles) {
		const currentCode = isSupportedLanguage(cssContext.lang) ? codes[cssContext.lang] : codes.css;

		const isStyleAttribute = cssContext.type === 'style-attribute';
		if (isStyleAttribute) currentCode.push('__ { ');
		currentCode.push([
			cssContext.content,
			undefined,
			cssContext.position.start,
			{
				verification: false,
				completion: true,
				semantic: true,
				navigation: true,
				structure: true,
				format: false,
			},
		]);
		if (isStyleAttribute) currentCode.push(' }\n');
	}

	let virtualCodes: VirtualCode[] = [];
	for (const lang of SUPPORTED_LANGUAGES) {
		if (codes[lang].length) {
			virtualCodes.push(createVirtualCodeForLanguage(codes[lang], lang));
		}
	}

	return virtualCodes;
}

function createVirtualCodeForLanguage(code: Segment<CodeInformation>[], lang: string): VirtualCode {
	const mappings = buildMappings(code);
	const text = toString(code);

	return {
		id: `style.${lang}`,
		languageId: lang,
		snapshot: {
			getText: (start, end) => text.substring(start, end),
			getLength: () => text.length,
			getChangeRange: () => undefined,
		},
		embeddedCodes: [],
		mappings,
	};
}

Domain

Subdomains

Dependencies

  • ../buildMappings.js
  • language-core
  • muggle-string
  • types

Frequently Asked Questions

What does parseCSS.ts do?
parseCSS.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in parseCSS.ts?
parseCSS.ts defines 4 function(s): createVirtualCodeForLanguage, extractStylesheets, isSupportedLanguage, mergeCSSContextsByLanguage.
What does parseCSS.ts depend on?
parseCSS.ts imports 4 module(s): ../buildMappings.js, language-core, muggle-string, types.
Where is parseCSS.ts in the architecture?
parseCSS.ts is located at packages/language-tools/language-server/src/core/parseCSS.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/language-tools/language-server/src/core).

Analyze Your Own Codebase

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

Try Supermodel Free