This PR fixes some accessibility issues in our theme docs (i.e. not generated code) flagged by #1513. Here, I target changes that I say are not "systemic", i.e. issues that are easily resolvable by changing our copy and page structure (rather than issues that are created by how kramdown/rouge generates HTML, or reworking our color themes). Here's a quick summary of the manual changes I made: - ~~writing some JS to set `tabindex="0"` on all code blocks; I'd prefer a ruby-native solution, but that involves writing Ruby code, which is incompatible with the pages gem~~ I've moved this to #1533 - rewriting many headings named "Example" which were almost always h4s into more descriptive headings + the appropriate heading level, adding .text-delta to maintain the previous style when necessary - removing some old heading ID hacks in `index-test` that are no longer necessary, since Jekyll does this automatically now - fixing the table headings in `docs/utilities/layout.md` - adding accessible titles + descriptions to the mermaid examples - occasionally, slightly moving around copy to make it align with new headings If you test with #1513 with the following rules disabled: ```rb skipped_rules = [ 'color-contrast', # requires theme auditing # issues w/ autogenerated footnotes 'aria-allowed-role', 'landmark-no-duplicate-contentinfo', 'landmark-unique', 'aria-deprecated-role', # issues w/ markdown checkboxes 'label' ] ``` You should get passing tests :) which is awesome! ## next steps 1. we need to do a pass over our docs copy - very inconsistent. This has been a pain point for me for a while now, just need to find time to sit down and do it. In particular, I'd love to standardize how we display example code (perhaps even hiding it with `<details>` and `<summary>`?), our headings language, what goes into the ToC, our overall writing style, etc. 2. ~~I don't love the JS hack for adding `tabindex="0"` to code blocks (so that they are keyboard-focusable). Ideally, we'd add a custom formatter to rouge to do this, but we can't execute arbitrary Ruby code when users use `github-pages`. I'll look into this some more - maybe rouge would be open to adding this as a feature.~~ see: #1533 4. There are some systemic issues that need a deeper look: 1. The most common issue is still color-contrast. Fixing this involves: - looking at our whites/blacks/grays for core text and highlighting - reevaluating our syntax highlighting themes - fixing dark mode, once and for all :) - also, picking accessible callout colours! 2. kramdown's autogenerated footnotes feature creates a bunch of errors that aXe flags: it seems like a deprecated aria role is being used, and perhaps some misuse of markup. Need to look into this more before I can make a solid attempt at resolving this issue. 3. We demonstrate the use of `- [ ]`, but this generates `<input type="checkbox">` values with no label. I'm not entirely sure what the best way to fix this problem is (without writing custom Ruby code). I'll have to think about this some more. --------- Co-authored-by: Michael Ball <michael@mball.co>
7.3 KiB
title | parent | nav_order |
---|---|---|
Code | UI Components | 6 |
Code
{: .no_toc }
Table of contents
{: .no_toc .text-delta }
- TOC {:toc}
Inline code
Code can be rendered inline by wrapping it in single back ticks.
Heading with <inline code snippet>
in it.
{: .no_toc }
Heading with <inline code snippet>
in it.
---
## Syntax highlighted code blocks
Use Jekyll's built-in syntax highlighting with Rouge for code blocks by using three backticks, followed by the language name:
<div class="code-example" markdown="1">
```js
// Javascript code with syntax highlighting.
var fun = function lang(l) {
dateformat.i18n = require('./lang/' + l)
return true;
}
{% highlight markdown %}
```js
// Javascript code with syntax highlighting.
var fun = function lang(l) {
dateformat.i18n = require('./lang/' + l)
return true;
}
```
{% endhighlight %}
Syntax highlighting, line numbers, and HTML compression do not work together; the combination of these features generates invalid HTML that renders incorrectly. To learn more, see ["Code with line numbers"]({% link docs/ui-components/code/line-numbers.md %}).
Code blocks with rendered examples
To demonstrate front end code, sometimes it's useful to show a rendered example of that code. After including the styles from your project that you'll need to show the rendering, you can use a <div>
with the code-example
class, followed by the code block syntax. If you want to render your output with Markdown instead of HTML, use the markdown="1"
attribute to tell Jekyll that the code you are rendering will be in Markdown format... This is about to get meta...
Link button{: .btn }
Link button{: .btn }
Mermaid diagram code blocks
{: .d-inline-block }
New (v0.4.0) {: .label .label-green }
Mermaid allows you to add diagrams and visualizations using Markdown code blocks. It is disabled by default. However, you can turn on support for mermaid by adding a mermaid
key to your _config.yml
.
The minimum configuration requires a version
key (matching a version in jsDelivr):
mermaid:
# Version of mermaid library
# Pick an available version from https://cdn.jsdelivr.net/npm/mermaid/
version: "9.1.3"
Additional configuration options are loaded through _includes/mermaid_config.js
. By default, the contents of the file are the empty object:
// _includes/mermaid_config.js
{}
This loads the default settings.
The contents of this object should follow mermaid's configuration API. For example, to override the theme, change _includes/mermaid_config.js
to:
// _includes/mermaid_config.js
{
theme: "forest"
}
Once mermaid is installed, it can be used in markdown files. The markdown for a simple flowchart example might look like the following:
{% highlight markdown %}
graph TD;
accTitle: the diamond pattern
accDescr: a graph with four nodes: A points to B and C, while B and C both point to D
A-->B;
A-->C;
B-->D;
C-->D;
{% endhighlight %}
which renders:
graph TD;
accTitle: the diamond pattern
accDescr: a graph with four nodes: A points to B and C, while B and C both point to D
A-->B;
A-->C;
B-->D;
C-->D;
Note: for demonstration purposes, we've enabled mermaid on this site. It is still disabled by default, and users need to opt-in to use it.
Using a local mermaid library
To load a local version of mermaid, also use the path
key to specify the location of the library; e.g.
mermaid:
version: "10.1.0"
# for (v10+)
path: "/assets/js/mermaid.esm.min.mjs"
# for (<v10):
# path: "/assets/js/mermaid.min.js"
# Note: copy both `mermaid.esm.min.mjs` (v10+) or `mermaid.min.js` (<v10) and the associated
# `.map` file from the specified version of `mermaid/dist` to `/assets/js/`.
For mermaid versions >=10
, this file is imported directly as an ESM module (rather than as a plain <script>
tag); users should use the mermaid.esm.min.mjs
file. In contrast, for mermaid versions <10
, this file is loaded as a script tag; it should be a standalone CJS file (i.e. mermaid.min.js
).
{: .warning }
Mermaid versions 10.0
- 10.1
(and possibly, future releases) still encode relative imports in mermaid.esm.min.mjs
. Local users must copy all of the contents of the dist
folder to the specified path (preserving the relative location of the files). Just the Docs is actively monitoring mermaid releases; an upstream fix is planned.
Using mermaid with AsciiDoc
Users of AsciiDoc (e.g. via jekyll-asciidoc) may need additional configuration to use mermaid.
By default, AsciiDoc generates HTML markup that mermaid cannot properly parse. The simplest way to resolve this is to use a passthrough block: {% highlight asciidoc %} ++++
graph TD; accTitle: the diamond pattern accDescr: a graph with four nodes: A points to B and C, while B and C both point to D A-->B; A-->C; B-->D; C-->D;
++++ {% endhighlight %}
Alternatively, community member @flyx has contributed a Ruby extension that does not require extra markup. The extension is available as a GitHub Gist. Thank you to @flyx!
The asciidoctor-diagram extension which also supports mermaid is not recommended for use with Just the Docs, since it requires separate configuration e.g. for theming, and is known to not be trivial to set up.
Copy button
{: .d-inline-block }
New (v0.4.0) {: .label .label-green }
The copy button for code blocks can be enabled or disabled via the enable_copy_code_button
key in _config.yml
. By default, the value of this key is false
; users need to opt-in.
# For copy button on code
enable_copy_code_button: true
Note that this feature requires JavaScript; if JavaScript is disabled in the browser, this feature will not work. In addition, this feature uses navigator.clipboard
, which is only available in secure contexts (such as over HTTPS). If the site is viewed in an insecure context, the copy button will not work (relevant issue: #1202).