Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Markdoc - improve syntax highlighting support (#7209)
* feat: prism and shiki support, with better exports! * chore: update tests * chore: fix lock * chore: add prism test * chore: remove `async` from prism * docs: update syntax highlight readme * chore: changeset * edit: remove `await` from prism docs * chore: update old changest with new shiki instructions * fix: add trailing newline on ts-expect-error * refactor: resolve promises internally * docs: remove `await` from shiki examples
- Loading branch information
1 parent
223e013
commit 16b8364
Showing
11 changed files
with
200 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
'@astrojs/markdoc': patch | ||
--- | ||
|
||
Add a built-in extension for syntax highlighting with Prism. Apply to your Markdoc config using the `extends` property: | ||
|
||
```js | ||
// markdoc.config.mjs | ||
import { defineMarkdocConfig } from '@astrojs/markdoc/config'; | ||
import prism from '@astrojs/markdoc/prism'; | ||
|
||
export default defineMarkdocConfig({ | ||
extends: [prism()], | ||
}) | ||
``` | ||
|
||
Learn more in the [`@astrojs/markdoc` README.](https://docs.astro.build/en/guides/integrations-guide/markdoc/#syntax-highlighting) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// leave space, so organize imports doesn't mess up comments | ||
// @ts-expect-error Cannot find module 'astro/runtime/server/index.js' or its corresponding type declarations. | ||
import { unescapeHTML } from 'astro/runtime/server/index.js'; | ||
|
||
import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter'; | ||
import { Markdoc, type AstroMarkdocConfig } from '../config.js'; | ||
|
||
export default function prism(): AstroMarkdocConfig { | ||
return { | ||
nodes: { | ||
fence: { | ||
attributes: Markdoc.nodes.fence.attributes!, | ||
transform({ attributes: { language, content } }) { | ||
const { html, classLanguage } = runHighlighterWithAstro(language, content); | ||
|
||
// Use `unescapeHTML` to return `HTMLString` for Astro renderer to inline as HTML | ||
return unescapeHTML( | ||
`<pre class="${classLanguage}"><code class="${classLanguage}">${html}</code></pre>` | ||
); | ||
}, | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.