Skip to content

docs: Add twoslash-svelte #955

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/svelte.dev/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const config: UserConfig = {
}
},
optimizeDeps: {
exclude: ['@sveltejs/site-kit', '@sveltejs/repl', '@rollup/browser']
exclude: ['@sveltejs/site-kit', '@sveltejs/repl', '@rollup/browser', 'svelte2tsx']
},
ssr: {
noExternal: ['@sveltejs/site-kit', '@sveltejs/repl'],
Expand Down
1 change: 1 addition & 0 deletions packages/site-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@fontsource/eb-garamond": "^5.1.0",
"@fontsource/fira-mono": "^5.1.0",
"@fontsource/fira-sans": "^5.1.0",
"@hugokorte/twoslash-svelte": "^0.0.3",
"@lezer/common": "^1.0.4",
"@replit/codemirror-lang-svelte": "^6.0.0",
"@shikijs/twoslash": "^1.22.0",
Expand Down
10 changes: 7 additions & 3 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ts from 'typescript';
import * as marked from 'marked';
import { codeToHtml, createCssVariablesTheme } from 'shiki';
import { transformerTwoslash } from '@shikijs/twoslash';
import { createTwoslasher } from '@hugokorte/twoslash-svelte';
import { SHIKI_LANGUAGE_MAP, slugify, smart_quotes, transform } from './utils';

interface SnippetOptions {
Expand Down Expand Up @@ -262,7 +263,6 @@ export async function render_content_markdown(
if (needs_controls) {
html += '</div>';
}

html += await syntax_highlight({ filename, language: token.lang, prelude, source, check });

if (converted) {
Expand Down Expand Up @@ -662,6 +662,8 @@ function highlight_spans(content: string, classname: string) {
.join('\n');
}

const twoslasherSvelte = createTwoslasher();

async function syntax_highlight({
prelude,
source,
Expand All @@ -684,7 +686,7 @@ async function syntax_highlight({
theme
})
);
} else if (language === 'js' || language === 'ts') {
} else if (['js', 'ts', 'svelte'].includes(language)) {
/** We need to stash code wrapped in `---` highlights, because otherwise TS will error on e.g. bad syntax, duplicate declarations */
const redactions: string[] = [];

Expand All @@ -695,11 +697,13 @@ async function syntax_highlight({

try {
html = await codeToHtml(prelude + redacted, {
lang: 'ts',
lang: language === 'svelte' ? language : 'ts',
theme,
transformers: check
? [
transformerTwoslash({
langs: ['svelte', 'js', 'ts'],
twoslasher: twoslasherSvelte,
twoslashOptions: {
compilerOptions: {
types: ['svelte', '@sveltejs/kit']
Expand Down
40 changes: 36 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.