Skip to content

Commit 82122ee

Browse files
Add Shiki
1 parent 797ab80 commit 82122ee

File tree

11 files changed

+148
-1655
lines changed

11 files changed

+148
-1655
lines changed

astro.config.mjs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,60 @@ import youtubeEmbed from "./src/plugins/youtubeEmbed.mjs"
88
import responsiveImages from "./src/plugins/responsiveImages.mjs"
99
import codeHighlightPre from "./src/plugins/codeHighlightPre.mjs"
1010
import preact from "@astrojs/preact" // @ts-check
11+
import remarkShiki from "@stefanprobst/remark-shiki"
12+
import * as shiki from "shiki"
13+
import rangeParser from "parse-numeric-range"
1114

1215
import { defineConfig } from "astro/config"
1316
import sitemap from "@astrojs/sitemap"
1417

1518
// https://astro.build/config
1619
import mdx from "@astrojs/mdx"
1720

21+
const shikiHighlighter = await shiki.getHighlighter({ theme: "dark-plus" })
22+
1823
// https://astro.build/config
1924
export default defineConfig(
2025
/** @type {import('astro').AstroUserConfig} */
2126
{
2227
integrations: [preact(), sitemap(), mdx()],
2328
site: "https://blog.webdevsimplified.com",
2429
markdown: {
25-
// TODO: Maybe use shiki and see if it is better and has line highlighting.
26-
syntaxHighlight: "prism",
27-
remarkPlugins: ["remark-gfm", youtubeEmbed, responsiveImages],
30+
syntaxHighlight: false,
31+
// TODO: Add official Shiki integration when line highlighting is supported
32+
remarkPlugins: [
33+
[
34+
remarkShiki,
35+
{
36+
highlighter: shikiHighlighter,
37+
parseMeta,
38+
},
39+
],
40+
youtubeEmbed,
41+
responsiveImages,
42+
],
2843
},
2944
}
3045
)
3146

32-
// TODO: Add line highlighting to code blocks.
47+
function parseMeta(meta, { lang }) {
48+
if (lang != null) parseLang(lang)
49+
50+
if (meta == null) return undefined
51+
if (meta.length === 0) return undefined
52+
53+
return rangeParser(meta.replaceAll(/[{}]/g, "")).map(line => {
54+
return { line, classes: ["highlighted-line"] }
55+
})
56+
}
57+
58+
function parseLang(lang) {
59+
if (shikiHighlighter.getLoadedLanguages().includes(lang)) return
60+
61+
console.error(`The language "${lang}" doesn't exist`)
62+
}
63+
3364
// TODO: Check to see if my plugins can be replaced with official integrations.
65+
// TODO: Implement experimental assets integration
3466
// TODO: Move over to the content folder
3567
// TODO: Add TS Support

0 commit comments

Comments
 (0)