Skip to content

Commit a243aa6

Browse files
vicentematusRich Harris
and
Rich Harris
authored
docs: fix js and ts extension when TSToggle is active (#9486)
* docs: fix js and ts extension when TSToggle is active * remove inline-flex * simplify * simplify * exclude certain files/patterns * deduplicate * exclude .d.ts files * simplify --------- Co-authored-by: Rich Harris <[email protected]>
1 parent e9dd255 commit a243aa6

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

documentation/docs/10-getting-started/30-project-structure.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ The `src` directory contains the meat of your project. Everything except `src/ro
5454
- `hooks.server.js` contains your server [hooks](/docs/hooks)
5555
- `service-worker.js` contains your [service worker](/docs/service-workers)
5656

57-
You can use `.ts` files instead of `.js` files, if using TypeScript.
57+
(Whether the project contains `.js` or `.ts` files depends on whether you opt to use TypeScript when you create your project. You can switch between JavaScript and TypeScript in the documentation using the toggle at the bottom of this page.)
5858

59-
If you added [Vitest](https://vitest.dev) when you set up your project, your unit tests will live in the `src` directory with a `.test.js` (or `.test.ts`) extension.
59+
If you added [Vitest](https://vitest.dev) when you set up your project, your unit tests will live in the `src` directory with a `.test.js` extension.
6060

6161
### static
6262

documentation/docs/20-core-concepts/10-routing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ A `+page.svelte` component defines a page of your app. By default, pages are ren
4646
4747
### +page.js
4848

49-
Often, a page will need to load some data before it can be rendered. For this, we add a `+page.js` (or `+page.ts`, if you're TypeScript-inclined) module that exports a `load` function:
49+
Often, a page will need to load some data before it can be rendered. For this, we add a `+page.js` module that exports a `load` function:
5050

5151
```js
5252
/// file: src/routes/blog/[slug]/+page.js
@@ -248,7 +248,7 @@ Like `+layout.js`, `+layout.server.js` can export [page options](page-options)
248248

249249
## +server
250250

251-
As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file (or `+server.ts`) exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, and `OPTIONS` that take a `RequestEvent` argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
251+
As well as pages, you can define routes with a `+server.js` file (sometimes referred to as an 'API route' or an 'endpoint'), which gives you full control over the response. Your `+server.js` file exports functions corresponding to HTTP verbs like `GET`, `POST`, `PATCH`, `PUT`, `DELETE`, and `OPTIONS` that take a `RequestEvent` argument and return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
252252

253253
For example we could create an `/api/random-number` route with a `GET` handler:
254254

documentation/docs/20-core-concepts/20-load.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Before a [`+page.svelte`](routing#page-page-svelte) component (and its containin
66

77
## Page data
88

9-
A `+page.svelte` file can have a sibling `+page.js` (or `+page.ts`) that exports a `load` function, the return value of which is available to the page via the `data` prop:
9+
A `+page.svelte` file can have a sibling `+page.js` that exports a `load` function, the return value of which is available to the page via the `data` prop:
1010

1111
```js
1212
/// file: src/routes/blog/[slug]/+page.js

sites/kit.svelte.dev/src/app.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
display: none;
2525
}
2626
.prefers-ts .ts-version {
27-
display: block;
27+
display: inline;
2828
}
2929
.no-js .ts-toggle {
3030
display: none;

sites/kit.svelte.dev/src/lib/docs/server/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,23 @@ modules.forEach((module) => {
5656
type_links.set(type.name, link);
5757
});
5858
});
59+
5960
/** @param {string} html */
6061
function replace_blank_lines(html) {
6162
// preserve blank lines in output (maybe there's a more correct way to do this?)
6263
return html.replaceAll(/<div class='line'>(&nbsp;)?<\/div>/g, '<div class="line">\n</div>');
6364
}
6465

66+
function dynamic_extensions(text) {
67+
if (text === 'svelte.config.js') return text;
68+
69+
return text.replace(/^(\S+)\.(js|ts)$/, (match, $1) => {
70+
if (match.endsWith('.d.ts')) return match;
71+
if ($1 === 'svelte.config') return match;
72+
return `${$1}<span class="js-version">.js</span><span class="ts-version">.ts</span>`;
73+
});
74+
}
75+
6576
/**
6677
* @param {string} file
6778
*/
@@ -307,7 +318,7 @@ export async function read_file(file) {
307318
codespan: (text) => {
308319
return (
309320
'<code>' +
310-
text.replace(type_regex, (match, prefix, name) => {
321+
dynamic_extensions(text).replace(type_regex, (match, prefix, name) => {
311322
const link = `<a href="${type_links.get(name)}">${name}</a>`;
312323
return `${prefix || ''}${link}`;
313324
}) +
@@ -385,7 +396,8 @@ function parse({ file, body, code, codespan }) {
385396
throw new Error(`Unexpected <h${level}> in ${file}`);
386397
}
387398

388-
return `<h${level} id="${slug}">${html}<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${level}>`;
399+
return `<h${level} id="${slug}">${dynamic_extensions(html)}
400+
<a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${level}>`;
389401
},
390402
code: (source, language) => code(source, language, current),
391403
codespan

0 commit comments

Comments
 (0)