-
-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathrenderer.ts
More file actions
79 lines (68 loc) · 3.05 KB
/
renderer.ts
File metadata and controls
79 lines (68 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { render_content_markdown } from '@sveltejs/site-kit/markdown';
export const render_content = (
filename: string,
body: string,
options: { check?: boolean } = {}
) => {
return render_content_markdown(filename, body, options, (filename, source) => {
// TODO these are copied from Svelte and SvelteKit - adjust for new filenames
const injected: string[] = [];
if (filename.includes('svelte-compiler')) {
injected.push('// @esModuleInterop');
}
if (filename.includes('svelte.md')) {
injected.push('// @errors: 2304');
}
// Actions JSDoc examples are invalid. Too many errors, edge cases
// TODO: d.ts files are not properly supported right now, fix this later
if (filename.includes('svelte-action') || source.includes(' declare const ')) {
injected.push('// @noErrors');
}
if (filename.includes('typescript')) {
injected.push('// @errors: 2304');
}
// twoslash doesn't recognise these as SvelteKit imports, so we need to
// explicitly reference the types in these instances
if (
source.includes('$app/') ||
source.includes('$service-worker')
) {
injected.push(`// @filename: ambient-kit.d.ts`, `/// <reference types="@sveltejs/kit" />`);
}
if (source.includes('$env/')) {
// TODO we're hardcoding static env vars that are used in code examples
// in the types, which isn't... totally ideal, but will do for now
injected.push(
`declare module '$env/dynamic/private' { export const env: Record<string, string>; }`,
`declare module '$env/dynamic/public' { export const env: Record<string, string>; }`,
// TODO: detect when a snippet imports from $env/static then generate the types on the fly
`declare module '$env/static/private' { export const API_KEY: string; export const BYPASS_TOKEN: string; export const VERCEL_COMMIT_REF: string; }`,
`declare module '$env/static/public' { export const PUBLIC_BASE_URL: string; }`
);
}
if (source.includes('./$types') && !source.includes('@filename: $types.d.ts')) {
injected.push(
`// @filename: $types.d.ts`,
`import type * as Kit from '@sveltejs/kit';`,
`export type PageLoad = Kit.Load<Record<string, any>>;`,
`export type PageServerLoad = Kit.ServerLoad<Record<string, any>>;`,
`export type LayoutLoad = Kit.Load<Record<string, any>>;`,
`export type LayoutServerLoad = Kit.ServerLoad<Record<string, any>>;`,
`export type RequestHandler = Kit.RequestHandler<Record<string, any>>;`,
`export type Action = Kit.Action<Record<string, any>>;`,
`export type Actions = Kit.Actions<Record<string, any>>;`,
`export type EntryGenerator = () => Promise<Array<Record<string, any>>> | Array<Record<string, any>>;`
);
}
// special case — we need to make allowances for code snippets coming
// from e.g. ambient.d.ts
if (filename.endsWith('$env-all.md') || filename.endsWith('$app-forms.md')) {
injected.push('// @errors: 7006 7031');
}
// another special case
if (source.includes('$lib/types')) {
injected.push(`declare module '$lib/types' { export interface User {} }`);
}
return injected.join('\n');
});
};