Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support adding documentRoot vfile context to MDC parser #275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JessicaSachs
Copy link

πŸ”— Linked issue

#274

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Problem

Some remark plugins rely on the standardized vfile context being attached to the file object. Currently, the MDC parser doesn't support passing this context through, which limits compatibility with these plugins. See #274

Solution

  • Added rootDocument option to inlineOptions in the markdown parser creation
  • This allows passing through vfile context without breaking existing functionality
  • Chose to add at the inlineOptions level rather than within parseMarkdown to minimize changes to function signatures

Implementation Details

  • Extended inlineOptions interface to include rootDocument option
  • Modified the processing stream to merge rootDocument with the existing context:
    const mdcWithContext = { ...(inlineOptions.rootDocument || {}), value: content, data: frontmatter }
  • Companion change needed in @nuxt/content to resolve and pass documentRoot (already working locally)

Impact

  • Non-breaking change
  • Improves compatibility with remark plugins that depend on vfile context
  • Maintains existing parser behavior while adding new capability

Related Changes

  • A follow-up PR will be opened in @nuxt/content to complete the implementation

@JessicaSachs
Copy link
Author

The VFile API is comfortingly universal and browser-safe, so I'm not nervous about using it.

@JessicaSachs
Copy link
Author

No idea why typecheck is failing and I can't get the @nuxtjs/mdc types to resolve locally when trying to run pnpm typecheck on my computer. Plz halp.

@@ -97,7 +97,8 @@ export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {})
const { content, data: frontmatter } = await parseFrontMatter(md)

// Start processing stream
const processedFile = await processor.process({ value: content, data: frontmatter })
const mdcWithContext = { ...(inlineOptions.rootDocument || {}), value: content, data: frontmatter }
const processedFile = await processor.process(mdcWithContext)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VFile is directly related to the current processing task. Passing a predefined rootDocument for all processing tasks may cause invalid results. Especially with Nuxt Content's multi-source feature.

Instead, we should define a second argument for return async (md: string): Promise<MDCParserResult> function to receive VFile data.

  return async (md: string, vFileData: VFile = {}): Promise<MDCParserResult> => {
    // Extract front matter data
    const { content, data: frontmatter } = await parseFrontMatter(md)

    // Start processing stream
    const processedFile = await processor.process({ ...vFileData, value: content, data: frontmatter })
    ...
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants