You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/docc/DocCDocumentation.docc/adding-code-snippets-to-your-content.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,20 @@
2
2
3
3
@Metadata {
4
4
@Available("Swift", introduced: "5.7")
5
+
@TitleHeading("Article")
5
6
}
6
7
7
8
Create and include code snippets to illustrate and provide examples of how to use your API.
8
9
9
10
## Overview
10
11
12
+
11
13
DocC supports code listings in your code, as described in <doc:formatting-your-documentation-content>.
12
-
However, the content in a code listing isn't compiled or verified to work.
14
+
In addition to code listings written directly in the markup, Swift Package Manager and DocC supports compiler verified code examples called "snippets".
13
15
14
16
Swift Package Manager looks for, and builds, any code included in the `Snippets` directory for your package.
15
17
DocC supports referencing all, or parts, of those files to present as code listings.
18
+
In addition to snippets presenting your code examples, you can run snippets directly on the command line.
16
19
This allows you to verify that code examples, referenced in your documentation, continue to compile as you evolve you app or library.
17
20
18
21
### Add the Swift DocC plugin
@@ -50,13 +53,12 @@ Your Swift package directory structure should resemble this:
50
53
51
54
```
52
55
YourProject
53
-
├─ Package.swift
54
-
├─ Snippets/
55
-
│ ├─ example-snippet.swift
56
-
├─ Sources/
57
-
│ ├─ YourProject.docc/
58
-
│ │ ├─ YourProject.md
59
-
│ ├─ main.swift
56
+
├── Package.swift
57
+
├── Snippets
58
+
│ └── example-snippet.swift
59
+
├── Sources
60
+
│ └── YourProject
61
+
│ └── YourProject.swift
60
62
etc...
61
63
```
62
64
@@ -71,25 +73,24 @@ import Foundation
71
73
print("Hello")
72
74
```
73
75
74
-
Within your snippet, you can import your local module, as well as any module that your package depends on.
76
+
Your snippets can import targets defined in your local package, as well as products from its direct dependencies.
77
+
Each snippet is its own unit and can't access code from other snippet files.
75
78
76
79
Every time you build your project, the Swift Package Manager compiles any code snippets, and then fails if the build if they are unable to compile.
77
80
78
81
### Run the snippet
79
82
80
-
Each code example file you create becomes it's own module.
81
-
The name of the code example file you create is the name of the module that Swift creates.
82
-
Use the `swift run` command in a terminal to compile and run the module to verify it compiles does what you expect.
83
+
You and consumers of your library can run your snippets from the command line using `swift run snippet-name` where "snippet-name" corresponds to a file name in your Snippets directory without the ".swift" file extension.
83
84
84
85
Run the earlier code example file named `example-snippet.swift` using the following command:
85
86
86
87
```bash
87
88
swift run example-snippet
88
89
```
89
90
90
-
### Reference the snippet
91
+
### Embed the snippet
91
92
92
-
To reference your snippet in an article or within the symbol reference pages, use the `@Snippet` directive.
93
+
To embed your snippet in an article or within the symbol reference pages, use the `@Snippet` directive.
@@ -120,8 +121,7 @@ This paragraph appears before the snippet.
120
121
This paragraph appears after the snippet.
121
122
```
122
123
123
-
Without any additional annotations in your snippet, Docc includes the entirety of your code example as the snippet.
124
-
To prevent parts of your snippet file from being rendered in documentation, add comments in your code in the format `// snippet.hide` and `// snippet.show` on new lines, surrounding the content you want to hide.
124
+
If your snippet code requires setup — like imports or variable definitions — that distract from the snippet's main focus, you can add `// snippet.hide` and `// snippet.show` lines in the snippet code to exclude the lines in between from displaying in your documentation.
125
125
These comments act as a toggle to hide or show content from the snippet.
0 commit comments