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

feat(markdown): add support for shiki option langAlias #12039

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Sep 20, 2024

Changes

Closes withastro/roadmap#954

This PR adds support for langAlias

Testing

I added new tests

Docs

withastro/docs#9453

Copy link

changeset-bot bot commented Sep 20, 2024

🦋 Changeset detected

Latest commit: a6a6995

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release labels Sep 20, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@ematipico ematipico changed the title feat(shiki): add support for langAlias feat(markdown): add support for shiki option langAlias Sep 20, 2024
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool, @ematipico ! I checked out the original feature documentation, and made a suggestion below for your consideration!

Comment on lines 6 to 8
Adds a configuration called https://shiki.style/guide/load-lang#custom-language-aliases, that allows a non-supported code language to a known language.

The below example will tell shiki to highlight the code blocks `cjs` using the `javascript` syntax highlighting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Adds a configuration called https://shiki.style/guide/load-lang#custom-language-aliases, that allows a non-supported code language to a known language.
The below example will tell shiki to highlight the code blocks `cjs` using the `javascript` syntax highlighting.
Adds a new Shiki configuration option `langAlias` to use [Shiki's `langAlias` option](https://shiki.style/guide/load-lang#custom-language-aliases). This allows you to define custom aliases for [Shiki's bundled language IDs](https://shiki.style/languages#bundled-languages) (such as the built-in `js` for `javascript`) for Markdown code blocks.
This can be used to map a non-supported code language to a known language, allowing you to indicate the exact language in your source code while using appropriate highlighting.
The example below configures any code blocks using the language alias `cjs` to use `javascript` syntax highlighting.

Maybe something like this? I took a look at the original feature, and their example seems a little more general purpose than only using a language that doesn't have a supported code. It seems you could create an alias for any reason, using anything, so I think describing this as one particular use case vs. "what this thing is for" frames it as being able to be used in other ways, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarah11918 I updated the changeset based on #12039 (comment), can you provide a new suggestion?

@bluwy bluwy added this to the 4.16 milestone Oct 1, 2024
packages/markdown/remark/src/types.ts Outdated Show resolved Hide resolved

Adds a configuration called https://shiki.style/guide/load-lang#custom-language-aliases, that allows a non-supported code language to a known language.

This option requires `langs` to be defined with the correct values. The option will tell shiki which language to load when mapping the alias.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Digging deeper into this now, it seems like a Shiki bug that requires you to specify the langs beforehand for aliases. It might be good to followup upstream later.

Copy link
Member Author

@ematipico ematipico Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the docs show that javascript is in the array

https://shiki.style/guide/load-lang#custom-language-aliases

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you have to. You can specify the langs upfront so that you can syntax highlight the code synchronously, but Astro doesn't do that and will always load the langs dynamically, so in practice the langs field is only for additional custom languages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that I came to peek in on this PR and I'm not sure whether this behaviour has been confirmed yet, so I'll wait to review!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is good for docs review now 👍

@bluwy
Copy link
Member

bluwy commented Oct 5, 2024

Actually it's our own bug 😅 I went ahead and push a commit to fix it. The idea is to use the aliased lang for loadLanguage and not the pre-aliased one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: markdown Related to Markdown (scope) pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants