边缘星云

返回

Shiki Code

一些更好地使用Shiki代码的方法

基本用法#

对于.astro文件:

---
import { Code } from 'astro:components'
---

<Code lang='shell' code={`git log --oneline`} />
astro

对于.mdx.md文件:

```shell
git log --oneline
```
markdown

添加标题#

start.sh
#!/bin/bash

bun check &
bun dev
shell
```shell title="start.sh"
#!/bin/bash

bun check &
bun dev
```
markdown

显示差异#

支持添加和删除行。

deploy.sh
#!/bin/bash

bun check
bun lint
bun format
# 不需要vercel或其他ci/cd
bun run build
git push
shell
<!-- 使用时需要删除`\` -->

```shell title="deploy.sh"
#!/bin/bash

bun check
bun lint
bun format # [\!code ++]
# 不需要vercel或其他ci/cd
bun run build # [\!code --]
git push
```
markdown

显示高亮#

<!doctype html><html><head><meta name="color-scheme" content="dark"></head><body style='overflow:hidden'> 
<!-- 高亮单行 -->
<p style='text-align:center'>君指先跃动の光は、私の一生不変の信仰に、唯私の超电磁炮永生き!</p> 

<!-- 你也可以用一个注释高亮多行: -->
<canvas id="can" width="400" height="400" style="background:Black;display:block;margin:20px auto;"></canvas> 
<script> 
var sn=[42,41], dz=43, fx=1, n, score=-1, ctx=document.getElementById("can").getContext("2d"), sz=20;
onkeydown = e => fx = sn[1]-sn[0]==(n=[-1,-20,1,20][(e||event).keyCode-37]||fx) ? fx : n;
function draw(t,c) { ctx.fillStyle=c; ctx.fillRect(t%sz*sz+1,~~(t/sz)*sz+1,sz-2,sz-2); }
!function() { sn.unshift(n=sn[0]+fx);
  if (sn.indexOf(n,1)>0||n<0||n>399||fx==1&&n%sz==0||fx==-1&&n%sz==sz-1) return alert("GAME OVER. Score: "+score);
  draw(n,"Lime");
  if (n==dz) { score++; do{dz=~~(Math.random()*400)}while(sn.indexOf(dz)>=0); draw(dz,"Yellow"); }
  else draw(sn.pop(),"Black");
  setTimeout(arguments.callee,130);
}(); </script> 
</body></html> 
html
<!-- 使用时需要删除`\` -->

```html
<!doctype html><html><head><meta name="color-scheme" content="dark"></head><body style='overflow:hidden'> 
<!-- 高亮单行 -->
<p style='text-align:center'>君指先跃动の光は、私の一生不変の信仰に、唯私の超电磁炮永生き!</p> <!-- [\!code highlight] -->

<!-- 你也可以用一个注释高亮多行: -->
<canvas id="can" width="400" height="400" style="background:Black;display:block;margin:20px auto;"></canvas> 
<script> // [\!code highlight:11]
var sn=[42,41], dz=43, fx=1, n, score=-1, ctx=document.getElementById("can").getContext("2d"), sz=20;
onkeydown = e => fx = sn[1]-sn[0]==(n=[-1,-20,1,20][(e||event).keyCode-37]||fx) ? fx : n;
function draw(t,c) { ctx.fillStyle=c; ctx.fillRect(t%sz*sz+1,~~(t/sz)*sz+1,sz-2,sz-2); }
!function() { sn.unshift(n=sn[0]+fx);
  if (sn.indexOf(n,1)>0||n<0||n>399||fx==1&&n%sz==0||fx==-1&&n%sz==sz-1) return alert("GAME OVER. Score: "+score);
  draw(n,"Lime");
  if (n==dz) { score++; do{dz=~~(Math.random()*400)}while(sn.indexOf(dz)>=0); draw(dz,"Yellow"); }
  else draw(sn.pop(),"Black");
  setTimeout(arguments.callee,130);
}(); </script> 
</body></html> 
```
markdown