Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 20, 2025

Problem

The extension was throwing runtime errors when frontmatter fields used variable formats (union types), specifically when a field could be either a string or an object. This commonly occurs with Astro's content schema definitions:

// Astro content.config.ts
cover: z.union([
  z.string(),
  z.object({
    src: z.string(),
    title: z.string().optional(),
  }),
])

Both of these frontmatter formats should be valid:

# String format
cover: some-filename.jpg

# Object format  
cover:
  src: some-filename.jpg
  title: a title for the file

However, the extension was calling .startsWith('http') directly on the field value without type checking, causing errors when the value was an object instead of a string.

Solution

1. Main Fix in PagesParser.ts

Added proper type checking before calling string methods on frontmatter field values:

  • ✅ String format: Uses the value directly
  • ✅ Object format with src property: Extracts the src value
  • ✅ Invalid formats: Safely skips processing without errors
  • ✅ Only calls string methods on validated string values

2. Additional Fix in ContentType.ts

Fixed validation logic that was calling .length on potentially non-string/non-array values:

  • ✅ Added type checking before accessing .length property
  • ✅ Updated return type to reflect actual returned data types

Testing

Created comprehensive tests covering all scenarios:

PagesParser scenarios:

  • String format: cover: "image.jpg"
  • Object format: cover: { src: "image.jpg", title: "title" }
  • External URLs in both formats ✓
  • Invalid objects and null values handled safely ✓

ContentType validation scenarios:

  • Proper handling of null, undefined, empty values ✓
  • Safe handling of objects and numbers without crashes ✓

Result

The extension now gracefully handles variable frontmatter formats without throwing errors or showing error popups. Users can confidently use Astro-style union types and other variable schemas in their frontmatter without encountering runtime errors.

Fixes #958.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 20, 2025 12:46
…s for preview image fields

Co-authored-by: estruyf <2900833+estruyf@users.noreply.github.com>
…s safely

Co-authored-by: estruyf <2900833+estruyf@users.noreply.github.com>
Copilot AI changed the title [WIP] Feedback: Variable frontmatter leads to error Jun 20, 2025
Copilot AI requested a review from estruyf June 20, 2025 12:49
davidsneighbour added a commit to davidsneighbour/kollitsch.dev that referenced this pull request Jul 15, 2025
Front Matter breaks when we use a union type on the content configuration.

A fix is in the works, but for now let's just use the substructure only.

Revert when PR/Issue is solved.

see estruyf/vscode-front-matter#958
see estruyf/vscode-front-matter#960
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants