Skip to content

chore: markdown rendering fixes #1094

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

Merged
merged 4 commits into from
Jan 7, 2025
Merged
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
6 changes: 4 additions & 2 deletions apps/svelte.dev/content/docs/kit/30-advanced/25-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ This throws an exception that SvelteKit catches, causing it to set the response
You can add extra properties to the error object if needed...

```js
import { error } from '@sveltejs/kit';

// @filename: ambient.d.ts
declare global {
namespace App {
interface Error {
Expand All @@ -68,7 +67,10 @@ declare global {
}
}
}
export {}

// @filename: index.js
import { error } from '@sveltejs/kit';
// ---cut---
error(404, {
message: 'Not found',
Expand Down
7 changes: 1 addition & 6 deletions apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ todos[0].done = !todos[0].done;
If you push a new object to the array, it will also be proxified:

```js
// @filename: ambient.d.ts
declare global {
const todos: Array<{ done: boolean, text: string }>
}

// @filename: index.js
let todos = [{ done: false, text: 'add more todos' }];
// ---cut---
todos.push({
done: false,
Expand Down
33 changes: 31 additions & 2 deletions packages/site-kit/src/lib/markdown/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,31 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
if (code.original[end - 1] === ';') end -= 1;
code.appendLeft(end, ` satisfies ${satisfies}`);
}
} else if (
(ts.isPropertyAssignment(node) && ts.isArrowFunction(node.initializer)) ||
ts.isMethodDeclaration(node)
) {
if (type) {
throw new Error('@type on property methods does nothing');
}

const parameters = ts.isMethodDeclaration(node)
? node.parameters
: (node.initializer as ts.ArrowFunction).parameters;
for (let i = 0; i < parameters.length; i += 1) {
if (params[i] !== undefined) {
code.appendLeft(parameters[i].getEnd(), `: ${params[i]}`);
}
}

if (returns) {
const body = ts.isMethodDeclaration(node)
? node.body
: (node.initializer as ts.ArrowFunction).body;
let start = body!.getStart();
while (code.original[start - 1] !== ')') start -= 1;
code.appendLeft(start, `: ${returns}`);
}
} else {
throw new Error('Unhandled @type JsDoc->TS conversion: ' + js_code);
}
Expand Down Expand Up @@ -699,16 +724,20 @@ async function syntax_highlight({

try {
html = await codeToHtml(prelude + redacted, {
lang: 'ts',
lang: language,
theme,
transformers: check
? [
transformerTwoslash({
twoslashOptions: {
compilerOptions: {
allowJs: true,
checkJs: true,
types: ['svelte', '@sveltejs/kit']
}
}
},
// by default, twoslash does not run on .js files, change that through this option
filter: () => true
})
]
: []
Expand Down
Loading