Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved search hits #439

Merged
merged 7 commits into from Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions _apidefinitions/broadcast-ops.md
Expand Up @@ -19,6 +19,9 @@ exclude: true
{% if op.disabled %}
<li class="warning"><strong><small>Disabled</small></strong></li>
{% endif %}
{% assign keywords = op.name | keywordify | escape %}
{% assign search_url = '/search/?q=' | append: keywords | split: '_' | join: ' ' %}
<li class="info"><strong><small><a href="{{ search_url | relative_url }}">Related <i class="fas fa-search fa-xs"></i></a></small></strong></li>
</ul>
<h4 id="broadcast_ops_{{ op.name | slug }}">
<code>{{op.name}}</code>
Expand Down
33 changes: 19 additions & 14 deletions _glossary/index.html
Expand Up @@ -11,18 +11,23 @@
<p>More terms and information can be found on the steemit.com <a href="https://steemit.com/faq.html">FAQ</a>.</p>

<section class="row">
<section class="row">
<dl class="dl-horizontal glossary">
{% for sections in site.data.glossary.index %}
<h3 id="{{ sections.id }}">{{sections.name}}</h3>
{% for entry in sections.items %}
<section id="{{entry.term | slugify}}">
<dt>{{entry.term}}</dt>
<dd>{{entry.def}}</dd>
</section>
{% endfor %}
<hr/>
{% endfor %}
</dl>
</section>
<section class="row">
<dl class="dl-horizontal glossary">
{% for sections in site.data.glossary.index %}
<h3 id="{{ sections.id }}">{{sections.name}}</h3>
{% for entry in sections.items %}
<section id="{{entry.term | slugify}}">
<dt>
{% assign keywords = entry.term | keywordify | escape %}
{% assign search_url = '/search/?q=' | append: keywords %}
<a href="{{ search_url | relative_url }}"><i class="fas fa-search fa-xs"></i></a>
{{entry.term}}
</dt>
<dd>{{entry.def}}</dd>
</section>
{% endfor %}
<hr/>
{% endfor %}
</dl>
</section>
</section>
3 changes: 3 additions & 0 deletions _includes/api-template.html
Expand Up @@ -11,6 +11,9 @@
{% if method.disabled %}
<li class="warning"><strong><small>Disabled</small></strong></li>
{% endif %}
{% assign keywords = method.api_method | split: '.' | last | split: '_' | join: ' ' | keywordify | escape %}
{% assign search_url = '/search/?q=' | append: keywords %}
<li class="info"><strong><small><a href="{{ search_url | relative_url }}">Related <i class="fas fa-search fa-xs"></i></a></small></strong></li>

</ul>
<h4 id="{{ method.api_method | slug }}">{{method.api_method}} <a href="#{{ method.api_method | slug}}"><i class="fas fa-link fa-xs"></i></a></h4>
Expand Down
52 changes: 52 additions & 0 deletions _plugins/keywordify_filter.rb
@@ -0,0 +1,52 @@
module Jekyll
module KeywordifyFilter
SEP = /[^a-z0-9_]+/i
STOP_WORDS = %w(i me my myself we our ours ourselves you your yours yourself
yourselves he him his himself she her hers herself it its itself they them
their theirs themselves what which who whom this that these those am is
are was were be been being have has had having do does did doing a an the
and but if or because as until while of at by for with about against
between into through during before after above below to from up down in
out on off over under again further then once here there when where why
how all any both each few more most other some such no nor not only own
same so than too very s t can will just don should now)

def keywordify(object, mode = 'subtract', extra_keywords = nil)
output = []

case object
when Hash
object.each do |k, v|
output += keywordify(k).split(SEP)
output += keywordify(v).split(SEP)
end
when Array
object.each do |k, v|
output += keywordify(v).split(SEP)
end
when NilClass
# no-op
else
output += object.inspect.split(SEP)
end

output = output.map(&:strip).map(&:downcase)
extra_keywords = extra_keywords.to_s.split(SEP).map(&:strip).map(&:downcase)

case mode
when 'subtract' then output -= extra_keywords
when 'add' then output += extra_keywords
else; raise "Unknown mode: #{mode}"
end

(output - STOP_WORDS).uniq.compact.join(' ').strip
end
end
end

Liquid::Template.register_filter(Jekyll::KeywordifyFilter)

# USAGE:
# {{object | keywordify}}
# {{object | keywordify 'subtract', 'some words to subtract'}}
# {{object | keywordify 'add', 'additional words to add'}}
1 change: 1 addition & 0 deletions js/search.js
Expand Up @@ -83,6 +83,7 @@
this.field("category");
this.field("url");
this.field("content");
this.field("keywords");
});

var query = decodeURIComponent((getQueryVariable("q") || "").replace(/\+/g, "%20")),
Expand Down
85 changes: 67 additions & 18 deletions search.html
Expand Up @@ -2,6 +2,9 @@
title: Search
sitemap: false
permalink: /search/
data_defined:
- apidefinitions
- broadcastops
---

<div class="search-page">
Expand All @@ -14,25 +17,71 @@ <h2>Search Results</h2>
<script>
window.data = {
{% for collection in site.collections %}
{% for item in collection.docs %}
{% if item.title %}
{% unless item.excluded_in_search %}
{% if added %},{% endif %}
{% assign added = false %}
"{{ item.id | strip | slugify }}": {
"id": "{{ item.id | strip | slugify }}",
"title": "{{ item.title | strip | xml_escape }}",
"category": "{{ collection.label | strip | xml_escape }}",
"description": "{{ item.description | strip | xml_escape }}",
"type": "{{ item.type | strip | xml_escape }}",
"url": "{{ item.url | strip }}",
"content": {{ item.content | strip_html | replace_regex: "[\s/\n]+"," " | strip | jsonify }}
}
{% assign added = true %}
{% endunless %}
{% endif %}
{% endfor %}
{% unless page.data_defined contains collection.label %}
{% for item in collection.docs %}
{% if item.title %}
{% unless item.excluded_in_search %}
{% if added %},{% endif %}
{% assign added = false %}
"{{ item.id | strip | slugify }}": {
"id": "{{ item.id | strip | slugify }}",
"title": "{{ item.title | strip | xml_escape }}",
"category": "{{ collection.label | strip | xml_escape }}",
"description": "{{ item.description | strip | xml_escape }}",
"type": "{{ item.type | strip | xml_escape }}",
"url": "{{ item.url | strip }}",
"content": {{ item.content | strip_html | replace_regex: "[\s/\n]+"," " | strip | jsonify }}
}
{% assign added = true %}
{% endunless %}
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}

// # data_defined is as follows

{% for api_defs in site.data.apidefinitions %}
{% for sections in api_defs[1] %}
{% for method in sections.methods %}
{% if added %},{% endif %}
{% assign added = false %}
{% assign api_defs_url = '../apidefinitions#' | append: method.api_method %}

"{{ method.api_method | strip | slugify }}": {
"id": "{{ method.api_method | strip | slugify }}",
"title": "{{ method.api_method | strip | xml_escape }}",
"category": "apidefinitions",
"description": "{{ method.api_method | strip | xml_escape }}",
"type": "method",
"url": "{{ api_defs_url | relative_url }}",
"content": {{ method.purpose | liquify | markdownify | strip_html | replace_regex: "[\s/\n]+"," " | strip | jsonify }},
"keywords": "{{ method | keywordify: 'subtract', method.purpose }}"
}
{% assign added = true %}
{% endfor %}
{% endfor %}
{% endfor %}

{% for sections in site.data.apidefinitions.broadcast_ops %}
{% for op in sections.ops %}
{% if added %},{% endif %}
{% assign added = false %}
{% assign api_defs_url = '../apidefinitions#broadcast_ops_' | append: op.name %}

"broadcast_ops_{{ op.name | strip | slugify }}": {
"id": "broadcast_ops_{{ op.name | strip | slugify }}",
"title": "{{ op.name | strip | xml_escape }}",
"category": "broadcastops",
"description": "{{ op.name | strip | xml_escape }}",
"type": "op",
"url": "{{ api_defs_url | relative_url }}",
"content": {{ op.purpose | liquify | markdownify | strip_html | replace_regex: "[\s/\n]+"," " | strip | jsonify }},
"keywords": "{{ op | keywordify: 'subtract', op.purpose }}"
}
{% assign added = true %}
{% endfor %}
{% endfor %}
};
</script>
{% if jekyll.environment == 'production' %}
Expand Down