Home / Function/ preprocessHTML() — astro Function Reference

preprocessHTML() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  f08bfd92_3339_d425_0b24_1f4319df6ddf["preprocessHTML()"]
  b9e6df3b_0db3_5da0_196d_ad954be5a3bd["parseHTML.ts"]
  f08bfd92_3339_d425_0b24_1f4319df6ddf -->|defined in| b9e6df3b_0db3_5da0_196d_ad954be5a3bd
  bebadfbe_8a04_994d_e104_1094a6547f4d["parseHTML()"]
  bebadfbe_8a04_994d_e104_1094a6547f4d -->|calls| f08bfd92_3339_d425_0b24_1f4319df6ddf
  5ecba48f_d9f9_6a3a_2e04_bda4d9c652a4["isInsideExpression()"]
  f08bfd92_3339_d425_0b24_1f4319df6ddf -->|calls| 5ecba48f_d9f9_6a3a_2e04_bda4d9c652a4
  style f08bfd92_3339_d425_0b24_1f4319df6ddf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/language-tools/language-server/src/core/parseHTML.ts lines 29–84

export function preprocessHTML(text: string, frontmatterEnd?: number) {
	let content = text.split('').fill(' ', 0, frontmatterEnd).join('');

	let scanner = createScanner(content);
	let token = scanner.scan();
	let currentStartTagStart: number | null = null;

	while (token !== html.TokenType.EOS) {
		const offset = scanner.getTokenOffset();

		if (token === html.TokenType.StartTagOpen) {
			currentStartTagStart = offset;
		}

		if (token === html.TokenType.StartTagClose) {
			if (shouldBlankStartOrEndTagLike(offset)) {
				blankStartOrEndTagLike(offset);
			} else {
				currentStartTagStart = null;
			}
		}

		if (token === html.TokenType.StartTagSelfClose) {
			currentStartTagStart = null;
		}

		// <Foo checked={a < 1}>
		// https://github.com/microsoft/vscode-html-languageservice/blob/71806ef57be07e1068ee40900ef8b0899c80e68a/src/parser/htmlScanner.ts#L327
		if (
			token === html.TokenType.Unknown &&
			scanner.getScannerState() === html.ScannerState.WithinTag &&
			scanner.getTokenText() === '<' &&
			shouldBlankStartOrEndTagLike(offset)
		) {
			blankStartOrEndTagLike(offset);
		}

		// TODO: Handle TypeScript generics inside expressions / Use the compiler to parse HTML instead?

		token = scanner.scan();
	}

	return content;

	function shouldBlankStartOrEndTagLike(offset: number) {
		// not null rather than falsy, otherwise it won't work on first tag(0)
		return (
			currentStartTagStart !== null && isInsideExpression(content, currentStartTagStart, offset)
		);
	}

	function blankStartOrEndTagLike(offset: number, state?: html.ScannerState) {
		content = content.substring(0, offset) + ' ' + content.substring(offset + 1);
		scanner = createScanner(content, offset, state ?? html.ScannerState.WithinTag);
	}
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does preprocessHTML() do?
preprocessHTML() is a function in the astro codebase, defined in packages/language-tools/language-server/src/core/parseHTML.ts.
Where is preprocessHTML() defined?
preprocessHTML() is defined in packages/language-tools/language-server/src/core/parseHTML.ts at line 29.
What does preprocessHTML() call?
preprocessHTML() calls 1 function(s): isInsideExpression.
What calls preprocessHTML()?
preprocessHTML() is called by 1 function(s): parseHTML.

Analyze Your Own Codebase

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

Try Supermodel Free