summaryrefslogtreecommitdiff
path: root/packages/integrations/markdoc/src/extensions/prism.ts
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2023-05-25 11:35:07 -0400
committerGravatar GitHub <noreply@github.com> 2023-05-25 11:35:07 -0400
commit16b836411980f18c58ca15712d92cec1b3c95670 (patch)
treecbcd2443815e225cff53ad1d4a94477bfc367f5a /packages/integrations/markdoc/src/extensions/prism.ts
parent223e0131fcd3cfc83575ab9860eb2648b7240b35 (diff)
downloadastro-16b836411980f18c58ca15712d92cec1b3c95670.tar.gz
astro-16b836411980f18c58ca15712d92cec1b3c95670.tar.zst
astro-16b836411980f18c58ca15712d92cec1b3c95670.zip
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
Diffstat (limited to 'packages/integrations/markdoc/src/extensions/prism.ts')
-rw-r--r--packages/integrations/markdoc/src/extensions/prism.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/integrations/markdoc/src/extensions/prism.ts b/packages/integrations/markdoc/src/extensions/prism.ts
new file mode 100644
index 000000000..e28112c9a
--- /dev/null
+++ b/packages/integrations/markdoc/src/extensions/prism.ts
@@ -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>`
+ );
+ },
+ },
+ },
+ };
+}