Skip to content

Commit 860e516

Browse files
patshaughnessyheckj
authored andcommitted
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.
1 parent 41f7b99 commit 860e516

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Sources/docc/DocCDocumentation.docc/adding-code-snippets-to-your-content.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,24 @@ let package = Package(
4444

4545
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`.
4646
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+
4865

4966
The following example illustrates a code example in the file `Snippets/example-snippet.swift`:
5067

@@ -85,6 +102,12 @@ The `path` argument has three parts:
85102

86103
3. The name of your snippet file without the `.swift` extension
87104

105+
In the example package above, the `YourProject.md` file could reference `example-snippet.swift` like this:
106+
107+
```markdown
108+
@Snippet(path: "YourProject/Snippets/example-snippet")
109+
```
110+
88111
Without any additional annotations in your snippet, Docc includes the entirety of your code example as the snippet.
89112
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.
90113
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
165188

166189
### Documenting the code in your snippet
167190

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.
169192
DocC will attempt to reference symbols from within these comments just like any other documentation content.
170193
You can reference symbols from your API, which DocC converts into hyperlinks to that symbol when displaying the content.
171194

0 commit comments

Comments
 (0)