|
| 1 | +# Adding Code Snippets to your Content |
| 2 | + |
| 3 | +Create and include code snippets to illustrate and provide examples of how to use your API. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +...tbd... |
| 8 | +- Describe the problem and summarize the developer action. |
| 9 | +- Explain why the problem is relevant and provide context for the task. Don't simply repeat the abstract; expand on it. |
| 10 | +- Keep the Overview to one or two paragraphs. |
| 11 | +- For very short articles that consist of just a couple of paragraphs, all of the content can be in the Overview. |
| 12 | + |
| 13 | +...tbd... |
| 14 | + |
| 15 | +### Create a code snippet |
| 16 | + |
| 17 | +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`. |
| 18 | +At the root of your project, create the directory `Snippets`. |
| 19 | +Within the Snippets directory, create a file with your code snippet. |
| 20 | + |
| 21 | +The following example illustrates a code example in the file `Snippets/example-snippet.swift`: |
| 22 | + |
| 23 | +```swift |
| 24 | +import Foundation |
| 25 | + |
| 26 | +print("Hello") |
| 27 | +``` |
| 28 | + |
| 29 | +Within your snippet, you can import your local module, as well as any module that your package depends on. |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +### Run the snippet |
| 34 | + |
| 35 | +Each code example file you create becomes it's own module. |
| 36 | +The name of the code example file you create is the name of the module that Swift creates. |
| 37 | +Use the `swift run` command in a terminal to compile and run the module to verify it compiles does what you expect. |
| 38 | + |
| 39 | +Run the earlier code example file named `example-snippet.swift` using the following command: |
| 40 | + |
| 41 | +```bash |
| 42 | +swift run example-snippet |
| 43 | +``` |
| 44 | + |
| 45 | +### Reference the snippet |
| 46 | + |
| 47 | +To reference your snippet in an article or within the symbol reference pages, use the `@Snippet` directive. |
| 48 | +```markdown |
| 49 | +@Snippet(path: "my-package/Snippets/example-snippet") |
| 50 | +``` |
| 51 | + |
| 52 | +The `path` argument has three parts: |
| 53 | + |
| 54 | +1. The package name as defined in `Package.swift` |
| 55 | + |
| 56 | +2. The directory path to the snippet file, starting with "Snippets". |
| 57 | + |
| 58 | +3. The name of your snippet file without the `.swift` extension |
| 59 | + |
| 60 | +Without any additional annotations in your snippet, Docc includes the entirety of your code example as the snippet. |
| 61 | +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. |
| 62 | +These comments act as a toggle to hide or show content from the snippet. |
| 63 | + |
| 64 | +```swift |
| 65 | +print("Hello") |
| 66 | + |
| 67 | +// snippet.hide |
| 68 | + |
| 69 | +print("Hidden") |
| 70 | + |
| 71 | +// snippet.show |
| 72 | + |
| 73 | +print("Shown") |
| 74 | +``` |
| 75 | + |
| 76 | +Hide segments of your snippet for things like license footers, test code, or unique setup code. |
| 77 | +Generally, it is mostly useful for things that you wouldn't want the reader to take with them as a starting point. |
| 78 | + |
| 79 | +### Preview your content |
| 80 | + |
| 81 | +Use the [swift-docc-plugin](https://github.com/swiftlang/swift-docc-plugin) to preview content that includes snippets. |
| 82 | +To run the preview, use the following command from a terminal: |
| 83 | + |
| 84 | +```bash |
| 85 | +swift package --disable-sandbox preview-documentation |
| 86 | +``` |
| 87 | + |
| 88 | +### Slice up your snippet to break it up in your content. |
| 89 | + |
| 90 | +Long snippets dropped into documentation can result in a wall of text that is harder to parse and understand. |
| 91 | +Instead, annotate non-overlapping slices in the snippet, which allows you to reference and embed the slice portion of the example code. |
| 92 | + |
| 93 | +Annotating slices in a snippet looks similiar to annotating `snippet.show` and `snippet.hide`. |
| 94 | +You define the slice's identity in the comment, and that slice continues until the next instance of `// snippet.end` appears on a new line. |
| 95 | +When selecting your identifiers, use URL-compatible path characters. |
| 96 | + |
| 97 | +For example, to start a slice with an ID of `setup`, add the following comment on a new line. |
| 98 | + |
| 99 | +```swift |
| 100 | +// snippet.setup |
| 101 | +``` |
| 102 | + |
| 103 | +Then end the `setup` slice with: |
| 104 | + |
| 105 | +```swift |
| 106 | +// snippet.end |
| 107 | +``` |
| 108 | + |
| 109 | +Adding a new slice identifier automatically terminates an earlier slice. |
| 110 | +For example, the follow code examples are effectively the same: |
| 111 | + |
| 112 | +```swift |
| 113 | +// snippet.setup |
| 114 | +var item = MyObject.init() |
| 115 | +// snippet.end |
| 116 | + |
| 117 | +// snipppet.configure |
| 118 | +item.size = 3 |
| 119 | +// snippet.end |
| 120 | +``` |
| 121 | + |
| 122 | +```swift |
| 123 | +// snippet.setup |
| 124 | +var item = MyObject.init() |
| 125 | + |
| 126 | +// snipppet.configure |
| 127 | +item.size = 3 |
| 128 | +``` |
| 129 | + |
| 130 | +Use the `@Snippet` directive with the `slice` parameter to embed that slice as sample code on your documentation. |
| 131 | +Extending the earlier snippet example, the slice `setup` would be referenced with |
| 132 | + |
| 133 | +```markdown |
| 134 | +@Snippet(path: "my-package/Snippets/example-snippet", slice: "setup") |
| 135 | +``` |
| 136 | + |
| 137 | +### Documenting the code in your snippet |
| 138 | + |
| 139 | +DocC parses contiguous comments within your the code of a snippet as markdown to annotate your code when embedded in documentation. |
| 140 | +DocC will attempt to reference symbols from within these comments just like any other documentation content. |
| 141 | +You can reference symbols from your API, which DocC converts into hyperlinks to that symbol when displaying the content. |
| 142 | + |
| 143 | +<!-- Copyright (c) 2024 Apple Inc and the Swift Project authors. All Rights Reserved. --> |
0 commit comments