Skip to content

Commit

Permalink
Change syntax highlighter from PrismJS to Shiki
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed Nov 20, 2024
1 parent 8db8e38 commit 37d2dff
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 233 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
package-lock.json
pnpm-lock.yaml
yarn.lock
src/lib/components/ui/**
src/lib/components/ui/**
.github/*.md
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"@sveltejs/vite-plugin-svelte": "^4.0.1",
"@tailwindcss/typography": "^0.5.15",
"@types/eslint": "^9.6.1",
"@types/prismjs": "^1.26.5",
"autoprefixer": "^10.4.20",
"bits-ui": "1.0.0-next.60",
"clsx": "^2.1.1",
Expand All @@ -61,9 +60,8 @@
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.2.8",
"prettier-plugin-tailwindcss": "^0.6.9",
"prism-svelte": "^0.5.0",
"prismjs": "^1.29.0",
"publint": "^0.2.12",
"shiki": "^1.23.1",
"svelte": "^5.2.4",
"svelte-check": "^4.0.9",
"tailwind-merge": "^2.5.4",
Expand Down
324 changes: 305 additions & 19 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

41 changes: 33 additions & 8 deletions src/content/CodeBlock.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
<script lang="ts">
import Prism from 'prismjs';
import 'prism-svelte';
import { createHighlighterCoreSync, createJavaScriptRegexEngine } from 'shiki';
import svelte from 'shiki/langs/svelte.mjs';
import dark from 'shiki/themes/github-dark-default.mjs';
let {
content,
lang = 'svelte'
content
}: {
content: string;
lang: string;
} = $props();
let highlighted = $derived(Prism.highlight(content, Prism.languages[lang], 'svelte'));
const shiki = createHighlighterCoreSync({
themes: [dark],
langs: [svelte],
engine: createJavaScriptRegexEngine()
});
const highlighted = $derived.by(() => {
return shiki.codeToHtml(content.trim(), { lang: 'svelte', theme: 'github-dark-default' });
});
</script>

<div class="my-6 text-[14px] subpixel-antialiased">
<pre class={`language-${lang}`}><code class={`language-${lang}`}>{@html highlighted}</code></pre>
<div class="my-6 subpixel-antialiased">
{@html highlighted}
</div>

<style>
:global(.shiki code) {
tab-size: 2;
counter-reset: step;
counter-increment: step 0;
}
:global(.shiki code .line::before) {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
display: inline-block;
text-align: right;
color: rgba(115, 138, 148, 0.4);
}
</style>
2 changes: 1 addition & 1 deletion src/content/examples/clusters/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: Use MapLibre's built-in functions to visualize points as clusters.

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
2 changes: 1 addition & 1 deletion src/content/examples/complex/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiu

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
2 changes: 1 addition & 1 deletion src/content/examples/plain/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: Plain map.

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
2 changes: 1 addition & 1 deletion src/content/examples/side-by-side/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: Synchronize two maps.

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
2 changes: 1 addition & 1 deletion src/content/examples/terrain/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: 3D terrain and sky.

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
2 changes: 1 addition & 1 deletion src/content/examples/video-on-a-map/content.svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ description: Display a video.

<Demo />

<CodeBlock content={demoRaw} />
<CodeBlock content={demoRaw} />
192 changes: 0 additions & 192 deletions src/lib/assets/prism-dark.css

This file was deleted.

2 changes: 0 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import '$lib/assets/fonts/geist.css';
import Header from './Header.svelte';
import '$lib/assets/prism-dark.css';
let { children } = $props();
</script>

Expand Down
25 changes: 23 additions & 2 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { mdsvex } from 'mdsvex';
// mdsvex
import { mdsvex, escapeSvelte } from 'mdsvex';
import { createHighlighter } from 'shiki';

const theme = 'github-dark-default';
const highlighter = await createHighlighter({
themes: [theme],
langs: ['javascript', 'typescript', 'svelte']
});

/** @type {import('mdsvex').MdsvexOptions} */
const mdsvexOptions = {
highlight: {
highlighter: async (code, lang = 'text') => {
const html = escapeSvelte(highlighter.codeToHtml(code, { lang, theme }));
return `{@html \`${html}\` }`;
}
},
extensions: ['.svelte.md', '.svx']
};

// SvelteKit
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: [vitePreprocess(), mdsvex({ extensions: ['.svelte.md', '.svx'] })],
preprocess: [vitePreprocess(), mdsvex(mdsvexOptions)],

kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
Expand Down

0 comments on commit 37d2dff

Please sign in to comment.