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

fix: don't throw when Shiki doesn't recognize a language #3911

Merged
merged 2 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-donuts-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Don't throw when Shiki doesn't recognize a language
6 changes: 6 additions & 0 deletions packages/astro/test/astro-markdown-shiki.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ describe('Astro Markdown Shiki', () => {
expect(segments[0].attribs.style).to.be.equal('color: #C9D1D9');
expect(segments[1].attribs.style).to.be.equal('color: #79C0FF');
expect(segments[2].attribs.style).to.be.equal('color: #C9D1D9');

const unknownLang = $('.line').last().html();
expect(unknownLang).to.be.equal('<span style="color: #c9d1d9">This language does not exist</span>')
});

it('<Markdown /> component', async () => {
Expand All @@ -129,6 +132,9 @@ describe('Astro Markdown Shiki', () => {
expect(segments).to.have.lengthOf(3);
expect(segments[0].attribs.style).to.be.equal('color: #C9D1D9');
expect(segments[1].attribs.style).to.be.equal('color: #79C0FF');

const unknownLang = $('.line').last().html();
expect(unknownLang).to.be.equal('<span style="color: #c9d1d9">This language does not exist</span>')
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ import Layout from '../layouts/content.astro';
Iniciar(Rinfo, 1, 1)
fin
```

```unknown
This language does not exist
```
</Markdown>
</Layout>
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ comenzar
Iniciar(Rinfo, 1, 1)
fin
```

```unknown
This language does not exist
```
16 changes: 15 additions & 1 deletion packages/markdown/remark/src/remark-shiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ const remarkShiki = async (

return () => (tree: any) => {
visit(tree, 'code', (node) => {
let html = highlighter!.codeToHtml(node.value, { lang: node.lang ?? 'plaintext' });
let lang: string;

if (typeof node.lang === 'string') {
const langExists = highlighter.getLoadedLanguages().includes(node.lang);
if (langExists) {
lang = node.lang;
} else {
console.warn(`The language "${node.lang}" doesn't exist, falling back to plaintext.`);
lang = 'plaintext';
}
} else {
lang = 'plaintext';
}

let html = highlighter!.codeToHtml(node.value, { lang });

// Q: Couldn't these regexes match on a user's inputted code blocks?
// A: Nope! All rendered HTML is properly escaped.
Expand Down