Skip to content

Conversation

@AmanCrafts
Copy link

PR Description

Title

feat(core): preserve HAST data and properties in codeToHast

Description

This PR adds support for preserving HAST data and properties when using codeToHast.

Currently, when codeToHast generates the HAST tree, it creates a new pre element and does not allow passing custom data or properties to it (other than meta which is handled specifically). This change adds a data property to CodeToHastOptions which is then assigned to the data property of the root pre element in the generated HAST.

This is useful for integrations (like rehype-shiki or remark-rehype) that want to preserve metadata from the original code block (e.g. fileName, positional info) in the generated HAST, allowing other plugins to consume it.

Changes

  • packages/types/src/options.ts: Added data property to CodeToHastOptionsCommon.
  • packages/core/src/highlight/code-to-hast.ts: Updated tokensToHast to assign options.data to preNode.data.

Related Issue

Fixes #629 (Preserve HAST element data and properties)

Checklist

  • Tests added/updated
  • Documentation updated (N/A - internal API change, but self-documenting in types)
  • Linting passed

@netlify
Copy link

netlify bot commented Dec 3, 2025

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit 698b134
🔍 Latest deploy log https://app.netlify.com/projects/shiki-next/deploys/69305a28f1460c00083be21d
😎 Deploy Preview https://deploy-preview-1204--shiki-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Dec 3, 2025

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit 698b134
🔍 Latest deploy log https://app.netlify.com/projects/shiki-matsu/deploys/69305a2870c37b0008a138d8
😎 Deploy Preview https://deploy-preview-1204--shiki-matsu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov
Copy link

codecov bot commented Dec 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.20%. Comparing base (61c87e8) to head (698b134).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1204   +/-   ##
=======================================
  Coverage   95.20%   95.20%           
=======================================
  Files          92       92           
  Lines        7922     7923    +1     
  Branches     1689     1689           
=======================================
+ Hits         7542     7543    +1     
  Misses        374      374           
  Partials        6        6           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

@shikijs/rehype: keep hast element data and properties

1 participant