Diff

Innehåll

Code diff can either be written manually using the diff language tag:

Input:

```diff
-export function foo(value: string): string {
+export function foo(value: string, param: number): string {
     return "bar";
 }
```

Output:

-export function foo(value: string): string {
+export function foo(value: string, param: number): string {
     return "bar";
 }

Each line in a diff should:

  • Use + to signify an added line.
  • Use - to signify a removed line.
  • Use (space) to have context lines surrounding added or removed lines.

Comparing

Diffs can also be created by comparing two examples by naming the first using name and comparing with compare. The original example may optionally be hidden with the hidden tag.

Tip

Using compare instead of handwriting diff helps with syntax highlight, ensures code is well formatted and if extracting examples ensure the source code compiles and lints as the extraction runs on the source code as written and not the diff.

Input:

```ts name=original hidden
export function foo(value: string): string {
    return "bar";
}
```

```ts compare=original
export function foo(value: string, param: number): string {
    return "bar";
}
```

Output:

-export function foo(value: string): string {
+export function foo(value: string, param: number): string {
     return "bar";
 }

Context lines

To control the number of context lines use context=N where N is an integer greater than zero. This sets the number of lines to show before and after the changed lines. Default is 3 lines.

For instance, with context=1 we see one line before and after:

 line 5
-line 6
+line 6 # change this line
 line 7
@@ -19,3 +19,3 @@
 line 19
-line 20
+line 20 # and this
 line 21

And with context=6 we see four lines instead:

 line 2
 line 3
 line 4
 line 5
-line 6
+line 6 # change this line
 line 7
 line 8
 line 9
 line 10
@@ -16,9 +16,9 @@
 line 16
 line 17
 line 18
 line 19
-line 20
+line 20 # and this
 line 21
 line 22
 line 23
 line 24

Tip

To avoid creating diffs with multiple chunks add two or more code fences each with their own part of the diff.

For instance, the above example could be split into:

 line 4
 line 5
-line 6
+line 6 # change this line
 line 7
 line 8
 line 18
 line 19
-line 20
+line 20 # and this
 line 21
 line 22

Esc för att stänga Pil upp/ner för att navigera Enter för att välja