Skip to content

Commit 8229dea

Browse files
authored
Support OTP apps in "Go to package" (#1945)
1 parent 6b79654 commit 8229dea

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

assets/js/keyboard-shortcuts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const keyboardShortcuts = [
4242
},
4343
{
4444
key: 'g',
45-
description: 'Search HexDocs package',
45+
description: 'Go to package docs',
4646
displayAs: '<kbd><kbd>g</kdb></kdb>',
4747
action: openQuickSwitchModal
4848
},

assets/js/quick-switch.js

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { debounce, qs, qsAll } from './helpers'
22
import { openModal } from './modal'
33

44
const HEX_DOCS_ENDPOINT = 'https://hexdocs.pm/%%'
5+
const OTP_DOCS_ENDPOINT = 'https://www.erlang.org/doc/apps/%%'
56
const HEX_SEARCH_ENDPOINT = 'https://hex.pm/api/packages?search=name:%%*'
67
const QUICK_SWITCH_LINK_SELECTOR = '.display-quick-switch'
78
const QUICK_SWITCH_INPUT_SELECTOR = '#quick-switch-input'
@@ -10,7 +11,46 @@ const QUICK_SWITCH_RESULT_SELECTOR = '.quick-switch-result'
1011
const DEBOUNCE_KEYPRESS_TIMEOUT = 300
1112
const NUMBER_OF_SUGGESTIONS = 9
1213

13-
// Core elixir packages to include in the autocomplete results
14+
const OTP_APPS = [
15+
'erts',
16+
'asn1',
17+
'common_test',
18+
'compiler',
19+
'crypto',
20+
'debugger',
21+
'dialyzer',
22+
'diameter',
23+
'edoc',
24+
'eldap',
25+
'erl_interface',
26+
'et',
27+
'eunit',
28+
'ftp',
29+
'inets',
30+
'jinterface',
31+
'kernel',
32+
'megaco',
33+
'mnesia',
34+
'observer',
35+
'odbc',
36+
'os_mon',
37+
'parsetools',
38+
'public_key',
39+
'reltool',
40+
'runtime_tools',
41+
'sasl',
42+
'snmp',
43+
'ssh',
44+
'ssl',
45+
'stdlib',
46+
'syntax_tools',
47+
'tftp',
48+
'tools',
49+
'wx',
50+
'xmerl'
51+
]
52+
53+
// Core Elixir/OTP packages to include in the autocomplete results
1454
const STATIC_SEARCH_RESULTS = [
1555
'elixir',
1656
'eex',
@@ -19,7 +59,7 @@ const STATIC_SEARCH_RESULTS = [
1959
'iex',
2060
'logger',
2161
'mix'
22-
].map(name => ({ name }))
62+
].concat(OTP_APPS).map(name => ({ name }))
2363

2464
const MIN_SEARCH_LENGTH = 2
2565

@@ -47,7 +87,7 @@ function handleKeyDown (event) {
4787
if (event.key === 'Enter') {
4888
const packageSlug = event.target.value
4989

50-
quickSwitchToPackage(packageSlug)
90+
quickSwitchToAppDocs(packageSlug)
5191
event.preventDefault()
5292
} else if (event.key === 'ArrowUp') {
5393
moveAutocompleteSelection(-1)
@@ -74,7 +114,7 @@ function handleInput (event) {
74114
*/
75115
export function openQuickSwitchModal () {
76116
openModal({
77-
title: 'Search HexDocs package',
117+
title: 'Go to package docs',
78118
body: Handlebars.templates['quick-switch-modal-body']()
79119
})
80120

@@ -89,27 +129,34 @@ export function openQuickSwitchModal () {
89129
}
90130

91131
/**
92-
* Navigate to a package on HexDocs.
132+
* Navigate to application docs.
133+
*
93134
* If an autocomplete entry is selected, it will be used instead of the input text.
94135
*
95-
* @param {String} packageSlug The searched package name
136+
* @param {String} name The searched application name
96137
*/
97-
function quickSwitchToPackage (packageSlug) {
138+
function quickSwitchToAppDocs (name) {
98139
if (state.selectedIdx === null) {
99-
navigateToHexDocPackage(packageSlug)
140+
navigateToAppDocs(name)
100141
} else {
101142
const selectedResult = state.autocompleteResults[state.selectedIdx]
102-
navigateToHexDocPackage(selectedResult.name)
143+
navigateToAppDocs(selectedResult.name)
103144
}
104145
}
105146

106147
/**
107-
* Navigates to HexDocs of a specific package.
148+
* Navigates to app docs.
149+
*
150+
* For Hex packages and Elixir apps go to hexdocs.pm. For OTP apps, erlang.org/doc.
108151
*
109-
* @param {String} packageSlug The package name to navigate to
152+
* @param {String} app The application name to navigate to
110153
*/
111-
function navigateToHexDocPackage (packageSlug) {
112-
window.location = HEX_DOCS_ENDPOINT.replace('%%', packageSlug.toLowerCase())
154+
function navigateToAppDocs (app) {
155+
if (OTP_APPS.includes(app.toLowerCase())) {
156+
window.location = OTP_DOCS_ENDPOINT.replace('%%', app.toLowerCase())
157+
} else {
158+
window.location = HEX_DOCS_ENDPOINT.replace('%%', app.toLowerCase())
159+
}
113160
}
114161

115162
const debouncedQueryForAutocomplete = debounce(queryForAutocomplete, DEBOUNCE_KEYPRESS_TIMEOUT)
@@ -145,7 +192,7 @@ function renderResults ({ results }) {
145192
result.addEventListener('click', event => {
146193
const index = result.getAttribute('data-index')
147194
const selectedResult = state.autocompleteResults[index]
148-
navigateToHexDocPackage(selectedResult.name)
195+
navigateToAppDocs(selectedResult.name)
149196
})
150197
})
151198
}

0 commit comments

Comments
 (0)