### Install Prettier and Svelte Plugin
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/README.md
Installs `prettier-plugin-svelte` and `prettier` as development dependencies using npm.
```bash
npm i --save-dev prettier-plugin-svelte prettier
```
--------------------------------
### JavaScript Constant Declarations
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-none.html
Example of declaring multiple constant variables on a single line in JavaScript.
```JavaScript
const name = "world"; const name1 = "world";
```
--------------------------------
### Svelte Component Text Interpolation
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-markup-options-scripts.html
This example shows a basic Svelte component template with text and a variable interpolation. The `{name}` syntax is used to display the value of the `name` variable within the component's output.
```svelte
Hello {name}!
```
--------------------------------
### JavaScript JSON Stringify Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/pre-mustache/output.html
Demonstrates the use of JSON.stringify to pretty-print a JavaScript object, often used for debugging or serialization.
```javascript
JSON.stringify({ foo: "bar" }, null, 2)
```
--------------------------------
### JavaScript Variable Declaration Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/script-only-extra-whitespace/output.html
A simple JavaScript code snippet demonstrating a constant variable declaration. This example illustrates basic syntax that might be found within a Svelte project context.
```JavaScript
const name = "world";
```
--------------------------------
### Prettier Configuration Example for Svelte Plugin
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/README.md
This JSON snippet provides an example `.prettierrc` file demonstrating how to configure various `prettier-plugin-svelte` options. It includes settings for `svelteSortOrder`, `svelteBracketNewLine`, `svelteAllowShorthand`, and `svelteIndentScriptAndStyle` to customize Svelte file formatting.
```JSON
{
"svelteSortOrder": "options-styles-scripts-markup",
"svelteBracketNewLine": false,
"svelteAllowShorthand": false,
"svelteIndentScriptAndStyle": false
}
```
--------------------------------
### Importing Svelte Components
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/script-style-script-snipping.html
Demonstrates how to import a Svelte component from a relative path. This is a common pattern for structuring Svelte applications, allowing modularity and reusability of UI elements.
```JavaScript
import MyFunction from "./this_is_a_long_import_path/this_is_a_long_import_name.svelte"; // a long, and useless comment
```
--------------------------------
### JavaScript Variable Declaration Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/single-attribute-per-line-bracket-no-new-line.html
A basic JavaScript example demonstrating constant variable declaration. This snippet initializes a constant variable named 'hi' with an empty string. It serves as a minimal illustration of JavaScript syntax.
```JavaScript
const hi = ""; const hi = "";
```
--------------------------------
### Apply Basic CSS Styling
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-options-styles-scripts-markup.html
This snippet demonstrates how to apply a basic CSS rule to an HTML 'h1' element, setting its color to red. This is a fundamental styling technique.
```CSS
h1 { color: red; }
```
--------------------------------
### Svelte Allow Shorthand Example (Enabled)
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/README.md
Demonstrates the Svelte component attribute shorthand when the `svelteAllowShorthand` option is set to `true`. This results in a more concise syntax for attributes.
```html
```
--------------------------------
### Svelte Object Literal Expression
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/pre-mustache/input.html
An example of a simple object literal within a Svelte expression block, illustrating how Prettier formats short inline objects.
```Svelte
{{a: true}}
```
--------------------------------
### Basic CSS Styling Rule
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/style.html
This snippet defines a simple CSS rule that sets the text color of `div` elements to red. It serves as a fundamental example of CSS syntax for applying styles.
```CSS
div { color: red; }
```
--------------------------------
### Basic H1 Styling in CSS
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-markup-scripts-options.html
This CSS rule applies a red color to all
HTML elements. It's a fundamental example of how to style text using CSS.
```css
h1 { color: red; }
```
--------------------------------
### Basic JavaScript Statement
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/script-tag-generics.html
A simple JavaScript statement demonstrating basic syntax. This snippet illustrates how a single expression might be formatted.
```JavaScript
hello;
```
--------------------------------
### JavaScript Constant Variable Declaration
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-markup-options-scripts-styles.html
This JavaScript snippet declares a constant variable named 'name' and assigns it the string value 'world'. It's a fundamental example of variable initialization.
```JavaScript
const name = "world";
```
--------------------------------
### Svelte Template String
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-none.html
A simple Svelte template string demonstrating basic text and variable interpolation.
```Svelte
Hello {name}!
```
--------------------------------
### JavaScript Constant Variable Declaration
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/script-only.html
This snippet illustrates the declaration of a constant variable `name` initialized with the string value `"world"` in JavaScript. It's a fundamental syntax example.
```JavaScript
const name = "world";
```
--------------------------------
### Svelte Template String Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-options-markup-scripts.html
An example of a Svelte template string demonstrating variable interpolation. The '{name}' placeholder will be dynamically replaced by a JavaScript variable named 'name' defined in the component's script section.
```svelte
Hello {name}!
```
--------------------------------
### Styling HTML Elements with CSS
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-scripts-markup-options.html
Demonstrates a basic CSS rule to set the color of an H1 element to red. This snippet illustrates fundamental styling within a web project.
```CSS
h1 { color: red; }
```
--------------------------------
### Svelte Key Block with Store Subscription
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/key-block.html
Illustrates using a Svelte store subscription ($store) as the key. This is useful for re-rendering a component whenever the value of the subscribed store changes, ensuring the component reacts to global state updates.
```Svelte
{#key $store}
hello
{/key}
```
--------------------------------
### Svelte Bracket New Line Formatting Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/README.md
This example demonstrates the effect of the `svelteBracketNewLine` option on HTML/Svelte code formatting. It shows the code before formatting and then after formatting with the option set to `true` and `false`, highlighting how multiline elements are affected.
```HTML
foo
bar
content
```
```HTML
foo
bar
content
```
```HTML
foo
bar
content
```
--------------------------------
### Svelte Await Block Variations for Async Handling
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/svelte-await-block-break/output.html
These snippets showcase different ways to implement the `{#await}` block in Svelte. They illustrate the standard structure with separate `{:then}` and `{:catch}` blocks, a variation with multiple content lines, and a shorthand syntax where the `then` clause is included directly in the opening `{#await}` tag.
```Svelte
{#await bla}
loooooooooooooooooooooong
{:then blubb}
loooooooooooooooooooooong
{:catch}
loooooooooooooooooooooong
{/await}
```
```Svelte
{#await bla}
loooooooooooooooooooooong
loooooooooooooooooooooong
{:then blubb}
loooooooooooooooooooooong
loooooooooooooooooooooong
{:catch}
loooooooooooooooooooooong
loooooooooooooooooooooong
{/await}
```
```Svelte
{#await bla then blubb}
loooooooooooooooooooooong
{:catch}
loooooooooooooooooooooong
{/await}
```
--------------------------------
### JavaScript Variable Declaration Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/single-attribute-per-line/output.html
This snippet demonstrates a basic variable declaration in JavaScript. It initializes a constant variable named 'hi' with an empty string. The declaration is repeated twice, which might indicate a copy-paste error or a simplified example.
```JavaScript
const hi = ""; const hi = "";
```
--------------------------------
### Svelte Each Block Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/each-block.html
This Svelte code snippet demonstrates how to iterate over a list of items using the `{#each}` block. It takes an array named `animals` and displays each `animal` within the template.
```Svelte
{#each animals as animal}
{animal}
{/each}
```
--------------------------------
### Svelte Key Block with Simple Variable
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/key-block.html
Demonstrates how to use a simple variable as the key for a {#key} block. When 'value' changes, the content inside this block will be re-rendered, effectively resetting its state.
```Svelte
{#key value}
hello
hello
{/key}
```
--------------------------------
### Basic Svelte Component Structure
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-scripts-options-styles-markup.html
This snippet illustrates the fundamental parts of a Svelte component, including JavaScript for reactivity, CSS for styling, and HTML-like template syntax for rendering. It defines a variable 'name', applies a red color to 'h1' elements, and displays a greeting using the defined variable.
```Svelte
Hello {name}!
```
--------------------------------
### Svelte Component Styling
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-options-markup-styles-scripts.html
A basic CSS rule defining the color for h1 elements, typically used within the
```
--------------------------------
### Svelte Each Block with Const Declaration
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/const/output.html
Demonstrates the use of an #each block for iterating over an array, combined with a @const declaration to assign a new variable within the loop's scope.
```Svelte
{#each \[1, 2\] as foo} {@const bar = foo} {foo}{bar} {/each}
```
--------------------------------
### Basic CSS Styling for Iframes
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/script-style-script-snipping.html
Illustrates a simple CSS rule to set the width of an iframe element to 100% of its parent container. This is a fundamental CSS property for ensuring responsive layouts and proper display of embedded content.
```CSS
iframe { width: 100%; }
```
--------------------------------
### Declaring a Constant Variable in JavaScript
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-scripts-markup-options.html
Illustrates the declaration of a constant string variable named 'name' with the value 'world' using JavaScript. This is a common pattern for defining static data.
```JavaScript
const name = "world";
```
--------------------------------
### Svelte Conditional Block Example
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/if-inline.html
Demonstrates a basic conditional block in Svelte using the {#if} directive. This syntax allows rendering content based on a boolean expression.
```Svelte
{#if foo}bar{/if}
```
--------------------------------
### Svelte HTML Templating
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-markup-styles-scripts-options.html
Demonstrates basic HTML markup within a Svelte component, including how to display reactive variables using curly braces.
```HTML
Hello {name}!
```
--------------------------------
### Svelte Await Block for Asynchronous Operations
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/await-block-with-only-pending.html
This Svelte template snippet illustrates how to use the `{#await}` block to manage asynchronous operations. It displays 'loading...' while `thePromise` is pending and will render its resolved content once the promise settles. This pattern is essential for handling data fetching or other async tasks in Svelte components.
```Svelte
{#await thePromise}
loading...
{/await}
```
--------------------------------
### Declare a JavaScript Constant Variable
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/formatting/samples/trim-whitespace-after-style/output.html
This snippet demonstrates the declaration of a constant variable in JavaScript, assigning it a string value. This is a fundamental syntax element in modern JavaScript.
```JavaScript
const name = "world";
```
--------------------------------
### Basic JavaScript Console Log
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/single-quote.html
A simple JavaScript example showing the use of `console.log()` to print a string to the console. This function is widely used for debugging and displaying information during development.
```JavaScript
console.log('hi')
```
--------------------------------
### CSS Style Rule for H1 Element
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-markup-options-scripts-styles.html
A basic CSS rule that targets the H1 HTML element and sets its text color to red. This demonstrates a simple inline style definition.
```CSS
h1 { color: red; }
```
--------------------------------
### Iterating over key-value pairs in Svelte
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/each-block-destructured.html
Demonstrates how to iterate over an array of arrays (or a Map-like structure) in Svelte, destructuring each item into `key` and `value` variables. This is useful for displaying data where both an identifier and its corresponding value are needed.
```Svelte
{#each animals as \[key, value\]}
{key}: {value}
{/each}
```
--------------------------------
### Render List with Svelte Each Block
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/each-inline.html
This snippet illustrates a fundamental Svelte templating construct: the `{#each}` block. It iterates over an array named `animals`, making each `animal` available within the block to be rendered. This is commonly used for displaying dynamic lists or collections of data.
```Svelte
{#each animals as animal}{animal}{/each}
```
--------------------------------
### Svelte Component Text Interpolation
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-styles-markup-scripts-options.html
This snippet shows a basic Svelte template where the {name} variable is interpolated into the text. This is a common pattern for displaying dynamic data in Svelte components.
```svelte
Hello {name}!
```
--------------------------------
### JavaScript Variable Declaration and Global Assignment
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/svelte-head-script-content.html
This snippet declares a local variable `foo` and initializes it with the string value 'bar'. Subsequently, it assigns the value of this local variable to `window.foo`, making it a globally accessible property in a browser environment. This pattern is often used for debugging or exposing internal state.
```JavaScript
let foo = "bar"; window.foo = foo;
```
--------------------------------
### Svelte Template: Displaying Variable
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-markup-scripts-options-styles.html
This Svelte template snippet demonstrates how to embed a JavaScript variable's value directly into the HTML output using curly braces `{}`. This is a core feature for dynamic content in Svelte components.
```Svelte
Hello {name}!
```
--------------------------------
### Svelte Await Block for Promise Handling
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/await-block-with-catch-without-pending.html
This Svelte template snippet demonstrates how to use the {#await} block to elegantly handle promises. It shows content while the promise is pending, displays the resolved value upon success, and catches any errors, providing a user-friendly message.
```Svelte
{#await thePromise then theValue}
the value is {theValue}
{:catch}
oh no! {theError.message}
{/await}
```
--------------------------------
### Svelte Conditional Rendering
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/if-inline-elseif-else.html
This snippet illustrates how to implement conditional logic in Svelte templates using the {#if} {:else if} {:else} {/if} block structure. It renders different content based on the truthiness of 'foo' or 'baz'.
```Svelte
{#if foo}bar{:else if baz}qux{:else}foobar{/if}
```
--------------------------------
### Svelte Await Block for Asynchronous Operations
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/nested-destructuring.html
This Svelte template snippet illustrates the use of the `{#await}` block to manage asynchronous operations. It shows how to display a loading state while a promise is pending, render data upon successful resolution using `{:then}`, and catch and display errors using `{:catch}`. It expects `thePromise` to be a Promise and handles nested data structures for success and error.
```Svelte
{#await thePromise}
loading...
{:then { data: [theValue] }}
the value is {theValue}
{:catch { data: [{ data: [theError] }] }}
oh no! {theError.message}
{/await}
```
--------------------------------
### Basic CSS Styling for Div Elements
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/style-inside-element.html
This CSS snippet applies a red color to all 'div' HTML elements. It's a fundamental example of how to style elements using CSS.
```css
div { color: red; }
```
--------------------------------
### Svelte Await Block with Const Declaration
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/const.html
Illustrates handling asynchronous operations with `{#await}` in Svelte. It captures the resolved `result` of a promise and declares a new constant `bar` from it using `{@const}` within the block.
```Svelte
{#await aPromise then result} {@const bar = result} {/await}
```
--------------------------------
### JavaScript: Declaring a Constant Variable
Source: https://github.com/sveltejs/prettier-plugin-svelte/blob/master/test/printer/samples/sort-order-markup-scripts-options-styles.html
This JavaScript snippet, typically found within the