0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-29 08:32:05 +01:00

move FAQ to /docs/faq (#8672)

* move FAQ to /docs/faq

* remove some more junk

* simplify

* escape

* move redirect to server, prerender

---------

Co-authored-by: Rich Harris <git@rich-harris.dev>
This commit is contained in:
Rich Harris 2023-06-01 11:34:17 -04:00 committed by GitHub
parent 2efd78cba4
commit 83d56713ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 196 additions and 295 deletions

View File

@ -0,0 +1,142 @@
---
title: Frequently asked questions
---
## I'm new to Svelte. Where should I start?
We think the best way to get started is playing through the interactive [tutorial](/tutorial). Each step there is mainly focused on one specific aspect and is easy to follow. You'll be editing and running real Svelte components right in your browser.
Five to ten minutes should be enough to get you up and running. An hour and a half should get you through the entire tutorial.
## Where can I get support?
If your question is about certain syntax, the [API page](https://svelte.dev/docs) is a good place to start.
Stack Overflow is a popular forum to ask code-level questions or if youre stuck with a specific error. Read through the existing questions tagged with [Svelte](https://stackoverflow.com/questions/tagged/svelte+or+svelte-3) or [ask your own](https://stackoverflow.com/questions/ask?tags=svelte)!
There are online forums and chats which are a great place for discussion about best practices, application architecture or just to get to know fellow Svelte users. [Our Discord](https://svelte.dev/chat) or [the Reddit channel](https://www.reddit.com/r/sveltejs/) are examples of that. If you have an answerable code-level question, Stack Overflow is usually a better fit.
## Are there any third-party resources?
Svelte Society maintains a [list of books and videos](https://sveltesociety.dev/resources).
## How can I get VS Code to syntax-highlight my .svelte files?
There is an [official VS Code extension for Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Is there a tool to automatically format my .svelte files?
You can use prettier with the [prettier-plugin-svelte](https://www.npmjs.com/package/prettier-plugin-svelte) plugin.
## How do I document my components?
In editors which use the Svelte Language Server you can document Components, functions and exports using specially formatted comments.
````svelte
<script>
/** What should we call the user? */
export let name = 'world';
</script>
<!--
@component
Here's some documentation for this component.
It will show up on hover.
- You can use markdown here.
- You can also use code blocks here.
- Usage:
```tsx
<main name="Arethra">
```
-->
<main>
<h1>
Hello, {name}
</h1>
</main>
````
Note: The `@component` is necessary in the HTML comment which describes your component.
## What about TypeScript support?
You need to install a preprocessor such as [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess). You can run type checking from the command line with [svelte-check](https://www.npmjs.com/package/svelte-check).
To declare the type of a reactive variable in a Svelte template, you should use the following syntax:
```ts
const count: number = 100;
// ---cut---
let x: number;
$: x = count + 1;
```
To import a type or interface make sure to use [TypeScript's `type` modifier](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export):
```ts
// @filename: SomeFile.ts
export interface SomeInterface {
foo: string;
}
// @filename: index.ts
// ---cut---
import type { SomeInterface } from './SomeFile';
```
You must use the `type` modifier because `svelte-preprocess` doesn't know whether an import is a type or a value — it only transpiles one file at a time without knowledge of the other files and therefore can't safely erase imports which only contain types without this modifier present.
## Does Svelte scale?
There will be a blog post about this eventually, but in the meantime, check out [this issue](https://github.com/sveltejs/svelte/issues/2546).
## Is there a UI component library?
There are several UI component libraries as well as standalone components. Find them under the [components section](https://sveltesociety.dev/components) of the Svelte Society website.
## How do I test Svelte apps?
How your application is structured and where logic is defined will determine the best way to ensure it is properly tested. It is important to note that not all logic belongs within a component - this includes concerns such as data transformation, cross-component state management, and logging, among others. Remember that the Svelte library has its own test suite, so you do not need to write tests to validate implementation details provided by Svelte.
A Svelte application will typically have three different types of tests: Unit, Component, and End-to-End (E2E).
_Unit Tests_: Focus on testing business logic in isolation. Often this is validating individual functions and edge cases. By minimizing the surface area of these tests they can be kept lean and fast, and by extracting as much logic as possible from your Svelte components more of your application can be covered using them. When creating a new SvelteKit project, you will be asked whether you would like to setup [Vitest](https://vitest.dev/) for unit testing. There are a number of other test runners that could be used as well.
_Component Tests_: Validating that a Svelte component mounts and interacts as expected throughout its lifecycle requires a tool that provides a Document Object Model (DOM). Components can be compiled (since Svelte is a compiler and not a normal library) and mounted to allow asserting against element structure, listeners, state, and all the other capabilities provided by a Svelte component. Tools for component testing range from an in-memory implementation like jsdom paired with a test runner like [Vitest](https://vitest.dev/) to solutions that leverage an actual browser to provide a visual testing capability such as [Playwright](https://playwright.dev/docs/test-components) or [Cypress](https://www.cypress.io/).
_End-to-End Tests_: To ensure your users are able to interact with your application it is necessary to test it as a whole in a manner as close to production as possible. This is done by writing end-to-end (E2E) tests which load and interact with a deployed version of your application in order to simulate how the user will interact with your application. When creating a new SvelteKit project, you will be asked whether you would like to setup [Playwright](https://playwright.dev/) for end-to-end testing. There are many other E2E test libraries available for use as well.
Some resources for getting started with testing:
- [Svelte Testing Library](https://testing-library.com/docs/svelte-testing-library/example/)
- [Svelte Component Testing in Cypress](https://docs.cypress.io/guides/component-testing/svelte/overview)
- [Example using vitest](https://github.com/vitest-dev/vitest/tree/main/examples/svelte)
- [Example using uvu test runner with JSDOM](https://github.com/lukeed/uvu/tree/master/examples/svelte)
- [Test Svelte components using Vitest & Playwright](https://davipon.hashnode.dev/test-svelte-component-using-vitest-playwright)
- [Component testing with WebdriverIO](https://webdriver.io/docs/component-testing/svelte)
## Is there a router?
The official routing library is [SvelteKit](https://kit.svelte.dev/). SvelteKit provides a filesystem router, server-side rendering (SSR), and hot module reloading (HMR) in one easy-to-use package. It shares similarities with Next.js for React.
However, you can use any router library. A lot of people use [page.js](https://github.com/visionmedia/page.js). There's also [navaid](https://github.com/lukeed/navaid), which is very similar. And [universal-router](https://github.com/kriasoft/universal-router), which is isomorphic with child routes, but without built-in history support.
If you prefer a declarative HTML approach, there's the isomorphic [svelte-routing](https://github.com/EmilTholin/svelte-routing) library and a fork of it called [svelte-navigator](https://github.com/mefechoel/svelte-navigator) containing some additional functionality.
If you need hash-based routing on the client side, check out [svelte-spa-router](https://github.com/ItalyPaleAle/svelte-spa-router) or [abstract-state-router](https://github.com/TehShrike/abstract-state-router/).
[Routify](https://routify.dev) is another filesystem-based router, similar to SvelteKit's router. Version 3 supports Svelte's native SSR.
You can see a [community-maintained list of routers on sveltesociety.dev](https://sveltesociety.dev/components#routers).
## Is Svelte v2 still available?
New features aren't being added to it, and bugs will probably only be fixed if they are extremely nasty or present some sort of security vulnerability.
The documentation is still available [here](https://v2.svelte.dev/guide).
## How do I do hot module reloading?
We recommend using [SvelteKit](https://kit.svelte.dev/), which supports HMR out of the box and is built on top of [Vite](https://vitejs.dev/) and [svelte-hmr](https://github.com/sveltejs/svelte-hmr). There are also community plugins for [rollup](https://github.com/rixo/rollup-plugin-svelte-hot) and [webpack](https://github.com/sveltejs/svelte-loader).

View File

@ -1,7 +0,0 @@
---
question: I'm new to Svelte. Where should I start?
---
We think the best way to get started is playing through the interactive [tutorial](/tutorial). Each step there is mainly focused on one specific aspect and is easy to follow. You'll be editing and running real Svelte components right in your browser.
Five to ten minutes should be enough to get you up and running. An hour and a half should get you through the entire tutorial.

View File

@ -1,9 +0,0 @@
---
question: Where can I get support?
---
If your question is about certain syntax, the [API page](https://svelte.dev/docs) is a good place to start.
Stack Overflow is a popular forum to ask code-level questions or if youre stuck with a specific error. Read through the existing questions tagged with [Svelte](https://stackoverflow.com/questions/tagged/svelte+or+svelte-3) or [ask your own](https://stackoverflow.com/questions/ask?tags=svelte)!
There are online forums and chats which are a great place for discussion about best practices, application architecture or just to get to know fellow Svelte users. [Our Discord](https://svelte.dev/chat) or [the Reddit channel](https://www.reddit.com/r/sveltejs/) are examples of that. If you have an answerable code-level question, Stack Overflow is usually a better fit.

View File

@ -1,7 +0,0 @@
---
question: Is Svelte v2 still available?
---
New features aren't being added to it, and bugs will probably only be fixed if they are extremely nasty or present some sort of security vulnerability.
The documentation is still available [here](https://v2.svelte.dev/guide).

View File

@ -1,5 +0,0 @@
---
question: How do I do hot module reloading?
---
We recommend using [SvelteKit](https://kit.svelte.dev/), which supports HMR out of the box and is built on top of [Vite](https://vitejs.dev/) and [svelte-hmr](https://github.com/sveltejs/svelte-hmr). There are also community plugins for [rollup](https://github.com/rixo/rollup-plugin-svelte-hot) and [webpack](https://github.com/sveltejs/svelte-loader).

View File

@ -1,5 +0,0 @@
---
question: Are there any third-party resources?
---
Svelte Society maintains a [list of books and videos](https://sveltesociety.dev/resources).

View File

@ -1,5 +0,0 @@
---
question: How can I get VS Code to syntax-highlight my .svelte files?
---
There is an [official VS Code extension for Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).

View File

@ -1,5 +0,0 @@
---
question: Is there a tool to automatically format my .svelte files?
---
You can use prettier with the [prettier-plugin-svelte](https://www.npmjs.com/package/prettier-plugin-svelte) plugin.

View File

@ -1,32 +0,0 @@
---
question: How do I document my components?
---
In editors which use the Svelte Language Server you can document Components, functions and exports using specially formatted comments.
````svelte
<script>
/** What should we call the user? */
export let name = 'world';
</script>
<!--
@component
Here's some documentation for this component.
It will show up on hover.
- You can use markdown here.
- You can also use code blocks here.
- Usage:
```tsx
<main name="Arethra">
```
-->
<main>
<h1>
Hello, {name}
</h1>
</main>
````
Note: The `@component` is necessary in the HTML comment which describes your component.

View File

@ -1,30 +0,0 @@
---
question: What about TypeScript support?
---
You need to install a preprocessor such as [svelte-preprocess](https://github.com/sveltejs/svelte-preprocess). You can run type checking from the command line with [svelte-check](https://www.npmjs.com/package/svelte-check).
To declare the type of a reactive variable in a Svelte template, you should use the following syntax:
```ts
const count: number = 100;
// ---cut---
let x: number;
$: x = count + 1;
```
To import a type or interface make sure to use [TypeScript's `type` modifier](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export):
```ts
// @filename: SomeFile.ts
export interface SomeInterface {
foo: string;
}
// @filename: index.ts
// ---cut---
import type { SomeInterface } from './SomeFile';
```
You must use the `type` modifier because `svelte-preprocess` doesn't know whether an import is a type or a value — it only transpiles one file at a time without knowledge of the other files and therefore can't safely erase imports which only contain types without this modifier present.

View File

@ -1,5 +0,0 @@
---
question: Does Svelte scale?
---
There will be a blog post about this eventually, but in the meantime, check out [this issue](https://github.com/sveltejs/svelte/issues/2546).

View File

@ -1,5 +0,0 @@
---
question: Is there a UI component library?
---
There are several UI component libraries as well as standalone components. Find them under the [components section](https://sveltesociety.dev/components) of the Svelte Society website.

View File

@ -1,22 +0,0 @@
---
question: How do I test Svelte apps?
---
How your application is structured and where logic is defined will determine the best way to ensure it is properly tested. It is important to note that not all logic belongs within a component - this includes concerns such as data transformation, cross-component state management, and logging, among others. Remember that the Svelte library has its own test suite, so you do not need to write tests to validate implementation details provided by Svelte.
A Svelte application will typically have three different types of tests: Unit, Component, and End-to-End (E2E).
_Unit Tests_: Focus on testing business logic in isolation. Often this is validating individual functions and edge cases. By minimizing the surface area of these tests they can be kept lean and fast, and by extracting as much logic as possible from your Svelte components more of your application can be covered using them. When creating a new SvelteKit project, you will be asked whether you would like to setup [Vitest](https://vitest.dev/) for unit testing. There are a number of other test runners that could be used as well.
_Component Tests_: Validating that a Svelte component mounts and interacts as expected throughout its lifecycle requires a tool that provides a Document Object Model (DOM). Components can be compiled (since Svelte is a compiler and not a normal library) and mounted to allow asserting against element structure, listeners, state, and all the other capabilities provided by a Svelte component. Tools for component testing range from an in-memory implementation like jsdom paired with a test runner like [Vitest](https://vitest.dev/) to solutions that leverage an actual browser to provide a visual testing capability such as [Playwright](https://playwright.dev/docs/test-components) or [Cypress](https://www.cypress.io/).
_End-to-End Tests_: To ensure your users are able to interact with your application it is necessary to test it as a whole in a manner as close to production as possible. This is done by writing end-to-end (E2E) tests which load and interact with a deployed version of your application in order to simulate how the user will interact with your application. When creating a new SvelteKit project, you will be asked whether you would like to setup [Playwright](https://playwright.dev/) for end-to-end testing. There are many other E2E test libraries available for use as well.
Some resources for getting started with testing:
- [Svelte Testing Library](https://testing-library.com/docs/svelte-testing-library/example/)
- [Svelte Component Testing in Cypress](https://docs.cypress.io/guides/component-testing/svelte/overview)
- [Example using vitest](https://github.com/vitest-dev/vitest/tree/main/examples/svelte)
- [Example using uvu test runner with JSDOM](https://github.com/lukeed/uvu/tree/master/examples/svelte)
- [Test Svelte components using Vitest & Playwright](https://davipon.hashnode.dev/test-svelte-component-using-vitest-playwright)
- [Component testing with WebdriverIO](https://webdriver.io/docs/component-testing/svelte)

View File

@ -1,15 +0,0 @@
---
question: Is there a router?
---
The official routing library is [SvelteKit](https://kit.svelte.dev/). SvelteKit provides a filesystem router, server-side rendering (SSR), and hot module reloading (HMR) in one easy-to-use package. It shares similarities with Next.js for React.
However, you can use any router library. A lot of people use [page.js](https://github.com/visionmedia/page.js). There's also [navaid](https://github.com/lukeed/navaid), which is very similar. And [universal-router](https://github.com/kriasoft/universal-router), which is isomorphic with child routes, but without built-in history support.
If you prefer a declarative HTML approach, there's the isomorphic [svelte-routing](https://github.com/EmilTholin/svelte-routing) library and a fork of it called [svelte-navigator](https://github.com/mefechoel/svelte-navigator) containing some additional functionality.
If you need hash-based routing on the client side, check out [svelte-spa-router](https://github.com/ItalyPaleAle/svelte-spa-router) or [abstract-state-router](https://github.com/TehShrike/abstract-state-router/).
[Routify](https://routify.dev) is another filesystem-based router, similar to SvelteKit's router. Version 3 supports Svelte's native SSR.
You can see a [community-maintained list of routers on sveltesociety.dev](https://sveltesociety.dev/components#routers).

View File

@ -5,6 +5,5 @@ export const CONTENT_BASE_PATHS = {
BLOG: `${CONTENT_BASE}/blog`,
TUTORIAL: `${CONTENT_BASE}/tutorial`,
DOCS: `${CONTENT_BASE}/docs`,
FAQ: `${CONTENT_BASE}/faq`,
EXAMPLES: `${CONTENT_BASE}/examples`
};

View File

@ -109,6 +109,7 @@ function get_sections(markdown) {
title: removeMarkdown(
escape(transform(match[1], { paragraph: (txt) => txt }))
.replace(/<\/?code>/g, '')
.replace(/&#39;/g, "'")
.replace(/&quot;/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')

View File

@ -1,32 +0,0 @@
// @ts-check
import { modules } from '$lib/generated/type-info';
import fs from 'node:fs';
import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { extract_frontmatter } from '../markdown/index.js';
import { render_markdown } from '../markdown/renderer.js';
/** @param {import('./types').FAQData} faq_data */
export async function get_parsed_faq(faq_data) {
const str = faq_data.map(({ content, title }) => `## ${title}\n\n${content}`).join('\n\n');
return await render_markdown('faq', str, { modules });
}
/**
* @returns {import('./types').FAQData}
*/
export function get_faq_data(base = CONTENT_BASE_PATHS.FAQ) {
const faqs = [];
for (const file of fs.readdirSync(base)) {
const { metadata, body } = extract_frontmatter(fs.readFileSync(`${base}/${file}`, 'utf-8'));
faqs.push({
title: metadata.question, // Initialise with empty
slug: file.split('-').slice(1).join('-').replace('.md', ''),
content: body
});
}
return faqs;
}

View File

@ -1,5 +0,0 @@
export type FAQData = {
title: string;
slug: string;
content: string;
}[];

View File

@ -33,7 +33,6 @@
<NavItem href="/examples">Examples</NavItem>
<NavItem href="/repl">REPL</NavItem>
<NavItem href="/blog">Blog</NavItem>
<NavItem href="/faq">FAQ</NavItem>
<Separator />

View File

@ -74,7 +74,6 @@
<a href="/docs">Docs</a>
<a href="/examples">Examples</a>
<a href="/blog">Blog</a>
<a href="/faq">FAQ</a>
<a href="https://opencollective.com/svelte">Open Collective</a>
</footer>

View File

@ -13,91 +13,76 @@ import { CONTENT_BASE } from '../../constants.js';
const base = CONTENT_BASE;
const categories = [
{
slug: 'docs',
label: null,
/** @param {string[]} parts */
href: (parts) => (parts.length > 1 ? `/docs/${parts[0]}#${parts.at(-1)}` : `/docs/${parts[0]}`)
},
{
slug: 'faq',
label: 'FAQ',
/** @param {string[]} parts */
href: (parts) => `/faq#${parts.join('-')}`
}
];
/** @param {string[]} parts */
function get_href(parts) {
return parts.length > 1 ? `/docs/${parts[0]}#${parts.at(-1)}` : `/docs/${parts[0]}`;
}
export function content() {
/** @type {import('@sveltejs/site-kit/search').Block[]} */
const blocks = [];
for (const category of categories) {
const breadcrumbs = category.label ? [category.label] : [];
const breadcrumbs = [];
for (const file of glob('**/*.md', { cwd: `${base}/${category.slug}` })) {
const basename = path.basename(file);
const match = /\d{2}-(.+)\.md/.exec(basename);
if (!match) continue;
for (const file of glob('**/*.md', { cwd: `${base}/docs` })) {
const basename = path.basename(file);
const match = /\d{2}-(.+)\.md/.exec(basename);
if (!match) continue;
const slug = match[1];
const slug = match[1];
const filepath = `${base}/${category.slug}/${file}`;
// const markdown = replace_placeholders(fs.readFileSync(filepath, 'utf-8'));
const markdown = replace_export_type_placeholders(
fs.readFileSync(filepath, 'utf-8'),
modules
);
const filepath = `${base}/docs/${file}`;
// const markdown = replace_placeholders(fs.readFileSync(filepath, 'utf-8'));
const markdown = replace_export_type_placeholders(fs.readFileSync(filepath, 'utf-8'), modules);
const { body, metadata } = extract_frontmatter(markdown);
const { body, metadata } = extract_frontmatter(markdown);
const sections = body.trim().split(/^## /m);
const intro = sections.shift().trim();
const rank = +metadata.rank || undefined;
const sections = body.trim().split(/^## /m);
const intro = sections.shift().trim();
const rank = +metadata.rank || undefined;
blocks.push({
breadcrumbs: [...breadcrumbs, removeMarkdown(remove_TYPE(metadata.title) ?? '')],
href: get_href([slug]),
content: plaintext(intro),
rank
});
for (const section of sections) {
const lines = section.split('\n');
const h2 = lines.shift();
const content = lines.join('\n');
const subsections = content.trim().split('## ');
const intro = subsections.shift().trim();
blocks.push({
breadcrumbs: [...breadcrumbs, removeMarkdown(remove_TYPE(metadata.title) ?? '')],
href: category.href([slug]),
breadcrumbs: [
...breadcrumbs,
removeMarkdown(remove_TYPE(metadata.title)),
remove_TYPE(removeMarkdown(h2))
],
href: get_href([slug, normalizeSlugify(h2)]),
content: plaintext(intro),
rank
});
for (const section of sections) {
const lines = section.split('\n');
const h2 = lines.shift();
const content = lines.join('\n');
const subsections = content.trim().split('## ');
const intro = subsections.shift().trim();
for (const subsection of subsections) {
const lines = subsection.split('\n');
const h3 = lines.shift();
blocks.push({
breadcrumbs: [
...breadcrumbs,
removeMarkdown(remove_TYPE(metadata.title)),
remove_TYPE(removeMarkdown(h2))
removeMarkdown(remove_TYPE(h2)),
removeMarkdown(remove_TYPE(h3))
],
href: category.href([slug, normalizeSlugify(h2)]),
content: plaintext(intro),
href: get_href([slug, normalizeSlugify(h2), normalizeSlugify(h3)]),
content: plaintext(lines.join('\n').trim()),
rank
});
for (const subsection of subsections) {
const lines = subsection.split('\n');
const h3 = lines.shift();
blocks.push({
breadcrumbs: [
...breadcrumbs,
removeMarkdown(remove_TYPE(metadata.title)),
removeMarkdown(remove_TYPE(h2)),
removeMarkdown(remove_TYPE(h3))
],
href: category.href([slug, normalizeSlugify(h2), normalizeSlugify(h3)]),
content: plaintext(lines.join('\n').trim()),
rank
});
}
}
}
}

View File

@ -1,7 +0,0 @@
import { get_parsed_faq, get_faq_data } from '$lib/server/faq/index.js';
export const prerender = true;
export async function load() {
return { faq: get_parsed_faq(get_faq_data()) };
}

View File

@ -1,35 +0,0 @@
<script>
import * as hovers from '$lib/utils/hovers.js';
export let data;
hovers.setup();
</script>
<svelte:head>
<title>FAQ • Svelte</title>
<meta name="twitter:title" content="Svelte FAQ" />
<meta name="twitter:description" content="Frequently asked questions about Svelte" />
<meta name="description" content="Frequently asked questions about Svelte" />
</svelte:head>
<div class="faqs stretch">
<h1>Frequently Asked Questions</h1>
<article class="text">
{@html data.faq}
</article>
</div>
<style>
.faqs {
grid-template-columns: 1fr 1fr;
grid-gap: 1em;
min-height: calc(100vh - var(--sk-nav-height));
padding: var(--sk-page-padding-top) var(--sk-page-padding-side) 6rem var(--sk-page-padding-side);
max-width: var(--sk-page-main-width);
margin: 0 auto;
tab-size: 2;
}
</style>

View File

@ -0,0 +1,7 @@
import { redirect } from '@sveltejs/kit';
export const prerender = true;
export function GET() {
throw redirect(308, '/docs/faq');
}