Skip to content

Document how to customize appearance of DocC docs #359

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
merged 36 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d61572b
Start writing spec for theme settings file.
mportiz08 Jul 21, 2022
97088a4
Specify features.
mportiz08 Jul 21, 2022
6144563
Fix formatting for `meta.title` description.
mportiz08 Jul 21, 2022
2ce049e
Add descriptions for theme settings schemas.
mportiz08 Jul 21, 2022
cc19bfe
Add specs for border-radius and code/typography settings.
mportiz08 Jul 22, 2022
bc01db5
Add specs for colors.
mportiz08 Jul 25, 2022
6872f4f
Document all existing, overridable color names.
mportiz08 Jul 26, 2022
6c011d2
Remove explicit schemas for nested objects where it makes sense.
mportiz08 Jul 26, 2022
632b363
Enforce positive indentation width.
mportiz08 Jul 26, 2022
50feb9b
Add documentation-intro colors.
mportiz08 Jul 27, 2022
fcb6fa4
Specify icon overrides.
mportiz08 Jul 27, 2022
7ac8603
Allow for code listing and aside elements to have custom borders.
mportiz08 Jul 29, 2022
b8df45e
Extract border attributes to remove unecessary property duplication.
mportiz08 Jul 29, 2022
dd4bea0
Support custom borders for buttons.
mportiz08 Aug 1, 2022
d957cd8
Support custom colors for tutorials overview page.
mportiz08 Aug 1, 2022
5e405d3
Support custom borders for badges.
mportiz08 Aug 1, 2022
5bb17ba
Support custom border radius for tutorial steps.
mportiz08 Aug 3, 2022
1c71b22
Add article stub: "Customizing Your Documentation Appearance".
mportiz08 Aug 5, 2022
900feb7
Start documenting appearance customization.
mportiz08 Aug 5, 2022
ddb8130
Add examples for adding/updating colors.
mportiz08 Aug 8, 2022
99b9123
Update advanced color example.
mportiz08 Aug 9, 2022
d993b79
Link to more complete theme example.
mportiz08 Aug 9, 2022
6ed4793
Add example of customizing fonts.
mportiz08 Aug 9, 2022
b1f1819
Add example of customizing borders.
mportiz08 Aug 10, 2022
569809b
Add example of custom page title.
mportiz08 Aug 10, 2022
8628480
Add example of enabling a feature flag.
mportiz08 Aug 10, 2022
d4f5684
Merge branch 'document-theme-settings-api' into document-theme-settings
mportiz08 Aug 10, 2022
b6e9804
Merge branch 'document-appearance-customization' into document-theme-…
mportiz08 Aug 10, 2022
3ed0578
Add example of overriding an icon.
mportiz08 Aug 11, 2022
0b4d51f
Fix whitespace.
mportiz08 Aug 12, 2022
be2d5ea
Fix typo.
mportiz08 Aug 12, 2022
3f470bf
Fix copyright year in comment.
mportiz08 Aug 12, 2022
f20fdcd
Use better screenshot for updated icons.
mportiz08 Aug 12, 2022
b957dc7
s/DocC-Render/DocC
mportiz08 Aug 12, 2022
c5f0be9
Update title of article.
mportiz08 Aug 12, 2022
efda5f4
Merge branch 'main' into document-theme-settings
mportiz08 Sep 23, 2022
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ DocC syntax — called _documentation markup_ — is a custom variant of Markdow
### Distribution

- <doc:distributing-documentation-to-other-developers>
- <doc:customizing-the-appearance-of-your-documentation-pages>

### Documentation Types

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# Customizing the Appearance of Your Documentation Pages

Customize the look and feel of your documentation webpages.

## Overview

By default, rendered documentation webpages produced by DocC come with a default
visual styling. If you wish, you may make adjustments to this styling by adding
an optional `theme-settings.json` file to the root of your documentation catalog
with some configuration. This file is used to customize various things like
colors and fonts. You can even make changes to the way that specific elements
appear, like buttons, code listings, and asides. Additionally, some metadata for
the website can be configured in this file, and it can also be used to opt in or
opt out of certain default rendering behavior.

If you're curious about the full capabilities of the `theme-settings.json` file,
you can check out the latest Open API specification for it [here][1], which
documents all valid settings that could be present.

Let's walk through some basic examples of customizing the appearance of
documentation now.

### Colors

DocC utilizes [CSS custom properties][2] (CSS variables) to define the various
colors that are used throughout the HTML elements of rendered documentation. Any
key/value in the `theme.color` object of `theme-settings.json` will either
update an existing color property used in the renderer or add a new one that
could be referenced by existing ones.

**Example**

As a simple example, the following configuration would update the background
fill color for the "documentation intro" element to `"rebeccapurple"` by
changing the value of the CSS property to `--color-documentation-intro-fill:
"rebeccapurple"`.

```json
{
"theme": {
"color": {
"documentation-intro-fill": "rebeccapurple"
}
}
}
```

![A screenshot of the documentation intro element with a purple background color](theme-screenshot-01.png)

**Example**

Note that any valid CSS color value could be used. Here is a more advanced
example which demonstrates how specialized dark and light versions of the main
background fill and nav colors could be defined as varying lightness percentages
of a green hue.

```json
{
"theme": {
"color": {
"hue-green": "120",
"fill": {
"dark": "hsl(var(--color-hue-green), 100%, 10%)",
"light": "hsl(var(--color-hue-green), 100%, 90%)"
},
"fill-secondary": {
"dark": "hsl(var(--color-hue-green), 100%, 20%)",
"light": "hsl(var(--color-hue-green), 100%, 80%)"
},
"nav-solid-background": "var(--color-fill)"
}
}
}
```

![A screenshot of the green page background in dark mode](theme-screenshot-02.png)

![A screenshot of the green page background in light mode](theme-screenshot-03.png)

As a general rule, the default color properties provided by DocC assumes a
naming convention where "fill" colors are used for backgrounds and "figure"
colors are used for foreground colors, like text.

> Tip:
> For a more complete example of a fully customized documentation website, you
> can check out [this fork][3] of the DocC documentation that is using this
> [theme-settings.json file][4].
### Fonts

By default, there are two main fonts used by DocC: one for monospaced text in
code voice and another for all other text content. With `theme-settings.json`,
you can customize the CSS `font-family` lists for these however you choose.

**Example**

```json
{
"theme": {
"typography": {
"html-font": "\"Comic Sans MS\", \"Comic Sans\", cursive",
"html-font-mono": "\"Courier New\", Courier, monospace"
}
}
}
```

![A screenshot of a documentation page with custom fonts](theme-screenshot-04)

### Borders

Another aspect of the renderer that you may wish to modify is the way that it
applies border to certain elements. With `theme-settings.json`, you can change
the default border radius for all elements and also individually update the
border styles for asides, buttons, code listings, and tutorial steps.

**Example**

```json
{
"theme": {
"aside": {
"border-radius": "6px",
"border-style": "double",
"border-width": "3px"
},
"border-radius": "0",
"button": {
"border-radius": "8px",
"border-style": "solid",
"border-width": "2px"
},
"code": {
"border-radius": "2px",
"border-style": "dashed",
"border-width": "2px"
},
"tutorial-step": {
"border-radius": "12px"
}
}
}
```

![A screenshot of a tutorial page with custom button
borders](theme-screenshot-05.png)

![A screenshot of tutorial steps with custom borders](theme-screenshot-06.png)

![A screenshot of a documentation page with custom code listing and aside border
styles](theme-screenshot-07.png)

### Icons

It is also possible to use `theme-settings.json` to specify custom icons to use
in place of the default ones provided by DocC. There are a few basic steps to
follow in order to override an icon:

1. Find the identifier that maps to the icon you want to override.
2. Add this value as an `id` attribute to the new SVG icon.
3. Associate a relative or absolute URL to the new SVG icon with the identifier
in `theme-settings.json`.

> Note:
> You can find all the possible identifier values for every valid icon kind in
> the Open API [specification][1] for `theme-settings.json`.
**Example**

As a simple example, here is how you could update the icon used for articles in
the sidebar to be a simple box:

1. Find the identifier for article icons: `"article"`
2. Add the `id="article"` attribute to the new SVG icon: `simple-box.svg`
```svg
<svg viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg" id="article">
<rect x="2" y="2" width="10" height="10" stroke="currentColor" fill="none"></rect>
</svg>
```
3. Configure `theme-settings.json` to map the URL for the new article icon
```json
{
"theme": {
"icons": {
"article": "/images/simple-box.svg"
}
}
}
```

![A screenshot of the sidebar using a custom icon for
articles](theme-screenshot-08.png)

### Metadata

You can specify some text that will be used in the HTML `<title>` element for
all webpages in `theme-settings.json` in place of the default "Documentation"
text.

**Example**

```json
{
"meta": {
"title": "Custom Title",
}
}
```

With that configuration, the title of this page would change from "Customizing
Your Documentation Appearance | Documentation" to "Customizing Your Documentation
Appearance | **Custom Title**".

### Feature Flags

From time to time, there may be instances of various experimental UI features
that can be enabled or disabled using `theme-settings.json`.

**Example**

As of the time of writing this, there is only one available "quick navigation"
feature that is disabled by default and can be enabled with the following
configuration. Note that this flag may be dropped in the future and new ones may
be added as necessary for other features.

```json
{
"features": {
"docs": {
"quickNavigation": {
"enable": true
}
}
}
}
```

[1]: https://github.com/apple/swift-docc/blob/main/Sources/SwiftDocC/SwiftDocC.docc/Resources/ThemeSettings.spec.json
[2]: https://drafts.csswg.org/css-variables/
[3]: https://mportiz08.github.io/swift-docc/documentation/docc
[4]: https://mportiz08.github.io/swift-docc/theme-settings.json

<!-- Copyright (c) 2022 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Loading