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

Cannot add language label for shiki code block #6869

Closed
1 task done
craxrev opened this issue Apr 18, 2023 · 1 comment
Closed
1 task done

Cannot add language label for shiki code block #6869

craxrev opened this issue Apr 18, 2023 · 1 comment

Comments

@craxrev
Copy link

craxrev commented Apr 18, 2023

What version of astro are you using?

2.1.7

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

pnpm

What operating system are you using?

Mac

What browser are you using?

Chrome

Describe the Bug

Currently there's no way to add a language label on top of the shiki code block, currently we are rendering the shiki code block this way:

<pre is:raw class="astro-code" style="..">
  <code>..</code>
</pre>

What will solve the issue, is to have some attribute in the <pre> tag that has the language name, like data-lang="js" or rel="html" or even inside the classname like: class="astro-code js":

<pre data-lang="html" is:raw class="astro-code" style="..">
  <code>..</code>
</pre>

And using css we will render the label:

.prose pre::before {
  content: attr(data-lang);
  position: absolute;
  right: 0.4rem;
  top: 0.4rem;
  ..
}

The result would be something like the following:

image

Currently I found only two mentions (discussions) on this issue:

Link to Minimal Reproducible Example

N/A

Participation

  • I am willing to submit a pull request for this issue.
@bluwy
Copy link
Member

bluwy commented Apr 27, 2023

This make sense to me, but I think we should keep feature requests in https://github.com/withastro/roadmap, so closing this for now. If you'd like to contribute, the change would be at:

function repairShikiTheme(html: string): string {
// Replace "shiki" class naming with "astro"
html = html.replace(/<pre class="(.*?)shiki(.*?)"/, '<pre class="$1astro-code$2"');
// Handle code wrapping
// if wrap=null, do nothing.
if (wrap === false) {
html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto;"');
} else if (wrap === true) {
html = html.replace(
/style="(.*?)"/,
'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
);
}
return html;
}

The lang can be passed from

const html = repairShikiTheme(_html);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants