Skip to content

Commit 340f663

Browse files
author
Sarah Edwards
authored
Add support for cURL in application selector (#19645)
1 parent 1afa277 commit 340f663

File tree

7 files changed

+54
-48
lines changed

7 files changed

+54
-48
lines changed

content/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ defaultPlatform: linux
212212

213213
### `defaultTool`
214214

215-
- Purpose: Override the initial tool selection for a page, where tool refers to the application the reader is using to work with GitHub, such as GitHub.com's web UI, the GitHub CLI, or GitHub Desktop. If this frontmatter is omitted, then the tool-specific content matching the GitHub web UI is shown by default. This behavior can be changed for individual pages, for which a manual selection is more reasonable.
216-
- Type: `String`, one of: `webui`, `cli`, `desktop`.
215+
- Purpose: Override the initial tool selection for a page, where tool refers to the application the reader is using to work with GitHub (such as GitHub.com's web UI, the GitHub CLI, or GitHub Desktop) or the GitHub APIs (such as cURL or the GitHub CLI). If this frontmatter is omitted, then the tool-specific content matching the GitHub web UI is shown by default. This behavior can be changed for individual pages, for which a manual selection is more reasonable.
216+
- Type: `String`, one of: `webui`, `cli`, `desktop`, `curl`.
217217
- Optional.
218218

219219
```yaml

contributing/content-markup-reference.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ You can define a default platform in the frontmatter. For more information, see
102102

103103
## Tool tags
104104

105-
We occasionally need to write documentation for different tools (GitHub UI, GitHub CLI, GitHub Desktop). Each tool may require a different set of instructions. We use tool tags to demarcate information for each tool.
105+
We occasionally need to write documentation for different tools (GitHub UI, GitHub CLI, GitHub Desktop, cURL). Each tool may require a different set of instructions. We use tool tags to demarcate information for each tool.
106106

107107
### Usage
108108

@@ -130,6 +130,14 @@ These instructions are pertinent to GitHub CLI users.
130130
{% enddesktop %}
131131
```
132132

133+
```
134+
{% curl %}
135+
136+
These instructions are pertinent to cURL users.
137+
138+
{% endcurl %}
139+
```
140+
133141
Unlike [operating system tags](#operating-system-tags), which will automatically add tabs to select the operating system at the top of the article, you must add `{% include tool-switcher %}` wherever you want to display tabs to select the tool. This allows you to display the tabs at the top of the article or immediately before a relevant section.
134142

135143
You can define a default tool in the frontmatter. For more information, see the [content README](../content/README.md#defaulttool).

includes/tool-switcher.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<div class="UnderlineNav-body">
44
<a href="#" class="UnderlineNav-item tool-switcher" data-tool="webui">GitHub.com</a>
55
<a href="#" class="UnderlineNav-item tool-switcher" data-tool="cli">CLI</a>
6+
<a href="#" class="UnderlineNav-item tool-switcher" data-tool="curl">cURL</a>
67
<a href="#" class="UnderlineNav-item tool-switcher" data-tool="desktop">Desktop</a>
78
</div>
89
</nav>

javascripts/display-tool-specific-content.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@ import Cookies from 'js-cookie'
22

33
import { sendEvent } from './events'
44

5-
const supportedTools = ['cli', 'desktop', 'webui']
6-
const detectedTools = new Set()
5+
const supportedTools = ['cli', 'desktop', 'webui', 'curl']
76

87
export default function displayToolSpecificContent () {
9-
let tool = getDefaultTool()
8+
const toolElements = Array.from(document.querySelectorAll('.extended-markdown'))
9+
.filter(el => supportedTools.some(tool => el.classList.contains(tool)))
10+
11+
const detectedTools = toolElements
12+
.flatMap(el => Array.from(el.classList).filter(className => supportedTools.includes(className)))
1013

11-
if (!tool) tool = 'webui'
14+
const tool = getDefaultTool(detectedTools)
1215

13-
const toolsInContent = findToolSpecificContent(tool)
16+
showToolSpecificContent(tool, toolElements)
1417

15-
hideSwitcherLinks(toolsInContent)
18+
hideSwitcherLinks(detectedTools)
1619

17-
showContentForTool(tool)
20+
highlightTabForTool(tool)
1821

1922
// configure links for switching tool content
2023
switcherLinks().forEach(link => {
2124
link.addEventListener('click', (event) => {
2225
event.preventDefault()
23-
showContentForTool(event.target.dataset.tool)
24-
findToolSpecificContent(event.target.dataset.tool)
26+
highlightTabForTool(event.target.dataset.tool)
27+
showToolSpecificContent(event.target.dataset.tool, toolElements)
2528

2629
// Save this preference as a cookie.
2730
Cookies.set('toolPreferred', event.target.dataset.tool, { sameSite: 'strict', secure: true })
@@ -36,7 +39,7 @@ export default function displayToolSpecificContent () {
3639
})
3740
}
3841

39-
function showContentForTool (tool) {
42+
function highlightTabForTool (tool) {
4043
// (de)activate switcher link appearances
4144
switcherLinks().forEach(link => {
4245
(link.dataset.tool === tool)
@@ -45,52 +48,45 @@ function showContentForTool (tool) {
4548
})
4649
}
4750

48-
function findToolSpecificContent (tool) {
49-
// find all tool-specific *block* elements and hide or show as appropriate
50-
// example: {{ #cli }} block content {{/cli}}
51-
Array.from(document.querySelectorAll('.extended-markdown'))
52-
.filter(el => supportedTools.some(tool => el.classList.contains(tool)))
53-
.forEach(el => {
54-
detectTools(el)
55-
el.style.display = el.classList.contains(tool)
56-
? ''
57-
: 'none'
58-
})
59-
60-
// find all tool-specific *inline* elements and hide or show as appropriate
61-
// example: <span class="tool-cli">inline content</span>
62-
Array.from(document.querySelectorAll('.tool-cli, .tool-desktop, .tool-webui'))
51+
function showToolSpecificContent (tool, toolElements) {
52+
// show the content only for the highlighted tool
53+
toolElements
54+
.filter(el => supportedTools.some(tool => (el.classList.contains(tool))))
6355
.forEach(el => {
64-
detectTools(el)
65-
el.style.display = el.classList.contains('tool-' + tool)
56+
el.style.display = (el.classList.contains(tool))
6657
? ''
6758
: 'none'
6859
})
69-
70-
return Array.from(detectedTools)
7160
}
7261

7362
// hide links for any tool-specific sections that are not present
74-
function hideSwitcherLinks (toolsInContent) {
63+
function hideSwitcherLinks (detectedTools) {
7564
Array.from(document.querySelectorAll('a.tool-switcher'))
7665
.forEach(link => {
77-
if (toolsInContent.includes(link.dataset.tool)) return
66+
if (detectedTools.includes(link.dataset.tool)) return
7867
link.style.display = 'none'
7968
})
8069
}
8170

82-
function detectTools (el) {
83-
el.classList.forEach(elClass => {
84-
const value = elClass.replace(/tool-/, '')
85-
if (supportedTools.includes(value)) detectedTools.add(value)
86-
})
87-
}
88-
89-
function getDefaultTool () {
71+
function getDefaultTool (detectedTools) {
72+
// If the user selected a tool preference and the tool is present on this page
9073
const cookieValue = Cookies.get('toolPreferred')
91-
if (cookieValue) return cookieValue
92-
const el = document.querySelector('[data-default-tool]')
93-
if (el) return el.dataset.defaultTool
74+
if (cookieValue && detectedTools.includes(cookieValue)) return cookieValue
75+
76+
// If there is a default tool and the tool is present on this page
77+
const defaultToolEl = document.querySelector('[data-default-tool]')
78+
if (defaultToolEl && detectedTools.includes(defaultToolEl.dataset.defaultTool)) {
79+
return defaultToolEl.dataset.defaultTool
80+
}
81+
82+
// Default to webui if present (this is generally the case where we show UI/CLI/Desktop info)
83+
if (detectedTools.includes('webui')) return 'webui'
84+
85+
// Default to cli if present (this is generally the case where we show curl/CLI info)
86+
if (detectedTools.includes('cli')) return 'cli'
87+
88+
// Otherwise, just choose the first detected tool
89+
return detectedTools[0]
9490
}
9591

9692
function switcherLinks () {

lib/frontmatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const schema = {
137137
// Tool-specific content preference
138138
defaultTool: {
139139
type: 'string',
140-
enum: ['webui', 'cli', 'desktop']
140+
enum: ['webui', 'cli', 'desktop', 'curl']
141141
},
142142
// Documentation contributed by a third party, such as a GitHub Partner
143143
contributor: {

lib/liquid-tags/extended-markdown.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const tags = {
55
cli: '',
66
desktop: '',
77
webui: '',
8+
curl: '',
89
all: '',
910
tip: 'border rounded-1 mb-4 p-3 color-border-info color-bg-info f5',
1011
note: 'border rounded-1 mb-4 p-3 color-border-info color-bg-info f5',

lib/schema-event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const context = {
118118
}, */
119119
application_preference: {
120120
type: 'string',
121-
enum: ['webui', 'cli', 'desktop'],
121+
enum: ['webui', 'cli', 'desktop', 'curl'],
122122
description: 'The application selected by the user.'
123123
}
124124
/* color_mode_preference: {
@@ -413,7 +413,7 @@ const preferenceSchema = {
413413
},
414414
preference_value: {
415415
type: 'string',
416-
enum: ['webui', 'cli', 'desktop'],
416+
enum: ['webui', 'cli', 'desktop', 'curl'],
417417
description: 'The application selected by the user.'
418418
}
419419
}

0 commit comments

Comments
 (0)