diff --git a/Cakefile b/Cakefile
index cd96e5e44e..6e72ab413a 100644
--- a/Cakefile
+++ b/Cakefile
@@ -179,6 +179,11 @@ buildDocs = (watch = no) ->
sectionsSourceFolder = 'documentation/sections'
examplesSourceFolder = 'documentation/examples'
outputFolder = "docs/v#{majorVersion}"
+ cheerio = require "cheerio"
+
+ searchCollection =
+ tree: {}
+ data: []
# Helpers
releaseHeader = (date, version, prevVersion) ->
@@ -188,8 +193,79 @@ buildDocs = (watch = no) ->
"""
+ formatDate = (date) ->
+ monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
+ date.replace /^(\d\d\d\d)-(\d\d)-(\d\d)$/, (match, $1, $2, $3) ->
+ "#{monthNames[$2 - 1]} #{+$3}, #{$1}"
+
codeFor = require "./documentation/site/code.coffee"
+ # Template for search results.
+ searchResults = """
+
+ """
+ # Template for search result item.
+ searchResultsList = """
+
+
+
+ <%= title %>
+
+
<%= content %>
+
+
+ """
+ searchResultsTemplate = _.template(searchResults).source
+ searchResultsListTemplate = _.template(searchResultsList).source
+
+ # Build search catalog.
+ buildSearchCatalog = (html) ->
+ $ = cheerio.load html
+ parseSectionContent = (section, level) ->
+ sectionId = $(section).attr "id"
+ header = "> h#{level + 1}"
+ # Chagelogs subsections, e.g. 2.3.0 -
+ version = $("#{header} a", section).text()
+ date = $("#{header} span time", section).text()
+ title = if version and date then "#{version} - #{date}" else $(header, section).text()
+ dataLevel = $(section).data("level") or no
+ content = $(":not(section)", section).text()
+ .replace ///^#{title}///, "" # Remove title from the content.
+ .replace /\n+/g, " " # Convertnewlines into spaces.
+ .replace /^(?:\t|\s)+/g, " " # Remove extra spaces.
+ {section:sectionId, title, content, dataLevel}
+
+ addCollection = ({el, level, parent=no}) ->
+ {section, title, dataLevel} = data = Object.assign {}, parseSectionContent(el, level), {level, parent}
+ if not dataLevel and parent
+ dataLevel = searchCollection.tree[parent].dataLevel
+ searchCollection.tree[section] = {title, parent, dataLevel}
+ searchCollection.data.push Object.assign {}, data, {dataLevel}
+ section
+
+ parseSections = (sections, level=1, parent=no) ->
+ # Level 1, e.g. main > section
+ sections.each (i, el) ->
+ section = addCollection {el, level, parent}
+ # Level 2, e.g. main > section > section
+ subSections = $("section", el)
+ if subSections?.length > 1
+ parseSections subSections, level + 1, section
+
+ parseSections $("main.main > section")
+ """
+ window.searchResultTemplate = #{searchResultsTemplate};
+ window.searchResultsListTemplate = #{searchResultsListTemplate};
+ window.searchCollection = #{JSON.stringify searchCollection};
+ """
+
htmlFor = ->
hljs = require 'highlight.js'
hljs.configure classPrefix: ''
@@ -204,7 +280,6 @@ buildDocs = (watch = no) ->
catch ex
return '' # No syntax highlighting
-
# Add some custom overrides to Markdown-It’s rendering, per
# https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
defaultFence = markdownRenderer.renderer.rules.fence
@@ -249,14 +324,16 @@ buildDocs = (watch = no) ->
includeScript: includeScript()
output
- # Task
do renderIndex = ->
render = _.template fs.readFileSync(indexFile, 'utf-8')
- output = render
- include: include()
+ output = render include: include()
+ searchIndex = buildSearchCatalog output
+ fs.writeFileSync "#{outputFolder}/search-index.js", searchIndex
+ log 'compiled', green, "search index → #{outputFolder}/search-index.js"
fs.writeFileSync "#{outputFolder}/index.html", output
log 'compiled', green, "#{indexFile} → #{outputFolder}/index.html"
try
+ fs.symlinkSync "v#{majorVersion}/search-index.js", 'docs/search-index.js'
fs.symlinkSync "v#{majorVersion}/index.html", 'docs/index.html'
catch exception
diff --git a/docs/search-index.js b/docs/search-index.js
new file mode 120000
index 0000000000..588e0e3850
--- /dev/null
+++ b/docs/search-index.js
@@ -0,0 +1 @@
+v2/search-index.js
\ No newline at end of file
diff --git a/docs/v2/index.html b/docs/v2/index.html
index 9eb16668cf..eeda9e9145 100644
--- a/docs/v2/index.html
+++ b/docs/v2/index.html
@@ -18,6 +18,715 @@
@@ -565,10 +1311,25 @@
GitHub
+
+
+