Skip to content

R117883810 @redirected placement #805

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

Merged

Conversation

patshaughnessy
Copy link
Contributor

Bug/issue #, if applicable: rdar://117883810

Summary

Today DocC does not parse the @Redirected directive properly if it appears before the abstract in an article or documentation extension. If the directive appears before the abstract, the abstract's contents is set to the directive only and the actual abstract is instead considered to be part of the following discussion.

For example, compiling this article:

# Getting Started with Sloths

@Redirected(from: "some/path")

Create a sloth and assign personality traits and abilities.

## Overview

Sloths are complex creatures that require careful creation and a suitable habitat. After creating a sloth, you're responsible for feeding them, providing fulfilling activities, and giving them opportunities to exercise and rest.

...would produce this page:

Screenshot 2024-01-18 at 2 55 17 PM

Note the abstract appears under "Overview", and that there are two "Overview" sections.

This fix removes any @Redirected directives from markdown content while parsing the title, abstract and discussion. Now DocC no longer considers @Redirected directives to be part of the markdown content at all, similar to how @Comment, @Metadata and @Options work. DocC saves the redirects' old path strings behind the scenes.

A related fix in this PR adds @Redirected as a child directive of @Metadata. This gives the author more flexibility to place @Redirected commands anywhere, including inside of the page's metadata which is the most logical place for it to appear.

Dependencies

None.

Testing

  1. Create a new article which contains redirects after the title, but before the abstract as shown above in the Getting Started With Sloths page.
  2. Compile and check the rendered article shows the abstract properly:

Screenshot 2024-01-18 at 2 57 36 PM

Repeat the same test, but move the @Redirected directive inside the @Metadata directives as a child. For example:

# Getting Started with Sloths

@Metadata {
    @Redirected(from: "some/path")
}

Create a sloth and assign personality traits and abilities.

## Overview

Sloths are complex creatures that require careful creation and a suitable habitat. After creating a sloth, you're responsible for feeding them, providing fulfilling activities, and giving them opportunities to exercise and rest. 

The page should render correctly again.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • [x ] Added tests
  • [x ] Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

while parsing titles, abstracts and discussions, allowing them to
appear anywhere after the title.
@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@d-ronnqvist d-ronnqvist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me.

Since it's a user facing enhancements it would be nice to document that @Redirected is supported in @Metadata as of 5.11.

I don't remember if running swift run generate-symbol-graph is sufficient to list @Redirected is a supported directive inside of @Metadata, since both directives are AutomaticDirectiveConvertible. Regardless, we should probably mention in a note that it's only available in 5.11 and later (otherwise it will look like it's been supported since 5.5 (since both directives are marked as being introduced then)

Metadata.directiveName,
Options.directiveName,
Redirect.directiveName,
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe that we should also add DeprecationSummary to this list (see rdar://79719308)—but that could fit better in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure sounds good. I can take that Radar.

Comment on lines 187 to 189
func checkSectionContent(expectedContent: String?, section: Section?) {
if let desc = section?.content.map({ $0.detachedFromParent.debugDescription() }).joined(separator: "\n") {
XCTAssertEqual(expectedContent, desc, "Found unexpected content: \n\(desc)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When adding test helpers like this it's helpful to given them file and line arguments that default to #file and #line and then also pass those values to all the XCTAssert calls. This makes it so that if the test fails then the failure will be attributed to the location that called checkSectionContent instead of the location inside of that function. It barely matters if the test helper is local to the same file, but if it's a general test helper in a separate file it can make a big difference.

Suggested change
func checkSectionContent(expectedContent: String?, section: Section?) {
if let desc = section?.content.map({ $0.detachedFromParent.debugDescription() }).joined(separator: "\n") {
XCTAssertEqual(expectedContent, desc, "Found unexpected content: \n\(desc)")
func checkSectionContent(expectedContent: String?, section: Section?, file: StaticString = #file, line: UInt = #line) {
if let desc = section?.content.map({ $0.detachedFromParent.debugDescription() }).joined(separator: "\n") {
XCTAssertEqual(expectedContent, desc, "Found unexpected content: \n\(desc)", file: file, line: line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks great suggestion 👍 Updated.

@@ -87,6 +90,7 @@ public final class Metadata: Semantic, AutomaticDirectiveConvertible {
"supportedLanguages" : \Metadata._supportedLanguages,
"_pageColor" : \Metadata.__pageColor,
"titleHeading" : \Metadata._titleHeading,
"redirects" : \Metadata._redirects,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the super nit but the misalignment bugged me

Suggested change
"redirects" : \Metadata._redirects,
"redirects" : \Metadata._redirects,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha ha no problem - updated

@@ -146,7 +154,7 @@ struct DocumentationMarkup {
// Found deprecation notice in the abstract.
deprecation = MarkupContainer(directive.children)
return
} else if directive.name == Comment.directiveName || directive.name == Metadata.directiveName || directive.name == Options.directiveName {
} else if Self.directivesRemovedFromContent.contains(directive.name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. That's more easier to expand to cover more directives in the future. 👍

@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

@patshaughnessy
Copy link
Contributor Author

Turns out that @Redirected wasn't documented at all. I've reenabled its docs, added curation so it appears under @Metadata, and also the note about version 5.11.

@patshaughnessy
Copy link
Contributor Author

@swift-ci please test

Comment on lines +21 to +24
///
/// > Note: Starting with version 5.11, @Redirected is supported as a child directive of @Metadata. In
/// previous versions, @Redirected must be used as a top level directive.
///
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this 💗

Comment on lines -34 to +38
static var hiddenFromDocumentation = true
static var hiddenFromDocumentation = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patshaughnessy I'm not following here, why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the generate-symbol-graph command to document all of DocC's directives automatically. This command generates a symbol graph file for these, which is then used when we compile and publish documentation for DocC itself. This particular setting is used here to filter out some directives. (I'm not sure why we need to filter them at all, to be honest). Since hiddenFromDocumentation was set to true previously for @Redirected, I changed it to false to include docs for this again.

Copy link
Contributor

@sofiaromorales sofiaromorales left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Pat!

@patshaughnessy patshaughnessy merged commit 21c3e25 into swiftlang:main Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants