From 35f7b6a9bb5ad8bd4085fa1285fc43ad9607ce8a Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sun, 26 Jul 2015 22:34:06 -0400 Subject: [PATCH] Add search abbreviations Closes #193. --- assets/javascripts/app/searcher.coffee | 13 +++++++--- assets/javascripts/models/entry.coffee | 25 +++++++++++++++++++ .../templates/pages/help_tmpl.coffee | 17 +++++++++++-- assets/stylesheets/components/_content.scss | 6 +++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/assets/javascripts/app/searcher.coffee b/assets/javascripts/app/searcher.coffee index d7e12097..36814add 100644 --- a/assets/javascripts/app/searcher.coffee +++ b/assets/javascripts/app/searcher.coffee @@ -120,6 +120,7 @@ class app.Searcher EMPTY_STRING = '' ELLIPSIS = '...' + STRING = 'string' @normalizeString: (string) -> string @@ -209,9 +210,15 @@ class app.Searcher matcher = @matcher for [0...@chunkSize()] value = @data[@cursor][@attr] - valueLength = value.length - if score = matcher() - @addResult @data[@cursor], score + if value.split # string + valueLength = value.length + @addResult(@data[@cursor], score) if score = matcher() + else # array + score = 0 + for value in @data[@cursor][@attr] + valueLength = value.length + score = Math.max(score, matcher() || 0) + @addResult(@data[@cursor], score) if score > 0 @cursor++ return diff --git a/assets/javascripts/models/entry.coffee b/assets/javascripts/models/entry.coffee index 9113f9d8..51bd6172 100644 --- a/assets/javascripts/models/entry.coffee +++ b/assets/javascripts/models/entry.coffee @@ -6,6 +6,7 @@ class app.models.Entry extends app.Model constructor: -> super @text = app.Searcher.normalizeString(@name) + @text = applyAliases(@text) fullPath: -> @doc.fullPath if @isIndex() then '' else @path @@ -32,3 +33,27 @@ class app.models.Entry extends app.Model loadFile: (onSuccess, onError) -> app.db.load(@, onSuccess, onError) + + applyAliases = (string) -> + if ALIASES.hasOwnProperty(string) + return [string, ALIASES[string]] + else + words = string.split('.') + for word, i in words when ALIASES.hasOwnProperty(word) + words[i] = ALIASES[word] + return [string, words.join('.')] + return string + + @ALIASES = ALIASES = + 'angular.js': 'ng' + 'backbone': 'bb' + 'c++': 'cpp' + 'coffeescript': 'cs' + 'javascript': 'js' + 'jquery': '$' + 'knockout.js': 'ko' + 'lodash': '_' + 'markdown': 'md' + 'postgresql': 'pg' + 'sass': 'scss' + 'underscore.js': '_' diff --git a/assets/javascripts/templates/pages/help_tmpl.coffee b/assets/javascripts/templates/pages/help_tmpl.coffee index 62458520..ea7fee8a 100644 --- a/assets/javascripts/templates/pages/help_tmpl.coffee +++ b/assets/javascripts/templates/pages/help_tmpl.coffee @@ -7,13 +7,16 @@ app.templates.helpPage = """

The search is case-insensitive and supports fuzzy matching (for queries longer than two characters). - For example, searching bgcp brings up background-clip. + For example, searching bgcp brings up background-clip.
+ Abbreviations are also supported (full list below). + For example, $ is an alias for jQuery.

@@ -102,4 +105,14 @@ app.templates.helpPage = """

Tip: If the cursor is no longer in the search field, press backspace or - continue to type and it will refocus the search field and start showing new results. """ + continue to type and it will refocus the search field and start showing new results. + +

Abbreviations

+

Feel free to suggest new abbreviations on GitHub. + + +
Word + Alias + #{("
#{key}#{value}" for key, value of app.models.Entry.ALIASES).join('')} +
+""" diff --git a/assets/stylesheets/components/_content.scss b/assets/stylesheets/components/_content.scss index 3fc014aa..e243e9f3 100644 --- a/assets/stylesheets/components/_content.scss +++ b/assets/stylesheets/components/_content.scss @@ -381,6 +381,12 @@ @extend %label; } +// +// Abbreviations +// + +._abbreviations td { @extend %code; } + // // Utilities //