Skip to content

Commit

Permalink
Merge pull request #11 from HiDeoo/hd-0-5-0
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed May 19, 2024
2 parents 61d1363 + ae1a490 commit 586f3b3
Show file tree
Hide file tree
Showing 7 changed files with 942 additions and 790 deletions.
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"lint": "prettier -c --cache . && eslint . --cache --max-warnings=0"
},
"dependencies": {
"@astrojs/starlight": "0.21.1",
"astro": "4.5.1",
"@astrojs/starlight": "0.23.1",
"astro": "4.8.6",
"sharp": "0.33.2",
"starlight-image-zoom": "workspace:*",
"starlight-package-managers": "0.4.0"
Expand Down
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: 3 additions & 3 deletions packages/starlight-image-zoom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"unist-util-visit-parents": "6.0.1"
},
"devDependencies": {
"@astrojs/starlight": "0.21.1",
"@astrojs/starlight": "0.23.1",
"@playwright/test": "1.42.1",
"@types/hast": "3.0.4",
"astro": "4.5.1",
"astro": "4.8.6",
"mdast-util-mdx-jsx": "3.1.2"
},
"peerDependencies": {
"@astrojs/starlight": ">=0.20.0"
"@astrojs/starlight": ">=0.22.0"
},
"engines": {
"node": ">=18"
Expand Down
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()
})
Loading

0 comments on commit 586f3b3

Please sign in to comment.