Home / Function/ shouldAddCharset() — astro Function Reference

shouldAddCharset() — astro Function Reference

Architecture documentation for the shouldAddCharset() function in rehype-apply-frontmatter-export.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e5738373_9d28_407c_3733_9fa1b3abbc0b["shouldAddCharset()"]
  50f0dfd4_d9b5_8cd5_80a6_3519a94ab6d7["rehype-apply-frontmatter-export.ts"]
  e5738373_9d28_407c_3733_9fa1b3abbc0b -->|defined in| 50f0dfd4_d9b5_8cd5_80a6_3519a94ab6d7
  3cb2c0d6_fe6a_7406_95ac_85f24f55edc1["rehypeApplyFrontmatterExport()"]
  3cb2c0d6_fe6a_7406_95ac_85f24f55edc1 -->|calls| e5738373_9d28_407c_3733_9fa1b3abbc0b
  style e5738373_9d28_407c_3733_9fa1b3abbc0b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/mdx/src/rehype-apply-frontmatter-export.ts lines 79–113

function shouldAddCharset(tree: Root, vfile: VFile) {
	const srcDirUrl = vfile.data.applyFrontmatterExport?.srcDir;
	if (!srcDirUrl) return false;

	const hasConstPartialTrue = tree.children.some(
		(node) => node.type === 'mdxjsEsm' && exportConstPartialTrueRe.test(node.value),
	);
	if (hasConstPartialTrue) return false;

	// NOTE: the pages directory is a non-configurable Astro behaviour
	const pagesDir = path.join(fileURLToPath(srcDirUrl), 'pages').replace(/\\/g, '/');
	// `vfile.path` comes from Vite, which is a normalized path (no backslashes)
	const filePath = vfile.path;
	if (!filePath.startsWith(pagesDir)) return false;

	const hasLeadingUnderscoreInPath = filePath
		.slice(pagesDir.length)
		.replace(/\\/g, '/')
		.split('/')
		.some((part) => part.startsWith('_'));
	if (hasLeadingUnderscoreInPath) return false;

	// Bail if the first content found is a wrapping layout component
	for (const child of tree.children) {
		if (child.type === 'element') break;
		if (child.type === 'mdxJsxFlowElement') {
			// If is fragment or lowercase tag name (html tags), skip and assume there's no layout
			if (child.name == null) break;
			if (child.name[0] === child.name[0].toLowerCase()) break;
			return false;
		}
	}

	return true;
}

Domain

Subdomains

Frequently Asked Questions

What does shouldAddCharset() do?
shouldAddCharset() is a function in the astro codebase, defined in packages/integrations/mdx/src/rehype-apply-frontmatter-export.ts.
Where is shouldAddCharset() defined?
shouldAddCharset() is defined in packages/integrations/mdx/src/rehype-apply-frontmatter-export.ts at line 79.
What calls shouldAddCharset()?
shouldAddCharset() is called by 1 function(s): rehypeApplyFrontmatterExport.

Analyze Your Own Codebase

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

Try Supermodel Free