Skip to content

Commit b70fcf2

Browse files
committed
Since search-index.js is loaded asynchronously, it needs to be the place that initializes the index, so add new global initializeSearch function that it can call; load scripts from https; improve feedback in doc:site; style cleanup
1 parent 8903c62 commit b70fcf2

File tree

8 files changed

+260
-253
lines changed

8 files changed

+260
-253
lines changed

Cakefile

+13-8
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,11 @@ buildDocs = (watch = no) ->
179179
sectionsSourceFolder = 'documentation/sections'
180180
examplesSourceFolder = 'documentation/examples'
181181
outputFolder = "docs/v#{majorVersion}"
182-
searchCollections = docs: [], changelogs: []
183182
{structure} = require "./documentation/structure.coffee"
184183

185-
monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
186-
187-
formatDate = (date) ->
188-
date.replace /^(\d\d\d\d)-(\d\d)-(\d\d)$/, (match, $1, $2, $3) ->
189-
"#{monthNames[$2 - 1]} #{+$3}, #{$1}"
184+
searchCollections =
185+
docs: []
186+
changelogs: []
190187

191188
# Helpers
192189
releaseHeader = (date, version, prevVersion) ->
@@ -198,6 +195,11 @@ buildDocs = (watch = no) ->
198195
</h2>
199196
"""
200197

198+
monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
199+
formatDate = (date) ->
200+
date.replace /^(\d\d\d\d)-(\d\d)-(\d\d)$/, (match, $1, $2, $3) ->
201+
"#{monthNames[$2 - 1]} #{+$3}, #{$1}"
202+
201203
codeFor = require "./documentation/site/code.coffee"
202204

203205
# Template for search results.
@@ -241,7 +243,7 @@ buildDocs = (watch = no) ->
241243
.replace /^\s+/g, " "
242244

243245
# Build search catalog.
244-
searchCatalogue = (mdDoc, section, data) ->
246+
searchCatalog = (mdDoc, section, data) ->
245247
return unless match = /^(#+?)\s+([^\n]+)\s+([\s\S]+)/.exec mdDoc
246248
[, level, title, body] = match
247249
content = clean body
@@ -293,7 +295,7 @@ buildDocs = (watch = no) ->
293295
md = md.replace /<%= releaseHeader %>/g, releaseHeader
294296
md = md.replace /<%= majorVersion %>/g, majorVersion
295297
md = md.replace /<%= fullVersion %>/g, CoffeeScript.VERSION
296-
searchCatalogue md, file, searchData
298+
searchCatalog md, file, searchData
297299
html = markdownRenderer.render md
298300
html = _.template(html)
299301
codeFor: codeFor()
@@ -391,11 +393,14 @@ buildDocs = (watch = no) ->
391393
window.searchResultTemplate = #{searchResultsTemplate};
392394
window.searchResultsListTemplate = #{searchResultsListTemplate};
393395
window.searchCollections = #{JSON.stringify searchCollections};
396+
window.initializeSearch();
394397
"""
395398
fs.writeFileSync "#{outputFolder}/search-index.js", searchIdx
399+
log 'output', green, "#{outputFolder}/search-index.js"
396400
fs.writeFileSync "#{outputFolder}/index.html", output
397401
log 'compiled', green, "#{indexFile}#{outputFolder}/index.html"
398402
try
403+
fs.symlinkSync "v#{majorVersion}/search-index.js", 'docs/search-index.js'
399404
fs.symlinkSync "v#{majorVersion}/index.html", 'docs/index.html'
400405
catch exception
401406

docs/v2/index.html

+6-6
Large diffs are not rendered by default.

docs/v2/search-index.js

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

documentation/site/body.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<%= include('logo.svg') %>
1414
</p>
1515
</header>
16-
<%= buildBody() %>
16+
<%= buildBody() %>
1717
</main>
1818
</div>
1919
</div>

documentation/site/docs.coffee

+33-32
Original file line numberDiff line numberDiff line change
@@ -209,28 +209,29 @@ $(document).ready ->
209209
# Initializing the code editors might’ve thrown off our vertical scroll position
210210
document.getElementById(window.location.hash.slice(1).replace(/try:.*/, '')).scrollIntoView()
211211

212-
buildSearch = (catalogue, keys) ->
213-
searchOptions = {
214-
includeScore: true,
215-
shouldSort: true,
216-
includeMatches: true,
217-
threshold: 0.2,
218-
location: 0,
219-
distance: 1000,
220-
maxPatternLength: 32,
221-
minMatchCharLength: 4,
222-
keys
223-
}
224-
new Fuse window.searchCollections[catalogue], searchOptions
225-
226-
window.fuseDocs = buildSearch "docs", ["section", "content"]
227-
window.fuseLogs = buildSearch "changelogs", ["content"]
228-
229-
230-
$(document).on "keyup", "#cs-search-input-navbar", ->
231-
resContainer = $("#search-results")
232-
searchResultBox = $("#searchResultBox")
233-
resContainer.hide().html ""
212+
213+
# This function is called from search-index.js, which is loaded asynchronously
214+
window.initializeSearch = ->
215+
buildSearch = (catalog, keys) ->
216+
searchOptions =
217+
keys: keys
218+
includeScore: yes
219+
shouldSort: yes
220+
includeMatches: yes
221+
threshold: 0.2
222+
location: 0
223+
distance: 1000
224+
maxPatternLength: 32
225+
minMatchCharLength: 4
226+
new Fuse window.searchCollections[catalog], searchOptions
227+
228+
window.fuseDocs = buildSearch 'docs', ['section', 'content']
229+
window.fuseLogs = buildSearch 'changelogs', ['content']
230+
231+
$(document).on 'keyup', '#cs-search-input-navbar', ->
232+
resContainer = $('#search-results')
233+
searchResultBox = $('#searchResultBox')
234+
resContainer.hide().html ''
234235
searchResultBox.hide()
235236
return unless @value.length >= 3
236237
markText = (text) ->
@@ -246,8 +247,8 @@ $(document).on "keyup", "#cs-search-input-navbar", ->
246247
marked = markText content[m[0]...m[1]]
247248
start = if m[0] > 50 then m[0] - 50 else 0
248249
end = m[1] + 50
249-
mContent.push "&hellip;" + content[start...m[0]] + marked + content[m[1]...end] + "&hellip;"
250-
mContent.join " "
250+
mContent.push '&hellip;' + content[start...m[0]] + marked + content[m[1]...end] + '&hellip;'
251+
mContent.join ' '
251252

252253
tmpl = window.searchResultTemplate
253254
tmplList = window.searchResultsListTemplate
@@ -260,10 +261,10 @@ $(document).on "keyup", "#cs-search-input-navbar", ->
260261
countMatches = 0
261262
for match in matches
262263
{key, indices} = match
263-
if key is "section" and indices.length > 0
264+
if key is 'section' and indices.length > 0
264265
title = markTitle title, indices
265266
countMatches += indices.length
266-
if key is "content"
267+
if key is 'content'
267268
content = markContent content, indices
268269
countMatches += indices.length
269270
continue if countMatches < 1
@@ -280,16 +281,16 @@ $(document).on "keyup", "#cs-search-input-navbar", ->
280281
continue if list.length < 1
281282
ctmpl = tmpl
282283
section: key
283-
results: list.join ""
284+
results: list.join ''
284285
results.push ctmpl
285286

286287
if results.length > 0
287288
searchResultBox.show()
288-
resContainer.show().html results.join ""
289+
resContainer.show().html results.join ''
289290

290-
$(document).on "click", ".searchWrapper", ->
291-
href = $(this).data "href"
292-
if href[0] is "#"
291+
$(document).on 'click', '.searchWrapper', ->
292+
href = $(this).data 'href'
293+
if href[0] is '#'
293294
window.location.hash = href
294295
else
295-
window.location = href
296+
window.location = href

documentation/site/navbar.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark bg-ribbed-dark site-navbar">
2-
<a class="navbar-brand" href="#" data-close="try"><%= include('logo.svg') %></a>
2+
<a class="navbar-brand" href="#" data-close="try" data-action="scroll-to-top"><%= include('logo.svg') %></a>
33
<button class="navbar-toggler" type="button" data-toggle="offcanvas" data-close="try" aria-label="Toggle sidebar">
44
<span class="navbar-toggler-icon"></span>
55
</button>

documentation/site/scripts.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha384-xBuQ/xzmlsLoJpyjoggmTEz8OWUFM0/RC5BsqQBDX2v5cMvDHcMakNTNrHIW2I5f" crossorigin="anonymous"></script>
2-
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
3-
<script src="//cdn.jsdelivr.net/combine/npm/[email protected]/lib/codemirror.js,npm/[email protected]/mode/coffeescript/coffeescript.js,npm/[email protected]/addon/lint/coffeescript-lint.js,npm/[email protected]/mode/javascript/javascript.js,npm/[email protected]/dist/fuse.min.js"></script>
1+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" integrity="sha384-xBuQ/xzmlsLoJpyjoggmTEz8OWUFM0/RC5BsqQBDX2v5cMvDHcMakNTNrHIW2I5f" crossorigin="anonymous"></script>
2+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
3+
<script src="https://cdn.jsdelivr.net/combine/npm/[email protected]/lib/codemirror.js,npm/[email protected]/mode/coffeescript/coffeescript.js,npm/[email protected]/addon/lint/coffeescript-lint.js,npm/[email protected]/mode/javascript/javascript.js,npm/[email protected]/dist/fuse.min.js"></script>
44
<script src="browser-compiler/coffeescript.js"></script>
55
<script>
66
<%= includeScript('docs.coffee') %>
77
</script>
88
<script async src="search-index.js"></script>
9-
<script async src="//www.googletagmanager.com/gtag/js?id=UA-106156830-1"></script>
9+
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-106156830-1"></script>

0 commit comments

Comments
 (0)