Skip to content

Commit

Permalink
Merge pull request #2344 from djpowers/include-search-aliases-docs-json
Browse files Browse the repository at this point in the history
Add field to docs.json denoting available aliases
  • Loading branch information
simon04 authored Nov 17, 2024
2 parents 22abb7d + 050022b commit 002eb72
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
1 change: 1 addition & 0 deletions assets/javascripts/app/config.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
app.config = {
db_filename: 'db.json',
default_docs: <%= App.default_docs.to_json %>,
docs_aliases: <%= App.docs_aliases.to_json %>,
docs_origin: '<%= App.docs_origin %>',
env: '<%= App.environment %>',
history_cache_size: 10,
Expand Down
45 changes: 5 additions & 40 deletions assets/javascripts/models/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,23 @@

app.models.Entry = class Entry extends app.Model {
static applyAliases(string) {
if (Entry.ALIASES.hasOwnProperty(string)) {
return [string, Entry.ALIASES[string]];
const aliases = app.config.docs_aliases;
if (aliases.hasOwnProperty(string)) {
return [string, aliases[string]];
} else {
const words = string.split(".");
for (let i = 0; i < words.length; i++) {
var word = words[i];
if (Entry.ALIASES.hasOwnProperty(word)) {
words[i] = Entry.ALIASES[word];
if (aliases.hasOwnProperty(word)) {
words[i] = aliases[word];
return [string, words.join(".")];
}
}
}
return string;
}

static ALIASES = {
angular: "ng",
"angular.js": "ng",
"backbone.js": "bb",
"c++": "cpp",
coffeescript: "cs",
crystal: "cr",
elixir: "ex",
javascript: "js",
julia: "jl",
jquery: "$",
"knockout.js": "ko",
kubernetes: "k8s",
less: "ls",
lodash: "_",
löve: "love",
marionette: "mn",
markdown: "md",
matplotlib: "mpl",
modernizr: "mdr",
"moment.js": "mt",
openjdk: "java",
nginx: "ngx",
numpy: "np",
pandas: "pd",
postgresql: "pg",
python: "py",
"ruby.on.rails": "ror",
ruby: "rb",
rust: "rs",
sass: "scss",
tensorflow: "tf",
typescript: "ts",
"underscore.js": "_",
};
// Attributes: name, type, path

constructor() {
super(...arguments);
this.text = Entry.applyAliases(app.Searcher.normalizeString(this.name));
Expand Down
36 changes: 36 additions & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ class App < Sinatra::Application
set :default_docs, %w(css dom html http javascript)
set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json')

set :docs_aliases, {
'angular' => 'ng',
'angular.js' => 'ng',
'backbone.js' => 'bb',
'c++' => 'cpp',
'coffeescript' => 'cs',
'crystal' => 'cr',
'elixir' => 'ex',
'javascript' => 'js',
'julia' => 'jl',
'jquery' => '$',
'knockout.js' => 'ko',
'kubernetes' => 'k8s',
'less' => 'ls',
'lodash' => '_',
'löve' => 'love',
'marionette' => 'mn',
'markdown' => 'md',
'matplotlib' => 'mpl',
'modernizr' => 'mdr',
'moment.js' => 'mt',
'openjdk' => 'java',
'nginx' => 'ngx',
'numpy' => 'np',
'pandas' => 'pd',
'postgresql' => 'pg',
'python' => 'py',
'ruby.on.rails' => 'ror',
'ruby' => 'rb',
'rust' => 'rs',
'sass' => 'scss',
'tensorflow' => 'tf',
'typescript' => 'ts',
'underscore.js' => '_',
}

set :csp, false

require 'docs'
Expand Down
1 change: 1 addition & 0 deletions lib/docs/core/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def as_json
if doc.options[:attribution].is_a?(String)
json[:attribution] = doc.options[:attribution].strip
end
json[:alias] = App.docs_aliases[json["slug"].try(:to_sym)]
result << json
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/files/docs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020},{"name":"HTML","slug":"html~5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647},{"name":"HTML","slug":"html~4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477}]
[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507,"alias":null},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128,"alias":null},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020,"alias":null},{"name":"HTML","slug":"html~5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647,"alias":null},{"name":"HTML","slug":"html~4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646,"alias":null},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083,"alias":null},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477,"alias":"js"}]
2 changes: 1 addition & 1 deletion test/lib/docs/core/manifest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ManifestTest < Minitest::Spec
it "includes the doc's meta representation" do
json = manifest.as_json
assert_equal 1, json.length
assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\"}", json[0].to_s
assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\", \"alias\"=>nil}", json[0].to_s
end
end

Expand Down

0 comments on commit 002eb72

Please sign in to comment.