Skip to content
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

Add authoring support for a @PageColor directive #525

Merged
merged 1 commit into from
Apr 7, 2023

Conversation

ethan-kusters
Copy link
Contributor

@ethan-kusters ethan-kusters commented Mar 29, 2023

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

Summary

Adds a new @PageColor metadata directive that allows for customizing the color used to represent a given page.

@PageColor gives authors control over the color used when rendering a page – initially this will affect the background color Swift-DocC-Render uses in the page's introduction (hero) section.

Example

# What's New in SlothCreator

@Metadata {
    @PageColor(blue)
}

![A sloth on a tree wearing a fedora.](sloth-fedora)

Let's check out what's new in SlothCreator!

...

Details

Declaration

@PageColor(_ color: Color)

Overview

Use the PageColor directive to provide a hint to the Swift-DocC renderer as to what color should represent this page. The renderer will use this color, depending on the context, as a foundation for the other accent colors used in the page. For example, Swift-DocC-Render will use this color as the primary background color of a page’s introduction section and adjust the colors of the icon and text in the page’s introduction accordingly.

This directive is only valid within an @Metadata directive.

@Metadata {
    @PageColor(purple)
}

Parameters

@PageColor accepts the following parameters:

  • color: An unnamed parameter that accepts one of the following pre-defined colors. Each color is context-dependent.
    • blue
    • gray
    • green
    • orange
    • purple
    • red
    • yellow

Examples

The following screenshots show how Swift-DocC Render might interpret the given color inputs initially.

Hero-yellow@2x
Hero-purple@2x
Hero-green@2x

Forums post: https://forums.swift.org/t/support-for-customizing-a-page-s-accent-color-in-swift-docc/64093

Dependencies

Testing

Add @PageColor to the @Metadata directive in a Swift-DocC article or extension file and confirm that the resulting RenderNode includes a color metadata property.

Checklist

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

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

@ethan-kusters
Copy link
Contributor Author

@swift-ci please test

@ethan-kusters ethan-kusters marked this pull request as ready for review March 31, 2023 00:49
@ethan-kusters
Copy link
Contributor Author

@swift-ci please test

*/

/// A color that associated with a documentation topic.
public struct TopicColor: Codable, Hashable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should be a protocol and standard colors can then be an implementation of the protocol?

public var color: Color

/// A context-dependent, standard color.
public enum Color: String, CaseIterable, DirectiveArgumentValueConvertible {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth defining these somewhere more central and extending them to conform to DirectiveArgumentValueConvertible here. the advantage would be that we wouldn't have to stringify these values until render node encoding time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather leave the RenderNode implementation more string-based for now until we have a better idea of how we'll implement longer-term support for custom page types. Our current needs are simple enough that I think we can prioritize flexibility here and reduce the risk of breaking API later on.

"properties": {
"standardColorIdentifier": {
"type": "string",
"enum": ["blue", "gray", "green", "orange", "purple", "red", "yellow"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: would a future custom color use a different property in the render json?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so – I wanted to give us the flexibility for that if we decide it's needed. My current thinking is that we'll likely have some kind of customColorReference here instead of a standard identifier.

# Article title

@Metadata {
@PageColor(green)
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the default diagnostics for automatic directive convertibles when arguments are unknown or when multiple directives are specified, do we need to customize those?

For example

@Metadata {
  @PageColor(banana)
}
@Metadata {
  @PageColor(blue)
  @PageColor(green)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those should both work:

/swift-docc/Sources/docc/DocCDocumentation.docc/customizing-the-appearance-of-your-documentation-pages.md:7:5: warning: Duplicate 'PageColor' child directive
The 'Metadata' directive must have at most one 'PageColor' child directive
/swift-docc/Sources/docc/DocCDocumentation.docc/customizing-the-appearance-of-your-documentation-pages.md:6:1: warning: Cannot convert 'banana' to type 'Color'

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.

This implementation looks good to me.

I left some non-blocking feedback about documentation and checking that diagnostic messages are good.

Copy link
Contributor

@mportiz08 mportiz08 left a comment

Choose a reason for hiding this comment

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

Not a blocker for this PR, but it would be awesome to update <doc:customizing-the-appearance-of-your-documentation-pages> article with an example of this new functionality.

Adds a new `@PageColor` metadata directive that allows for
customizing the color used to represent a given page.

`@PageColor` gives authors control over the color used
when rendering a page – initially this will affect the background
color Swift-DocC-Render uses in the page's introduction (hero) section.

Example:

    # What's New in SlothCreator

    @metadata {
        @PageColor(blue)
    }

    ![A sloth on a tree wearing a fedora.](sloth-fedora)

    Let's check out what's new in SlothCreator!

    ...

Details:

`@PageColor` accepts the following parameters:

  - `color`: An unnamed parameter that accepts one of the following:

     - `blue`: A context-dependent blue color.

     - `gray`: A context-dependent gray color.

     - `green`: A context-dependent orange color.

     - `orange`: A context-dependent orange color.

     - `purple`: A context-dependent purple color.

     - `red`: A context-dependent red color.

     - `yellow`: A context-dependent yellow color.

`@PageColor` is described on the Swift forums here:
https://forums.swift.org/t/support-for-customizing-a-page-s-accent-color-in-swift-docc/64093

Dependencies:

- swiftlang/swift-docc-render#582

rdar://106153042
@ethan-kusters
Copy link
Contributor Author

Not a blocker for this PR, but it would be awesome to update <doc:customizing-the-appearance-of-your-documentation-pages> article with an example of this new functionality.

Good point! I filed #537 to track it.

@ethan-kusters
Copy link
Contributor Author

@swift-ci please test

@ethan-kusters ethan-kusters merged commit 7c39507 into swiftlang:main Apr 7, 2023
@ethan-kusters ethan-kusters deleted the page-color-semantic branch April 7, 2023 18:22
ethan-kusters added a commit to ethan-kusters/swift-docc that referenced this pull request Apr 7, 2023
Adds a new `@PageColor` metadata directive that allows for
customizing the color used to represent a given page.

`@PageColor` gives authors control over the color used
when rendering a page – initially this will affect the background
color Swift-DocC-Render uses in the page's introduction (hero) section.

Example:

    # What's New in SlothCreator

    @metadata {
        @PageColor(blue)
    }

    ![A sloth on a tree wearing a fedora.](sloth-fedora)

    Let's check out what's new in SlothCreator!

    ...

Details:

`@PageColor` accepts the following parameters:

  - `color`: An unnamed parameter that accepts one of the following:

     - `blue`: A context-dependent blue color.

     - `gray`: A context-dependent gray color.

     - `green`: A context-dependent orange color.

     - `orange`: A context-dependent orange color.

     - `purple`: A context-dependent purple color.

     - `red`: A context-dependent red color.

     - `yellow`: A context-dependent yellow color.

`@PageColor` is described on the Swift forums here:
https://forums.swift.org/t/support-for-customizing-a-page-s-accent-color-in-swift-docc/64093

Dependencies:

- swiftlang/swift-docc-render#582

rdar://106153042
@moshe-foreflight
Copy link

Does this work with Tutorials?

@daniel-grumberg
Copy link
Contributor

Does this work with Tutorials?

I am 99% sure it doesn't, Tutorial semantics are very different and this concept doesn't port cleanly over.

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.

5 participants