Improve accessibility of the docs (#9338)

Merge pull request 9338
This commit is contained in:
Daniel Haim 2024-01-12 17:57:15 +01:00 committed by GitHub
parent 108c8419e3
commit 55e993be25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 98 additions and 84 deletions

View File

@ -1,33 +1,33 @@
<script> <script>
var anchorForId = function (id) { /* Creates an anchor element with the given ID and link for the permalink*/
var anchor = document.createElement("a"); const anchorForId = (id) => {
anchor.className = "header-link"; const anchor = document.createElement("a");
anchor.href = "#" + id; anchor.className = "header-link";
anchor.innerHTML = "<span class=\"sr-only\">Permalink</span><i class=\"fa fa-link\"></i>"; anchor.href = `#${id}`;
anchor.title = "Permalink"; anchor.innerHTML = `<span class="sr-only">Permalink</span><i class="fa fa-link" aria-hidden="true"></i>`;
return anchor; anchor.title = "Permalink";
}; return anchor;
};
var linkifyAnchors = function (level, containingElement) { /* Finds all headers of the specified level within the given element, and adds a permalink to each header*/
var headers = containingElement.getElementsByTagName("h" + level); const linkifyAnchors = (level, containingElement) => {
for (var h = 0; h < headers.length; h++) { const headers = Array.from(containingElement.getElementsByTagName(`h${level}`));
var header = headers[h]; headers.forEach((header) => {
if (header.id) {
if (typeof header.id !== "undefined" && header.id !== "") { header.appendChild(anchorForId(header.id));
header.appendChild(anchorForId(header.id));
}
} }
}; });
};
document.onreadystatechange = function () { /* Executes the function when the document is ready */
if (this.readyState === "complete") { document.onreadystatechange = () => {
var contentBlock = document.getElementsByClassName("docs")[0] || document.getElementsByClassName("news")[0]; if (document.readyState === "complete") {
if (!contentBlock) { const contentBlock = document.getElementsByClassName("docs")[0]
return; ?? document.getElementsByClassName("news")[0];
} if (!contentBlock) { return; }
for (var level = 1; level <= 6; level++) { for (let level = 1; level <= 6; level++) {
linkifyAnchors(level, contentBlock); linkifyAnchors(level, contentBlock);
}
} }
}; }
};
</script> </script>

View File

@ -1,14 +1,17 @@
<div class="unit one-fifth hide-on-mobiles"> <div class="unit one-fifth hide-on-mobiles">
<aside> <aside>
{% for section in site.data.docs_nav -%} {% for section in site.data.docs_nav %}
<h4>{{ section.title }}</h4> <h4>{{ section.title }}</h4>
<ul> <ul>
{%- for item in section.docs -%} {%- for item in section.docs -%}
{%- assign p = site.docs | where: "url", item.link | first %} {%- assign current_page = site.docs | where: "url", item.link | first -%}
<li {%- if page.url == p.url %} class="current" {%- endif -%}><a href="{{ p.url | relative_url }}"> {%- capture item_html -%}
{{- p.menu_name | default: p.title -}} <a href="{{ current_page.url | relative_url }}">
</a></li> {{ current_page.menu_name | default: current_page.title }}
{%- endfor %} </a>
{% endcapture %}
<li{%- unless current_page.url != page.url -%} class="current"{%- endunless -%}>{{ item_html }}</li>
{% endfor %}
</ul> </ul>
{% endfor -%} {% endfor -%}
</aside> </aside>

View File

@ -1,15 +1,15 @@
<div class="docs-nav-mobile unit whole show-on-mobiles"> <div class="docs-nav-mobile unit whole show-on-mobiles">
<select onchange="if (this.value) window.location.href=this.value"> <select id="doc-nav" onchange="navigateToUrl(this)" aria-label="Select a page from the documentation">
<option value="">Navigate the docs…</option> <option value="">Navigate the docs…</option>
{% for section in site.data.docs_nav -%} {% for section in site.data.docs_nav %}
<optgroup label="{{ section.title }}"> <optgroup label="{{ section.title }}">
{%- for item in section.docs -%} {%- for item in section.docs -%}
{% assign p = site.docs | where: "url", item.link | first %} {% assign page = site.docs | where: "url", item.link | first %}
<option value="{{ p.url | relative_url }}"> <option value="{{ page.url | relative_url }}">
{{- p.menu_name | default: p.title -}} {{- page.menu_name | default: page.title -}}
</option> </option>
{%- endfor %} {%- endfor %}
</optgroup> </optgroup>
{% endfor -%} {% endfor %}
</select> </select>
</div> </div>

View File

@ -3,12 +3,12 @@
{% if p.show_on_mobile -%} {% if p.show_on_mobile -%}
<li <li
{%- if p.link == '/' -%} {%- if p.link == '/' -%}
{%- if page.url == '/' %} class="current" {% endif -%} {%- if page.url == '/' %} class="current" aria-current="page" {% endif -%}
{% else -%} {% else -%}
{%- if page.url contains p.link %} class="current" {% endif -%} {%- if page.url contains p.link %} class="current" aria-current="page" {% endif -%}
{% endif -%} {% endif -%}
><a href="{{ p.link | relative_url }}">{{ p.title }}</a></li> ><a href="{{ p.link | relative_url }}">{{ p.title }}</a></li>
{% endif -%} {% endif -%}
{% endfor -%} {% endfor -%}
<li><a href="{{ site.repository }}">GitHub</a></li> <li><a href="{{ site.repository }}" target="_blank" rel="noopener">GitHub</a></li>
</ul> </ul>

View File

@ -1,5 +1,5 @@
<div class="docs-nav-mobile unit whole show-on-mobiles"> <div class="docs-nav-mobile unit whole show-on-mobiles">
<select onchange="if (this.value) window.location.href=this.value"> <select id="news-nav" onchange="navigateToUrl(this)" aria-label="Select a post from the blog">
<option value="">Navigate the blog…</option> <option value="">Navigate the blog…</option>
<option value="{{ '/news/' | relative_url }}">Home</option> <option value="{{ '/news/' | relative_url }}">Home</option>
<optgroup label="posts"> <optgroup label="posts">
@ -8,4 +8,4 @@
{% endfor -%} {% endfor -%}
</optgroup> </optgroup>
</select> </select>
</div> </div>

View File

@ -1,5 +1,5 @@
<article> <article itemscope itemtype="http://schema.org/NewsArticle">
<h2> <h2 itemprop="headline">
<a href="{{ post.url | relative_url }}"> <a href="{{ post.url | relative_url }}">
{{- post.title -}} {{- post.title -}}
</a> </a>
@ -10,16 +10,16 @@
</span> </span>
</span> </span>
<div class="post-meta"> <div class="post-meta">
<span class="post-date"> <span class="post-date" itemprop="datePublished" content="{{ post.date | date: "%Y-%m-%d" }}">
{{- post.date | date_to_string -}} {{- post.date | date_to_string -}}
</span> </span>
{% assign author = post.author -%} {% assign author = post.author -%}
<a href="https://github.com/{{ author }}" class="post-author"> <a class="post-author" itemprop="author" itemscope itemtype="http://schema.org/Person" target="_blank" href="https://github.com/{{ author }}">
{% avatar user=author size=24 -%} {% avatar user=author size=24 -%}
<span class="author-name">{{ author }}</span> <span class="author-name" itemprop="name">{{ author }}</span>
</a> </a>
</div> </div>
<div class="post-content"> <div class="post-content" itemprop="articleBody">
{{- post.content -}} {{- post.content -}}
</div> </div>
</article> </article>

View File

@ -1,4 +1,4 @@
<article> <article itemscope itemtype="http://schema.org/Article">
<div class="cell-left"> <div class="cell-left">
<span class="post-category"> <span class="post-category">
<span class="label"> <span class="label">
@ -8,18 +8,18 @@
</div> </div>
<div class="cell-right"> <div class="cell-right">
<div class="post-meta"> <div class="post-meta">
<h2 class="post-title"> <h2 class="post-title" itemprop="headline">
<a href="{{ post.url | relative_url }}"> <a href="{{ post.url | relative_url }}" itemprop="url">
{{- post.title -}} {{- post.title -}}
</a> </a>
</h2> </h2>
<span class="post-date"> <span class="post-date" itemprop="datePublished" content="{{ post.date | date: '%Y-%m-%d' }}">
{{- post.date | date_to_string -}} {{- post.date | date_to_string -}}
</span> </span>
{% assign author = post.author -%} {% assign author = post.author -%}
<a href="https://github.com/{{ author }}" class="post-author"> <a href="https://github.com/{{ author }}" class="post-author" itemprop="author" itemscope itemtype="http://schema.org/Person">
{% avatar user=author size=24 -%} {% avatar user=author size=24 -%}
<span class="author-name">{{ author }}</span> <span class="author-name" itemprop="name">{{ author }}</span>
</a> </a>
</div> </div>
</div> </div>

View File

@ -1 +1 @@
<input type="text" id="docsearch-input" placeholder="Search the docs…"> <input type="text" id="docsearch-input" placeholder="Search the docs…" aria-label="Search">

View File

@ -1,9 +1,9 @@
<script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js" defer></script>
<script> docsearch({ <script> docsearch({
apiKey: '50fe39c839958dfad797000f33e2ec17', apiKey: '50fe39c839958dfad797000f33e2ec17',
indexName: 'jekyllrb', indexName: 'jekyllrb',
inputSelector: '#docsearch-input', inputSelector: '#docsearch-input',
enhancedSearchInput: true, enhancedSearchInput: true,
debug: false // Set debug to true if you want to inspect the dropdown debug: false
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
<!DOCTYPE HTML> <!DOCTYPE html>
<html lang="en-US"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="viewport" content="width=device-width,initial-scale=1">

View File

@ -1,14 +1,14 @@
<div class="docs-nav-mobile unit whole show-on-mobiles"> <div class="docs-nav-mobile unit whole show-on-mobiles">
<select onchange="if (this.value) window.location.href=this.value"> <select id="tutorial-nav" onchange="navigateToUrl(this)" aria-label="Select a tutorial">
<option value="">Navigate the tutorials…</option> <option value="">Navigate the tutorials…</option>
{% for section in site.data.tutorials -%} {% for section in site.data.tutorials -%}
<optgroup label="{{ section.title }}"> <optgroup label="{{ section.title }}">
{% for item in section.tutorials -%} {% for item in section.tutorials -%}
{% assign item_url = item | prepend:"/tutorials/" | append:"/" -%} {% assign item_url = item | prepend:"/tutorials/" | append:"/" -%}
{% assign tutorial = site.tutorials | where: "url", item_url | first -%} {% assign tutorial = site.tutorials | where: "url", item_url | first -%}
<option value="{{ tutorial.url | relative_url }}">{{ tutorial.title }}</option> <option value="{{ tutorial.url | relative_url }}">{{ tutorial.title }}</option>
{% endfor -%} {% endfor -%}
</optgroup> </optgroup>
{% endfor -%} {% endfor -%}
</select> </select>
</div> </div>

View File

@ -9,5 +9,16 @@
{%- include anchor_links.html -%} {%- include anchor_links.html -%}
{%- include analytics.html -%} {%- include analytics.html -%}
{%- include search/script.html -%} {%- include search/script.html -%}
<script>
{%- comment -%}
// This function is called when the user selects an option from a <select> element.
// If the selected option has a valid URL, it changes the window location to that URL.
{%- endcomment %}
const navigateToUrl = (select) => {
const url = select.value;
url && (window.location.href = url);
};
</script>
</body> </body>
</html> </html>

View File

@ -2,8 +2,8 @@
layout: news layout: news
--- ---
<article> <article itemscope itemtype="http://schema.org/Article">
<h2> <h2 itemprop="headline">
{{ page.title }} {{ page.title }}
<a href="{{ page.url | relative_url }}" class="header-link" title="Permalink"> <a href="{{ page.url | relative_url }}" class="header-link" title="Permalink">
<span class="sr-only">Permalink</span> <span class="sr-only">Permalink</span>
@ -16,16 +16,16 @@ layout: news
</span> </span>
</span> </span>
<div class="post-meta"> <div class="post-meta">
<span class="post-date"> <span class="post-date" itemprop="datePublished" content="{{ page.date | date: "%Y-%m-%d" }}">
{{- page.date | date_to_string -}} {{- page.date | date_to_string -}}
</span> </span>
{% assign author = page.author -%} {% assign author = page.author -%}
<a href="https://github.com/{{ author }}" class="post-author"> <a class="post-author" itemprop="author" itemscope itemtype="http://schema.org/Person" target="_blank" href="https://github.com/{{ author }}">
{% avatar user=author size=24 -%} {% avatar user=author size=24 -%}
<span class="author-name">{{ author }}</span> <span class="author-name" itemprop="name">{{ author }}</span>
</a> </a>
</div> </div>
<div class="post-content"> <div class="post-content" itemprop="articleBody">
{{ content }} {{ content }}
</div> </div>
</article> </article>