Skip to content

Commit

Permalink
fix: make markdoc transforms async
Browse files Browse the repository at this point in the history
Markdoc actually supports async transforms already. Now that the shiki
highlighting is async, we also need to make our transform async.

Do note that typescript will be unhappy until markdoc/markdoc#495 is
resolved.
  • Loading branch information
43081j committed Apr 1, 2024
1 parent 74cdd2f commit 5e9b258
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/integrations/markdoc/components/Renderer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Props = {
const { stringifiedAst, config } = Astro.props as Props;
const ast = Markdoc.Ast.fromJSON(stringifiedAst);
const content = Markdoc.transform(ast, config);
const content = await Markdoc.transform(ast, config);
---

{
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/markdoc/src/extensions/shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default async function shiki(config?: ShikiConfig): Promise<AstroMarkdocC
nodes: {
fence: {
attributes: Markdoc.nodes.fence.attributes!,
transform({ attributes }) {
async transform({ attributes }) {
// NOTE: The `meta` from fence code, e.g. ```js {1,3-4}, isn't quite supported by Markdoc.
// Only the `js` part is parsed as `attributes.language` and the rest is ignored. This means
// some Shiki transformers may not work correctly as it relies on the `meta`.
const lang = typeof attributes.language === 'string' ? attributes.language : 'plaintext';
const html = highlighter.highlight(attributes.content, lang);
const html = await highlighter.highlight(attributes.content, lang);

// Use `unescapeHTML` to return `HTMLString` for Astro renderer to inline as HTML
return unescapeHTML(html) as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Markdoc - syntax highlighting', () => {
describe('shiki', () => {
it('transforms with defaults', async () => {
const ast = Markdoc.parse(entry);
const content = Markdoc.transform(ast, await getConfigExtendingShiki());
const content = await Markdoc.transform(ast, await getConfigExtendingShiki());

assert.equal(content.children.length, 2);
for (const codeBlock of content.children) {
Expand All @@ -36,7 +36,7 @@ describe('Markdoc - syntax highlighting', () => {
});
it('transforms with `theme` property', async () => {
const ast = Markdoc.parse(entry);
const content = Markdoc.transform(
const content = await Markdoc.transform(
ast,
await getConfigExtendingShiki({
theme: 'dracula',
Expand All @@ -53,7 +53,7 @@ describe('Markdoc - syntax highlighting', () => {
});
it('transforms with `wrap` property', async () => {
const ast = Markdoc.parse(entry);
const content = Markdoc.transform(
const content = await Markdoc.transform(
ast,
await getConfigExtendingShiki({
wrap: true,
Expand All @@ -76,7 +76,7 @@ describe('Markdoc - syntax highlighting', () => {
const config = await setupConfig({
extends: [prism()],
});
const content = Markdoc.transform(ast, config);
const content = await Markdoc.transform(ast, config);

assert.equal(content.children.length, 2);
const [tsBlock, cssBlock] = content.children;
Expand Down

0 comments on commit 5e9b258

Please sign in to comment.