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
Add an ASCII diagram of an example Swift package directory structure, to
make it clear where the snippets go, and also where the documentation
catalog goes.
Also show a concrete example of how to reference a snippet.
Fix a typo.
Copy file name to clipboardExpand all lines: Sources/docc/DocCDocumentation.docc/adding-code-snippets-to-your-content.md
+25-2Lines changed: 25 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,24 @@ let package = Package(
44
44
45
45
Swift Package Manager expects to find your code examples in the directory `Snippets` at the top of your project, parallel to the file `Package.swift` and the directory `Sources`.
46
46
At the root of your project, create the directory `Snippets`.
47
-
Within the Snippets directory, create a file with your code snippet.
47
+
Within the `Snippets` directory, create a file with your code snippet.
48
+
49
+
Your Swift package directory structure should resemble this:
50
+
51
+
```
52
+
YourProject
53
+
├─ Package.swift
54
+
├─ Snippets/
55
+
│ ├─ example-snippet.swift
56
+
├─ Sources/
57
+
│ ├─ YourProject.docc/
58
+
│ │ ├─ YourProject.md
59
+
│ ├─ main.swift
60
+
etc...
61
+
```
62
+
63
+
> Note: Unlike `Snippets`, your Swift package's documentation catalog should be located as a subdirectory of `Sources`, where all of your other Swift source code files are located. For example, see `YourProject.docc` above.
64
+
48
65
49
66
The following example illustrates a code example in the file `Snippets/example-snippet.swift`:
50
67
@@ -85,6 +102,12 @@ The `path` argument has three parts:
85
102
86
103
3. The name of your snippet file without the `.swift` extension
87
104
105
+
In the example package above, the `YourProject.md` file could reference `example-snippet.swift` like this:
Without any additional annotations in your snippet, Docc includes the entirety of your code example as the snippet.
89
112
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.
90
113
These comments act as a toggle to hide or show content from the snippet.
@@ -165,7 +188,7 @@ Extending the earlier snippet example, the slice `setup` would be referenced wit
165
188
166
189
### Documenting the code in your snippet
167
190
168
-
DocC parses contiguous comments within your the code of a snippet as markdown to annotate your code when embedded in documentation.
191
+
DocC parses contiguous comments within the code of a snippet as markdown to annotate your code when embedded in documentation.
169
192
DocC will attempt to reference symbols from within these comments just like any other documentation content.
170
193
You can reference symbols from your API, which DocC converts into hyperlinks to that symbol when displaying the content.
0 commit comments