Skip to content

Commit 8b56182

Browse files
authored
chore: markdown rendering fixes (#1094)
* chore: markdown fixes - handle `@param` on properties - properly run markdown on `.js` files. Previously `.js` was treated as TS, which resulted in errors because the jsdoc was ignored. This was done this way because twoslash does not run on `js` by default, but there's a different way to make it still run on them while respecting jsdoc. * set relevant compiler options * see if that helps * another one
1 parent 83881c6 commit 8b56182

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

apps/svelte.dev/content/docs/kit/30-advanced/25-errors.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ This throws an exception that SvelteKit catches, causing it to set the response
5858
You can add extra properties to the error object if needed...
5959

6060
```js
61-
import { error } from '@sveltejs/kit';
62-
61+
// @filename: ambient.d.ts
6362
declare global {
6463
namespace App {
6564
interface Error {
@@ -68,7 +67,10 @@ declare global {
6867
}
6968
}
7069
}
70+
export {}
7171

72+
// @filename: index.js
73+
import { error } from '@sveltejs/kit';
7274
// ---cut---
7375
error(404, {
7476
message: 'Not found',

apps/svelte.dev/content/docs/svelte/02-runes/02-$state.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ todos[0].done = !todos[0].done;
4545
If you push a new object to the array, it will also be proxified:
4646

4747
```js
48-
// @filename: ambient.d.ts
49-
declare global {
50-
const todos: Array<{ done: boolean, text: string }>
51-
}
52-
53-
// @filename: index.js
48+
let todos = [{ done: false, text: 'add more todos' }];
5449
// ---cut---
5550
todos.push({
5651
done: false,

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,31 @@ async function convert_to_ts(js_code: string, indent = '', offset = '') {
498498
if (code.original[end - 1] === ';') end -= 1;
499499
code.appendLeft(end, ` satisfies ${satisfies}`);
500500
}
501+
} else if (
502+
(ts.isPropertyAssignment(node) && ts.isArrowFunction(node.initializer)) ||
503+
ts.isMethodDeclaration(node)
504+
) {
505+
if (type) {
506+
throw new Error('@type on property methods does nothing');
507+
}
508+
509+
const parameters = ts.isMethodDeclaration(node)
510+
? node.parameters
511+
: (node.initializer as ts.ArrowFunction).parameters;
512+
for (let i = 0; i < parameters.length; i += 1) {
513+
if (params[i] !== undefined) {
514+
code.appendLeft(parameters[i].getEnd(), `: ${params[i]}`);
515+
}
516+
}
517+
518+
if (returns) {
519+
const body = ts.isMethodDeclaration(node)
520+
? node.body
521+
: (node.initializer as ts.ArrowFunction).body;
522+
let start = body!.getStart();
523+
while (code.original[start - 1] !== ')') start -= 1;
524+
code.appendLeft(start, `: ${returns}`);
525+
}
501526
} else {
502527
throw new Error('Unhandled @type JsDoc->TS conversion: ' + js_code);
503528
}
@@ -699,16 +724,20 @@ async function syntax_highlight({
699724

700725
try {
701726
html = await codeToHtml(prelude + redacted, {
702-
lang: 'ts',
727+
lang: language,
703728
theme,
704729
transformers: check
705730
? [
706731
transformerTwoslash({
707732
twoslashOptions: {
708733
compilerOptions: {
734+
allowJs: true,
735+
checkJs: true,
709736
types: ['svelte', '@sveltejs/kit']
710737
}
711-
}
738+
},
739+
// by default, twoslash does not run on .js files, change that through this option
740+
filter: () => true
712741
})
713742
]
714743
: []

0 commit comments

Comments
 (0)