Skip to content

[docs] Add basic documentation on using Snippets to DocC documentation #1166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d7e5b88
Initial work on snippets documentation
heckj Jan 21, 2025
583606e
consolidate copyright statements at bottom of markdown files and add …
heckj Mar 5, 2025
d622ac0
enables Snippet directive code for documentation, and updates the gen…
heckj Mar 9, 2025
074187f
updating with an overview
heckj May 2, 2025
c2eb970
fixing license header for check script
heckj May 7, 2025
432ae65
Update Sources/docc/DocCDocumentation.docc/DocC Documentation.md
heckj May 9, 2025
6c7ac9b
Update Sources/docc/DocCDocumentation.docc/snippets.md
heckj May 9, 2025
1f83f8e
Update Sources/docc/DocCDocumentation.docc/Reference Syntax/API Refer…
heckj May 9, 2025
5810d8c
renaming article per feedback
heckj May 9, 2025
41f7b99
update documentation to explicitly call out adding swift-docc-plugin …
heckj Jun 6, 2025
860e516
Add an ASCII diagram of an example Swift package directory structure, to
patshaughnessy Jun 13, 2025
aa6a2af
Include a full markdown example containing @Snippet
patshaughnessy Jun 13, 2025
2585c1c
applying updates based on David's feedback
heckj Jun 16, 2025
51d1cbe
co-locates snippet API ref content to source class rather than using …
heckj Jun 17, 2025
781ea81
revising note about snippets vs. docc catalogs and target linkage
heckj Jun 17, 2025
a2a4a7a
Update Sources/docc/DocCDocumentation.docc/adding-code-snippets-to-yo…
heckj Jun 23, 2025
cbe2279
removing section about markdown comments inside snippets
heckj Jun 26, 2025
27446ac
collapsing some of the demo material down per David's feedback
heckj Jul 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Sources/SwiftDocC/Semantics/Snippets/Snippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@ import Foundation
public import Markdown
import SymbolKit

/// Embeds a code example from the project's code snippets.
///
/// ```markdown
/// @Snippet(path: "my-package/Snippets/example-snippet", slice: "setup")
/// ```
///
/// Place the `Snippet` directive to embed a code example from the project's snippet directory.
/// The path that references the snippet is identified with three parts:
///
/// 1. The package name as defined in `Package.swift`
///
/// 2. The directory path to the snippet file, starting with "Snippets".
///
/// 3. The name of your snippet file without the `.swift` extension
///
/// If the snippet had slices annotated within it, an individual slice of the snippet can be referenced with the `slice` option.
/// Without the option defined, the directive embeds the entire snippet.
public final class Snippet: Semantic, AutomaticDirectiveConvertible {
public static let introducedVersion = "5.6"
public static let introducedVersion = "5.7"
public let originalMarkup: BlockDirective

/// The path components of a symbol link that would be used to resolve a reference to a snippet,
Expand All @@ -30,8 +47,6 @@ public final class Snippet: Semantic, AutomaticDirectiveConvertible {
"slice" : \Snippet._slice,
]

static var hiddenFromDocumentation = true

@available(*, deprecated, message: "Do not call directly. Required for 'AutomaticDirectiveConvertible'.")
init(originalMarkup: BlockDirective) {
self.originalMarkup = originalMarkup
Expand Down
1 change: 1 addition & 0 deletions Sources/docc/DocCDocumentation.docc/DocC Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DocC syntax — called _documentation markup_ — is a custom variant of Markdow
- <doc:writing-symbol-documentation-in-your-source-files>
- <doc:adding-supplemental-content-to-a-documentation-catalog>
- <doc:linking-to-symbols-and-other-content>
- <doc:adding-code-snippets-to-your-content>
- <doc:documenting-api-with-different-language-representations>

### Structure and Formatting
Expand Down
207 changes: 207 additions & 0 deletions Sources/docc/DocCDocumentation.docc/DocC.symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -5246,6 +5246,213 @@
"Small"
]
},
{
"accessLevel" : "public",
"availability" : [
{
"domain" : "Swift-DocC",
"introduced" : {
"major" : 5,
"minor" : 7,
"patch" : 0
}
}
],
"declarationFragments" : [
{
"kind" : "typeIdentifier",
"spelling" : "@"
},
{
"kind" : "typeIdentifier",
"spelling" : "Snippet"
},
{
"kind" : "text",
"spelling" : "("
},
{
"kind" : "identifier",
"spelling" : "path"
},
{
"kind" : "text",
"spelling" : ": "
},
{
"kind" : "typeIdentifier",
"spelling" : "String"
},
{
"kind" : "text",
"spelling" : ", "
},
{
"kind" : "identifier",
"spelling" : "slice"
},
{
"kind" : "text",
"spelling" : ": "
},
{
"kind" : "typeIdentifier",
"spelling" : "String"
},
{
"kind" : "text",
"spelling" : "?"
},
{
"kind" : "text",
"spelling" : ")"
}
],
"docComment" : {
"lines" : [
{
"text" : "Embeds a code example from the project's code snippets."
},
{
"text" : ""
},
{
"text" : "```markdown"
},
{
"text" : "@Snippet(path: \"my-package\/Snippets\/example-snippet\", slice: \"setup\")"
},
{
"text" : "```"
},
{
"text" : ""
},
{
"text" : "Place the `Snippet` directive to embed a code example from the project's snippet directory."
},
{
"text" : "The path that references the snippet is identified with three parts:"
},
{
"text" : ""
},
{
"text" : "1. The package name as defined in `Package.swift`"
},
{
"text" : ""
},
{
"text" : "2. The directory path to the snippet file, starting with \"Snippets\"."
},
{
"text" : ""
},
{
"text" : "3. The name of your snippet file without the `.swift` extension"
},
{
"text" : ""
},
{
"text" : "If the snippet had slices annotated within it, an individual slice of the snippet can be referenced with the `slice` option."
},
{
"text" : "Without the option defined, the directive embeds the entire snippet."
},
{
"text" : "- Parameters:"
},
{
"text" : " - path: The path components of a symbol link that would be used to resolve a reference to a snippet,"
},
{
"text" : " only occurring as a block directive argument."
},
{
"text" : " **(required)**"
},
{
"text" : " - slice: An optional named range to limit the lines shown."
},
{
"text" : " **(optional)**"
}
]
},
"identifier" : {
"interfaceLanguage" : "swift",
"precise" : "__docc_universal_symbol_reference_$Snippet"
},
"kind" : {
"displayName" : "Directive",
"identifier" : "class"
},
"names" : {
"navigator" : [
{
"kind" : "attribute",
"spelling" : "@"
},
{
"kind" : "identifier",
"preciseIdentifier" : "__docc_universal_symbol_reference_$Snippet",
"spelling" : "Snippet"
}
],
"subHeading" : [
{
"kind" : "identifier",
"spelling" : "@"
},
{
"kind" : "identifier",
"spelling" : "Snippet"
},
{
"kind" : "text",
"spelling" : "("
},
{
"kind" : "identifier",
"spelling" : "path"
},
{
"kind" : "text",
"spelling" : ": "
},
{
"kind" : "typeIdentifier",
"spelling" : "String"
},
{
"kind" : "text",
"spelling" : ", "
},
{
"kind" : "identifier",
"spelling" : "slice"
},
{
"kind" : "text",
"spelling" : ": "
},
{
"kind" : "typeIdentifier",
"spelling" : "String"
},
{
"kind" : "text",
"spelling" : ")"
}
],
"title" : "Snippet"
},
"pathComponents" : [
"Snippet"
]
},
{
"accessLevel" : "public",
"availability" : [
Expand Down
Loading