Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit ed19add

Browse files
committed
Search engine optimization
1 parent 736634b commit ed19add

File tree

7 files changed

+100
-52
lines changed

7 files changed

+100
-52
lines changed

app/components/searchresult/search_result_body.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def gotoslug slug, sectionname, pageid
2121

2222
def highlight(text, search_string)
2323
keywords = search_string.strip.split(" ").compact.uniq
24-
matcher = Regexp.new( '(' + keywords.join("|") + ')' )
24+
#matcher = Regexp.new( '(' + keywords.join("|") + ')' )
25+
matcher = Regexp.new('\\b(' + keywords.join("|") + ')\\b')
2526
highlighted = text.gsub(matcher) { |match| "<a class='ui teal label'>#{match}</a>" }
2627
return highlighted
2728
end
@@ -81,6 +82,8 @@ def highlight(text, search_string)
8182

8283
end
8384

85+
86+
8487
DIV(dangerously_set_inner_HTML: { __html: highlight(resulthtmlparagraph, result[:matchingwords]) })
8588

8689
BR()

app/components/shared/md_converter.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ def process
3838
`renderer.codespan = #{lambda {|code| on_codespan(code)} }`
3939
`renderer.blockquote = #{lambda {|quote| on_blockquote(quote)} }`
4040
`renderer.table = #{lambda {|header, body| on_table(header, body)} }`
41+
`renderer.list = #{lambda {|body, ordered| on_list(body, ordered)} }`
4142

4243
`Marked.setOptions({ renderer: renderer })`
4344
@html = `Marked(#{@md})`
4445
end
4546

4647
def on_table header, body
47-
"<table class='ui celled table'><thead class>#{header}</thead><tbody class>#{body}</tbody></table>"
48+
return_html = "<table class='ui celled table'><thead class>#{header}</thead><tbody class>#{body}</tbody></table>"
49+
@headings[@headings_index-1][:paragraphs] << return_html
50+
return_html
4851
end
4952

5053
def on_code code, lang
@@ -53,8 +56,9 @@ def on_code code, lang
5356
cb[:code] = code
5457
cb[:lang] = lang
5558
@code_blocks << cb
56-
@headings[@headings_index-1][:paragraphs] << "<pre><code class='lang-#{lang} hljs'>#{ cb[:html] }</code></pre>"
57-
"<pre><code class='lang-#{lang} hljs'>#{ cb[:html] }</code></pre>"
59+
return_html = "<pre><code class='lang-#{lang} hljs'>#{ cb[:html] }</code></pre>"
60+
@headings[@headings_index-1][:paragraphs] << return_html
61+
return_html
5862
end
5963

6064
def on_codespan code
@@ -86,8 +90,15 @@ def on_heading text, level
8690
end
8791

8892
def on_paragraph text
89-
@headings[@headings_index-1][:paragraphs] << "<p>#{text}</p>"
90-
"<p>#{text}</p>"
93+
return_html = "<p>#{text}</p>"
94+
@headings[@headings_index-1][:paragraphs] << return_html
95+
return_html
96+
end
97+
98+
def on_list body, ordered
99+
return_html = "<ul>#{body}</ul>"
100+
@headings[@headings_index-1][:paragraphs] << return_html
101+
return_html
91102
end
92103

93104
end

app/components/shared/page_toc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PageToc < Hyperloop::Component
1010

1111
before_mount do
1212

13-
if NavigationStore.accordionindex < 0
13+
if (NavigationStore.accordionindex < 0)
1414
NavigationStore.mutate.accordionindex -1
1515
end
1616
end

app/stores/search_engine_store.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def build_lunr_section_searchindex section
2020
end
2121
SearchEngineStore.mutate.lunr_section_searchindex[section] = `lunrsectionindex`
2222

23+
# test = SearchEngineStore.lunr_section_searchindex[section].map{|element| Hash.new(element)}
24+
# puts test
2325
end
2426

2527
def search_withlunr section

app/stores/section_store.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ class SectionStore < Hyperloop::Store
55
state loaded: false
66

77
def initialize pages, sectionname, sectionid
8-
#puts "SectionStore is starting now"
8+
99
@sectionname = sectionname
1010
@sectionid = sectionid
1111
@pages = pages
12-
#mutate.current_sectionname_lunrsectionindex sectionname
13-
`lunrsectionindex = [];`
14-
12+
1513
load_and_convert_pages
1614
mutate.current_page @pages[0]
1715

@@ -91,17 +89,26 @@ def get page
9189
end
9290
end
9391

94-
def build_lunr_page_searchindex page
92+
def purify_text text
93+
puretext = text.gsub(/<\/?[^>]*>/, "")
94+
.gsub(/\s+/, ' ')
95+
.gsub(/&#39;/, ' ')
96+
.gsub(/[^\w\d\_\?]/, ' ')
97+
.strip
98+
return puretext
99+
end
100+
95101

102+
def build_lunr_page_searchindex page
96103

97104
`lunrpageindex=[]`
98105

99106
page[:headings].each_with_index do |heading, index|
100107

101108
lunrheadingindex = `{
102109
"headingid": #{heading[:id]},
103-
"headingname": #{heading[:text]},
104-
"text": #{heading[:paragraphs].join(' ').gsub(/<\/?[^>]*>/, "")}
110+
"headingname": #{purify_text(heading[:text])},
111+
"text": #{purify_text(heading[:paragraphs].join(' '))}
105112
}`
106113

107114

dist/app.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16222,22 +16222,22 @@ Opal.modules["components/shared/search_result_modal"] = function(Opal) {
1622216222

1622316223
/* Generated by Opal 0.10.5 */
1622416224
Opal.modules["components/shared/md_converter"] = function(Opal) {
16225-
function $rb_plus(lhs, rhs) {
16226-
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs + rhs : lhs['$+'](rhs);
16227-
}
1622816225
function $rb_minus(lhs, rhs) {
1622916226
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs - rhs : lhs['$-'](rhs);
1623016227
}
16228+
function $rb_plus(lhs, rhs) {
16229+
return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs + rhs : lhs['$+'](rhs);
16230+
}
1623116231
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $klass = Opal.klass, $hash2 = Opal.hash2;
1623216232

16233-
Opal.add_stubs(['$gsub', '$process', '$lambda', '$on_heading', '$on_paragraph', '$on_code', '$on_codespan', '$on_blockquote', '$on_table', '$[]=', '$highlight', '$<<', '$[]', '$downcase', '$+', '$-']);
16233+
Opal.add_stubs(['$gsub', '$process', '$lambda', '$on_heading', '$on_paragraph', '$on_code', '$on_codespan', '$on_blockquote', '$on_table', '$on_list', '$<<', '$[]', '$-', '$[]=', '$highlight', '$downcase', '$+']);
1623416234
return (function($base, $super) {
1623516235
function $MdConverter(){};
1623616236
var self = $MdConverter = $klass($base, $super, 'MdConverter', $MdConverter);
1623716237

16238-
var def = self.$$proto, $scope = self.$$scope, TMP_1, TMP_2, TMP_3, TMP_4, TMP_11, TMP_12, TMP_13, TMP_14, TMP_15, TMP_16, TMP_17, TMP_18;
16238+
var def = self.$$proto, $scope = self.$$scope, TMP_1, TMP_2, TMP_3, TMP_4, TMP_12, TMP_13, TMP_14, TMP_15, TMP_16, TMP_17, TMP_18, TMP_19, TMP_20;
1623916239

16240-
def.html = def.code_blocks = def.headings = def.md = def.sectionid = def.pageid = def.headings_index = def.sectionname = def.pagename = nil;
16240+
def.html = def.code_blocks = def.headings = def.md = def.headings_index = def.sectionid = def.pageid = def.sectionname = def.pagename = nil;
1624116241
Opal.defn(self, '$initialize', TMP_1 = function $$initialize(md, sectionname, sectionid, pageid, pagename) {
1624216242
var self = this;
1624316243

@@ -16271,8 +16271,8 @@ Opal.modules["components/shared/md_converter"] = function(Opal) {
1627116271
return self.headings;
1627216272
}, TMP_4.$$arity = 0);
1627316273

16274-
Opal.defn(self, '$process', TMP_11 = function $$process() {
16275-
var $a, $b, TMP_5, $c, TMP_6, $d, TMP_7, $e, TMP_8, $f, TMP_9, $g, TMP_10, self = this;
16274+
Opal.defn(self, '$process', TMP_12 = function $$process() {
16275+
var $a, $b, TMP_5, $c, TMP_6, $d, TMP_7, $e, TMP_8, $f, TMP_9, $g, TMP_10, $h, TMP_11, self = this;
1627616276

1627716277
var renderer = new Marked.Renderer();
1627816278
renderer.heading = ($a = ($b = self).$lambda, $a.$$p = (TMP_5 = function(text, level){var self = TMP_5.$$s || this;
@@ -16293,49 +16293,56 @@ if (quote == null) quote = nil;
1629316293
renderer.table = ($a = ($g = self).$lambda, $a.$$p = (TMP_10 = function(header, body){var self = TMP_10.$$s || this;
1629416294
if (header == null) header = nil;if (body == null) body = nil;
1629516295
return self.$on_table(header, body)}, TMP_10.$$s = self, TMP_10.$$arity = 2, TMP_10), $a).call($g);
16296+
renderer.list = ($a = ($h = self).$lambda, $a.$$p = (TMP_11 = function(body, ordered){var self = TMP_11.$$s || this;
16297+
if (body == null) body = nil;if (ordered == null) ordered = nil;
16298+
return self.$on_list(body, ordered)}, TMP_11.$$s = self, TMP_11.$$arity = 2, TMP_11), $a).call($h);
1629616299
Marked.setOptions({ renderer: renderer });
1629716300
return self.html = Marked(self.md);
16298-
}, TMP_11.$$arity = 0);
16301+
}, TMP_12.$$arity = 0);
1629916302

16300-
Opal.defn(self, '$on_table', TMP_12 = function $$on_table(header, body) {
16301-
var self = this;
16303+
Opal.defn(self, '$on_table', TMP_13 = function $$on_table(header, body) {
16304+
var self = this, return_html = nil;
1630216305

16303-
return "<table class='ui celled table'><thead class>" + (header) + "</thead><tbody class>" + (body) + "</tbody></table>";
16304-
}, TMP_12.$$arity = 2);
16306+
return_html = "<table class='ui celled table'><thead class>" + (header) + "</thead><tbody class>" + (body) + "</tbody></table>";
16307+
self.headings['$[]']($rb_minus(self.headings_index, 1))['$[]']("paragraphs")['$<<'](return_html);
16308+
return return_html;
16309+
}, TMP_13.$$arity = 2);
1630516310

16306-
Opal.defn(self, '$on_code', TMP_13 = function $$on_code(code, lang) {
16307-
var self = this, cb = nil;
16311+
Opal.defn(self, '$on_code', TMP_14 = function $$on_code(code, lang) {
16312+
var self = this, cb = nil, return_html = nil;
1630816313

1630916314
cb = $hash2([], {});
1631016315
cb['$[]=']("html", self.$highlight(code, lang));
1631116316
cb['$[]=']("code", code);
1631216317
cb['$[]=']("lang", lang);
1631316318
self.code_blocks['$<<'](cb);
16314-
return "<pre><code class='lang-" + (lang) + " hljs'>" + (cb['$[]']("html")) + "</code></pre>";
16315-
}, TMP_13.$$arity = 2);
16319+
return_html = "<pre><code class='lang-" + (lang) + " hljs'>" + (cb['$[]']("html")) + "</code></pre>";
16320+
self.headings['$[]']($rb_minus(self.headings_index, 1))['$[]']("paragraphs")['$<<'](return_html);
16321+
return return_html;
16322+
}, TMP_14.$$arity = 2);
1631616323

16317-
Opal.defn(self, '$on_codespan', TMP_14 = function $$on_codespan(code) {
16324+
Opal.defn(self, '$on_codespan', TMP_15 = function $$on_codespan(code) {
1631816325
var self = this;
1631916326

1632016327
return "<code class='inline-codespan'>" + (code) + "</code>";
16321-
}, TMP_14.$$arity = 1);
16328+
}, TMP_15.$$arity = 1);
1632216329

16323-
Opal.defn(self, '$highlight', TMP_15 = function $$highlight(code, lang) {
16330+
Opal.defn(self, '$highlight', TMP_16 = function $$highlight(code, lang) {
1632416331
var self = this;
1632516332

1632616333
if (lang == null) {
1632716334
lang = nil;
1632816335
}
1632916336
return hljs.highlightAuto(code).value;
16330-
}, TMP_15.$$arity = -2);
16337+
}, TMP_16.$$arity = -2);
1633116338

16332-
Opal.defn(self, '$on_blockquote', TMP_16 = function $$on_blockquote(quote) {
16339+
Opal.defn(self, '$on_blockquote', TMP_17 = function $$on_blockquote(quote) {
1633316340
var self = this;
1633416341

1633516342
return "<div class='ui cards'><div class='ui card fluid'><div class='content'>" + (quote) + "</div></div></div>";
16336-
}, TMP_16.$$arity = 1);
16343+
}, TMP_17.$$arity = 1);
1633716344

16338-
Opal.defn(self, '$on_heading', TMP_17 = function $$on_heading(text, level) {
16345+
Opal.defn(self, '$on_heading', TMP_18 = function $$on_heading(text, level) {
1633916346
var self = this, heading = nil;
1634016347

1634116348
heading = $hash2([], {});
@@ -16350,14 +16357,23 @@ if (header == null) header = nil;if (body == null) body = nil;
1635016357
self.headings_index = $rb_plus(self.headings_index, 1);
1635116358
self.headings['$<<'](heading);
1635216359
return "<h" + (level) + " class='doc_h" + (level) + " chapteranchor' id='" + (heading['$[]']("slug")) + "'>" + (text) + "</h" + (level) + ">";
16353-
}, TMP_17.$$arity = 2);
16360+
}, TMP_18.$$arity = 2);
1635416361

16355-
return (Opal.defn(self, '$on_paragraph', TMP_18 = function $$on_paragraph(text) {
16356-
var self = this;
16362+
Opal.defn(self, '$on_paragraph', TMP_19 = function $$on_paragraph(text) {
16363+
var self = this, return_html = nil;
16364+
16365+
return_html = "<p>" + (text) + "</p>";
16366+
self.headings['$[]']($rb_minus(self.headings_index, 1))['$[]']("paragraphs")['$<<'](return_html);
16367+
return return_html;
16368+
}, TMP_19.$$arity = 1);
16369+
16370+
return (Opal.defn(self, '$on_list', TMP_20 = function $$on_list(body, ordered) {
16371+
var self = this, return_html = nil;
1635716372

16358-
self.headings['$[]']($rb_minus(self.headings_index, 1))['$[]']("paragraphs")['$<<']("<p>" + (text) + "</p>");
16359-
return "<p>" + (text) + "</p>";
16360-
}, TMP_18.$$arity = 1), nil) && 'on_paragraph';
16373+
return_html = "<ul>" + (body) + "</ul>";
16374+
self.headings['$[]']($rb_minus(self.headings_index, 1))['$[]']("paragraphs")['$<<'](return_html);
16375+
return return_html;
16376+
}, TMP_20.$$arity = 2), nil) && 'on_list';
1636116377
})($scope.base, null)
1636216378
};
1636316379

@@ -16746,7 +16762,7 @@ Opal.modules["components/shared/page_toc"] = function(Opal) {
1674616762

1674716763
($a = ($b = self).$before_mount, $a.$$p = (TMP_1 = function(){var self = TMP_1.$$s || this, $c;
1674816764

16749-
if ((($c = $rb_lt($scope.get('NavigationStore').$accordionindex(), 0)) !== nil && $c != null && (!$c.$$is_boolean || $c == true))) {
16765+
if ((($c = ($rb_lt($scope.get('NavigationStore').$accordionindex(), 0))) !== nil && $c != null && (!$c.$$is_boolean || $c == true))) {
1675016766
return $scope.get('NavigationStore').$mutate().$accordionindex(-1)
1675116767
} else {
1675216768
return nil
@@ -17308,7 +17324,7 @@ Opal.modules["components/searchresult/search_result_body"] = function(Opal) {
1730817324
var $a, $b, TMP_2, self = this, keywords = nil, matcher = nil, highlighted = nil;
1730917325

1731017326
keywords = search_string.$strip().$split(" ").$compact().$uniq();
17311-
matcher = $scope.get('Regexp').$new($rb_plus($rb_plus("(", keywords.$join("|")), ")"));
17327+
matcher = $scope.get('Regexp').$new($rb_plus($rb_plus("\\b(", keywords.$join("|")), ")\\b"));
1731217328
highlighted = ($a = ($b = text).$gsub, $a.$$p = (TMP_2 = function(match){var self = TMP_2.$$s || this;
1731317329
if (match == null) match = nil;
1731417330
return "<a class='ui teal label'>" + (match) + "</a>"}, TMP_2.$$s = self, TMP_2.$$arity = 1, TMP_2), $a).call($b, matcher);
@@ -17630,7 +17646,7 @@ Opal.modules["stores/section_store"] = function(Opal) {
1763017646
}
1763117647
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $klass = Opal.klass, $hash2 = Opal.hash2;
1763217648

17633-
Opal.add_stubs(['$state', '$load_and_convert_pages', '$current_page', '$mutate', '$[]', '$loaded', '$current_anchor', '$private', '$each', '$get', '$+', '$puts', '$ok?', '$new', '$body', '$[]=', '$headings', '$code_blocks', '$html', '$edit_url', '$build_lunr_page_searchindex', '$raw_url', '$-', '$==', '$each_with_index', '$gsub', '$join']);
17649+
Opal.add_stubs(['$state', '$load_and_convert_pages', '$current_page', '$mutate', '$[]', '$loaded', '$current_anchor', '$private', '$each', '$get', '$+', '$puts', '$ok?', '$new', '$body', '$[]=', '$headings', '$code_blocks', '$html', '$edit_url', '$build_lunr_page_searchindex', '$raw_url', '$-', '$==', '$each_with_index', '$strip', '$gsub', '$join']);
1763417650
return (function($base, $super) {
1763517651
function $SectionStore(){};
1763617652
var self = $SectionStore = $klass($base, $super, 'SectionStore', $SectionStore);
@@ -17646,7 +17662,6 @@ Opal.modules["stores/section_store"] = function(Opal) {
1764617662
self.sectionname = sectionname;
1764717663
self.sectionid = sectionid;
1764817664
self.pages = pages;
17649-
lunrsectionindex = [];
1765017665
self.$load_and_convert_pages();
1765117666
return self.$mutate().$current_page(self.pages['$[]'](0));
1765217667
}, TMP_1.$$arity = 3);
@@ -17748,7 +17763,7 @@ if (heading == null) heading = nil;if (index == null) index = nil;
1774817763
lunrheadingindex = {
1774917764
"headingid": heading['$[]']("id"),
1775017765
"headingname": heading['$[]']("text"),
17751-
"text": heading['$[]']("paragraphs").$join(" ").$gsub(/<\/?[^>]*>/, "")
17766+
"text": heading['$[]']("paragraphs").$join(" ").$gsub(/<\/?[^>]*>/, "").$gsub(/\s+/, " ").$gsub(/&#39;/, " ").$gsub(/::/, " :: ").$gsub(/[^a-zA-Z]+/, " ").$strip()
1775217767
};
1775317768
return lunrpageindex.push(lunrheadingindex);;}, TMP_14.$$s = self, TMP_14.$$arity = 2, TMP_14), $a).call($b);
1775417769
lunrpageindex = lunrpageindex;

dist/lunar_search_helpers.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ function Lunrindex(data) {
1818
this.add(item)
1919
}, this);
2020

21-
this.pipeline.remove(lunr.stemmer);
22-
this.pipeline.remove(lunr.stopWordFilter)
21+
//this.pipeline.remove(lunr.stemmer);
22+
//this.searchPipeline.remove(lunr.stemmer);
23+
//this.pipeline.remove(lunr.stopWordFilter);
24+
2325
})
2426

2527
return index;
@@ -50,6 +52,12 @@ function Searchquery(index, data, querystring){
5052

5153
var lunrresults = index.search(querystring);
5254

55+
// index.query(function (q) {
56+
// q.term(term, { boost: 100 }) // exact match
57+
// q.term(term, { usePipeline: false, wildcard: lunr.query.wildcard.TRAILING, boost: 10 }) // prefix match, no stemmer
58+
// q.term(term, { usePipeline: false, editDistance: 1, boost: 1 }) // fuzzy matching
59+
// })
60+
5361

5462
// searchresults wanted format
5563
//[{"sectionid"=>0, "sectionname"=>"docs", "pageid"=>0, "pagename"=>"Hyperloop Components", "headingid"=>0, "headingslug"=>"components-dsl-overview", "headingname"=>"Components DSL Overview", "text"=>"Hyperloop Components are implemented ..."}]
@@ -81,9 +89,11 @@ function Searchquery(index, data, querystring){
8189
// if (fieldName=="text") { positionintext = fieldNameKey.position}
8290
})
8391
})
84-
92+
8593
searchresultitem = {"ref": result.ref,
8694
"nbresults": nbresults,
95+
// "positioninheadingname": positioninheadingname,
96+
// "positionintext": positionintext,
8797
"matchingwords": matchingwords.replace(/[^\w\s]|/g, "")}
8898

8999
searchresults.push(searchresultitem)

0 commit comments

Comments
 (0)