Swift-DocC sidebar

Just a quick update here: The initial support for the Navigator was merged to main this afternoon but will remain behind a feature flag as we continue iterating on it and discussing in this thread.

Here are some instructions if you're interested in trying it out locally with your own content:

  1. Follow the instructions here to build and run the version of docc on main.

  2. Clone the pre-built version of Swift-DocC-Render from the swift-docc-render-artifact repo with:

    git clone https://github.com/apple/swift-docc-render-artifact.git
    
  3. Modify the theme-settings.json at dist/theme-settings.json so that the features.docs.navigator.enable property is set to true.

  4. Set the DOCC_HTML_DIR environment variable to the dist directory at the root of your swift-docc-render-artifact repository.

    export DOCC_HTML_DIR=/path/to/swift-docc-render-artifact/dist
    
  5. Generate a compatible DocC archive by passing the --enable-experimental-json-index flag to docc convert.

    docc convert <path-to-docc-catalog> --enable-experimental-json-index
    

Alternatively, the Swift Argument Parser demo is still up on my fork if you're looking for an easier way to try things out here.

1 Like

I totally agree! Fuzzy search here would be great. We don't expect the initial release of the sidebar to have it, but I've filed [SR-15900] Add support for fuzzy search in the DocC-Render navigator · Issue #278 · swiftlang/swift-docc-render · GitHub to track this as an enhancement.

3 Likes

@rauhul Agreed! This really is great feedback we should explore. I've filed [SR-15902] DocC-Render footer causes navigator sidebar to scroll · Issue #277 · apple/swift-docc-render · GitHub to track this. Thank you for bringing it up here.


Thanks @Joseph_Heck! This is really great feedback from you and others.

We're using the navigator fragments emitted by the Swift compiler's symbol graph tool directly here, but per the symbol graph spec, the intention of the navigator fragments is to be optimized for scenarios with limited horizontal space so I think this is essentially a bug.

We actually have some inconsistencies here, for example, we don't include identifiers like struct and class but we do include func . I agree that the kind icons already convey this information so optimizing the experience to just include the symbol's name and (if applicable) arguments is likely what makes sense here.

I've filed [SR-15901] Swift symbol graph navigator fragments should be better optimized for limited horizontal space · Issue #180 · apple/swift-docc · GitHub to track this. CC: @QuietMisdreavus :smiley:

1 Like

We actually have some inconsistencies here, for example, we don't include identifiers like struct and class but we do include func . I agree that the kind icons already convey this information so optimizing the experience to just include the symbol's name and (if applicable) arguments is likely what makes sense here.

On this specific point: The rational for this choice was that some symbol kinds like structures and classes have specific icons, but many other symbol kinds share the same icon. Some examples (not sure this is exhaustive):

  • Functions (mutating or otherwise) and static functions use the icon [M]
  • Mutable and immutable properties both use [P]
  • static let members and and global variables (whether they're let or var) use [V]
  • Enumerations and enumeration cases both use [E]. Interestingly, enumerations don't get an enum prefix but cases do get a case prefix (presumably because it's obvious in context; cases are always children)

I don't have an opinion on what we should do—if anything—to address those cases.

1 Like

I agree that hiding the sidebar should be optional, we should probably consider that improvement in the future across all viewports and not only for small/medium viewport sizes.

1 Like

The field only allows for filtering the content—placing it at the bottom provides more clarity for the functionality it provides.

1 Like

I'm excited to announce that the Swift-DocC sidebar has been enabled by default in Swift-DocC-Render with:

The sidebar will only be available for DocC archives produced by the latest version of DocC that includes the necessary index file.

I've updated the ArgumentParser demo with the latest changes: https://ethan-kusters.github.io/swift-argument-parser/documentation/argumentparser/.

If you'd like to try out the sidebar with your own Swift Package, you can do the following:

Testing instructions:

Note that I'm going to use the Swift-DocC Plugin to simplify the instructions here but it's not actually required for this functionality. It just makes testing prerelease versions of the docc CLI a little more user-friendly.

  1. Build the latest version of Swift-DocC:

    git clone https://github.com/apple/swift-docc.git
    cd swift-docc
    swift build
    

  1. Set the DOCC_EXEC environment variable to your locally built copy of docc:
    export DOCC_EXEC=`pwd`/.build/debug/docc
    

  1. Clone the latest pre-built version of Swift-DocC-Render:

    git clone https://github.com/apple/swift-docc-render-artifact.git
    cd swift-docc-render-artifact
    

  1. Set the DOCC_HTML_DIR environment variable to your local copy of Swift-DocC-Render:

    export DOCC_HTML_DIR=`pwd`/dist
    

  1. Adopt the Swift-DocC-Plugin in your package by following the instructions here.

    .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
    

  1. Change directory to your package and run the preview-documentation command for your target:
    cd <path-to-your-package>
    swift package --disable-sandbox preview-documentation --target <name-of-package-target>
    

  1. That's it!

If you encounter any issues while trying things out, a bug report on bugs.swift.org (and soon GitHub Issues) would be really appreciated.

- Ethan

5 Likes

I've been generating docs with the commands

env DOCC_JSON_PRETTYPRINT="YES" \
swift package \
--allow-writing-to-directory "\(outputPath)" \
generate-documentation \
--disable-indexing \
--transform-for-static-hosting \
--output-path "\(outputPath)" \
--hosting-base-path "\(hostingBasePath)" \
--target "\(target)"

for macOS and

xcodebuild \
-derivedDataPath "$PWD/.derivedData" \
docbuild \
-scheme "\(target)" \
-destination 'generic/platform=\(platform.rawValue)'

xcrun docc process-archive transform-for-static-hosting \
$(find "$PWD/.derivedData" -type d -name "*.doccarchive") \
--output-path "\(outputPath)" \
--hosting-base-path "\(hostingBasePath)"

for iOS using the DocC plugin in a package manifest, with Swift 5.6. (This is in the context of generating package documentation in the Swift Package Index.)

Following this discussion here I was wondering if there's a way to get the new sidebar with this setup or if we need to wait for some SPM/DocC release to get it automatically?

I've tried removing the parameter --disable-indexing but that doesn't change anything. The documentation of that parameter

By default, the Swift-DocC plugin will generate an index of content that is useful for IDE’s, like Xcode, to render a navigator of all included documentation in the archive. Since this index isn’t relevant when hosting online, you can pass the --disable-indexing flag when generating documentation thats intended for an online host.

led me to believe this is something we'll want to set. But reading the discussion above about --index becoming the default I'm now wondering if this setting would prevent the sidebar from appearing even if we were using the proper DocC version that supported it.

Is this a parameter we should be setting for generated and hosted documentation?

2 Likes

Hi @finestructure!

The sidebar is included in Swift 5.7 Development toolchains on Swift.org here. In general both of the command-line invocations you provided look correct to me (it is correct to continue passing --disable-indexing even when you want the sidebar to be available on the web) and I'd expect them to "just work" once you update to a 5.7 toolchain.

If you'd like to continue using Swift 5.6 for building the actual source code, but use the Swift 5.7 version of Swift-DocC, you should be able to by passing a custom DocC executable path to your SwiftPM or xcodebuild invocation.

Assuming you have a recent 5.7 development toolchain installed but a version of Xcode with a 5.6 toolchain selected:

env DOCC_EXEC="/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/docc" \
env DOCC_JSON_PRETTYPRINT="YES" \
swift package \
    --allow-writing-to-directory "\(outputPath)" \
    generate-documentation \
    --disable-indexing \
    --transform-for-static-hosting \ # <-- Note: This is optional with Swift 5.7 since the transformation is enabled by default.
    --output-path "\(outputPath)" \
    --hosting-base-path "\(hostingBasePath)" \
    --target "\(target)"

And for iOS, you should be able to combine your two commands into one via the EXTRA_DOCC_FLAGS build setting.

xcodebuild \
    -derivedDataPath "$PWD/.derivedData" \
    docbuild \
    -scheme "\(target)" \
    -destination 'generic/platform=\(platform.rawValue)' \
    EXTRA_DOCC_FLAGS="--hosting-base-path '\(hostingBasePath)' --transform-for-static-hosting"
    DOCC_EXEC="/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/docc" \

cp -r $(find "$PWD/.derivedData" -type d -name "*.doccarchive") "\(outputPath)"
4 Likes

That's great to hear, thanks for confirming, Ethan!

1 Like

Looks like this enabled for all Apple docs. Looks pretty nice. I did notice there's no control to hide it on most of these pages, even for things like release notes where the sidebar just shows older note. Can we get that control?

2 Likes

This is something that was brought up earlier in this thread and a feedback that we've seen come up few times already. I agree, this is definitely a feature we should consider making available for all screen size.

2 Likes

@Jon_Shier if you have a chance, an enhancement request on GitHub Issues for this would be great.

2 Likes

Has this style been made the default in Xcode 14? I generated the docs, only to be greeted with this pleasant surprise :)

Yes! The sidebar is included by default in Xcode 14 and Swift 5.7 toolchains when publishing to the web.

3 Likes

Is there a way to filter search results with symbol type? i.e. functions, instance methods, initializers etc. This would help narrow down the results where I know the type of result I am looking for.

1 Like