Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy loaded shiki languages during syntax highlighting #10618

Merged
merged 10 commits into from
Apr 1, 2024
Prev Previous commit
Next Next commit
fix: make markdoc transforms async
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
commit 5e9b2589ed483db2f8ac108e9db9b30dba5d014e
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