Skip to content

Commit

Permalink
fix: fixes an issue preventing to use Expressive Code attributes in M…
Browse files Browse the repository at this point in the history
…arkdown files
  • Loading branch information
HiDeoo committed May 19, 2024
1 parent 8aace7a commit ae1a490
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/src/content/docs/tests/specific-syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Specific syntax Markdown
slug: tests/specific-syntax-md
pagefind: false
---

A code block with no frame:

```js frame="none"
// src/example.js
console.log('Hello, world!')
```

A remote image using Markdown syntax:

![Astro logo](https://astro.build/assets/press/astro-logo-light-gradient.png)

A remote image using HTML syntax with the `img` tag:

<img src="https://astro.build/assets/press/astro-logo-light-gradient.png" alt="Astro logo" />
4 changes: 2 additions & 2 deletions packages/starlight-image-zoom/libs/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import rehypeRaw from 'rehype-raw'

import type { StarlightImageZoomConfig } from '..'

import { rehypeStarlightImageZoom } from './rehype'
import { rehypeMetaString, rehypeStarlightImageZoom } from './rehype'
import { vitePluginStarlightImageZoomConfig } from './vite'

export function starlightImageZoomIntegration(config: StarlightImageZoomConfig): AstroIntegration {
Expand All @@ -13,7 +13,7 @@ export function starlightImageZoomIntegration(config: StarlightImageZoomConfig):
'astro:config:setup': ({ updateConfig }) => {
updateConfig({
markdown: {
rehypePlugins: [rehypeRaw, rehypeStarlightImageZoom],
rehypePlugins: [rehypeMetaString, rehypeRaw, rehypeStarlightImageZoom],
},
vite: {
plugins: [vitePluginStarlightImageZoomConfig(config)],
Expand Down
23 changes: 23 additions & 0 deletions packages/starlight-image-zoom/libs/rehype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,26 @@ export function rehypeStarlightImageZoom() {
})
}
}

/**
* rehype-raw strips the `meta` property from code blocks so we manually moved it to a `metastring` property which is
* supported by expressive-code.
*
* @see https://github.com/syntax-tree/hast-util-raw/issues/13
* @see https://github.com/expressive-code/expressive-code/blob/21fdaa441c89d6a6ac38f5d522b6b60741df2f5d/packages/rehype-expressive-code/src/utils.ts#L15
*/
export function rehypeMetaString() {
return function (tree: Root) {
visit(tree, ['element'], (node) => {
if (node.type === 'element' && node.tagName === 'code' && node.data?.meta) {
node.properties['metastring'] = node.data.meta
}
})
}
}

declare module 'hast' {
interface Data {
meta?: string
}
}
6 changes: 6 additions & 0 deletions packages/starlight-image-zoom/tests/specific-syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ test('mdx: zooms an image using the `<Picture>` component', async ({ testPage })

await expect(testPage.getNthImage(1)).toBeZoomedAfterClick()
})

test('md: preserves code block metadata', async ({ testPage }) => {
await testPage.goto('specific-syntax-md')

await expect(testPage.page.getByText('// src/example.js')).toBeVisible()
})

0 comments on commit ae1a490

Please sign in to comment.