Skip to content

Conversation

@Albez0-An7h
Copy link

Description

This PR fixes an issue where code blocks inside markdown aren't highlighted when using fine-grained bundles.

What's happening:
When you use createHighlighterCore with just markdown, embedded languages (JavaScript, Python, HTML, etc. in code blocks) don't get loaded automatically. This is intentional to keep bundles small, but it's confusing and leads to unhighlighted code blocks.

Users have been working around this by manually overriding the markdown grammar's embeddedLangs property, which is pretty fragile and verbose.

What this PR does:
Adds clear documentation and examples showing three ways to handle this properly:

  1. Manual Loading - Load all the languages you need upfront
  2. Dynamic Detection - Use guessEmbeddedLanguages to auto-detect what's needed
  3. Custom Shorthands (recommended) - Set it up once, then it just works automatically

The best part? The guessEmbeddedLanguages utility was already there - we just needed to document how to use it!

What changed:

  • Added "Handling Embedded Languages" guide in docs/guide/bundles.md
  • Added a helpful tip in docs/guide/best-performance.md
  • Created tests showing all three approaches working
  • Added a practical example file people can copy from

Linked Issues

Fixes #1039

Additional context

The recommended approach (custom shorthands) gives you the same smooth experience as the full bundle presets, but with complete control over what gets bundled.

Everything is backward compatible - this is just documentation to help people avoid common pitfalls.

…ndles

Fixes shikijs#1039

When using fine-grained bundles with createHighlighterCore, markdown's embedded
languages (like JavaScript, Python, HTML in code blocks) are not loaded automatically.
This is by design to keep bundles minimal, but it can be confusing for users.

This PR provides comprehensive documentation and examples showing three approaches:

1. **Manual Loading**: Explicitly load all expected embedded languages upfront
2. **Dynamic Detection**: Use guessEmbeddedLanguages to detect and load on-demand
3. **Custom Shorthands (Recommended)**: Create custom shorthands with auto-loading

The guessEmbeddedLanguages utility was already exported from @shikijs/core, so no
code changes were needed to the library itself.

Changes:
- Added "Handling Embedded Languages" section to bundles.md with detailed examples
- Added tip in best-performance.md about embedded languages
- Created comprehensive test suite with all three solution approaches
- Added practical example file demonstrating each solution

This helps users avoid the current workaround of manually overriding embeddedLangs
on the markdown grammar object, which is verbose and fragile.
@netlify
Copy link

netlify bot commented Nov 7, 2025

Deploy Preview for shiki-matsu ready!

Name Link
🔨 Latest commit 4a34dc7
🔍 Latest deploy log https://app.netlify.com/projects/shiki-matsu/deploys/690dd43bcbe99f0008927051
😎 Deploy Preview https://deploy-preview-1105--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.

@netlify
Copy link

netlify bot commented Nov 7, 2025

Deploy Preview for shiki-next ready!

Name Link
🔨 Latest commit 4a34dc7
🔍 Latest deploy log https://app.netlify.com/projects/shiki-next/deploys/690dd43bcbe99f0008927055
😎 Deploy Preview https://deploy-preview-1105--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.

- Fix import sorting in test file
- Remove trailing whitespace
- Fix quote style for object properties
- Remove example file with excessive console.log usage
- Format code blocks in documentation properly
- Fix import ordering (move vitest import to end)
- Fix guessEmbeddedLanguages test expectations:
  - Function returns language aliases as found in code (e.g., 'js', 'ts')
  - When no highlighter provided, returns all detected languages
  - When highlighter provided, filters to only bundled languages
- Fix test for dynamically loading embedded languages:
  - Don't pass highlighter to guessEmbeddedLanguages when using createHighlighterCore
  - createHighlighterCore has no bundle, so filtering would return empty array
- Fix HTML entity test (HTML tags are escaped in output)
- Fix markdown code block formatting in docs
- Use createdBundledHighlighter in bundle filter test for correct behavior
@codecov
Copy link

codecov bot commented Nov 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.38%. Comparing base (5068b26) to head (4a34dc7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1105      +/-   ##
==========================================
+ Coverage   88.35%   88.38%   +0.03%     
==========================================
  Files          74       74              
  Lines        3322     3322              
  Branches      956      954       -2     
==========================================
+ Hits         2935     2936       +1     
+ Misses        344      343       -1     
  Partials       43       43              

☔ 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.

- Sort named imports alphabetically (createdBundledHighlighter before createSingletonShorthands)
- Add blank lines in tip block for proper markdown formatting
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.

Sub-grammars for markdown are not loaded/activated in fine-grained bundled

1 participant