From 3fb63f302d98207fa8d98cae8b7f0ea65055d860 Mon Sep 17 00:00:00 2001 From: Nick Garlis Date: Fri, 5 Jan 2018 03:22:33 +0200 Subject: [PATCH] Minimize JSON data (#1449) * Remove Lunr trimmer & bring back colons * Add Greek Stemmer * Translate search_placeholder_text and results_found to Greek * Minimize JSON data * Truncate Words * Move store variable into a new file * Move Lunr files into a new folder * Add defer to lunr scripts * Add search_full_content switch --- _config.yml | 3 +- _includes/scripts.html | 5 +-- assets/js/{ => lunr}/lunr-en.js | 55 +++++++------------------------- assets/js/{ => lunr}/lunr-gr.js | 53 +++++++----------------------- assets/js/lunr/lunr-store.js | 36 +++++++++++++++++++++ assets/js/{ => lunr}/lunr.min.js | 0 docs/_config.yml | 1 + test/_config.yml | 3 +- 8 files changed, 67 insertions(+), 89 deletions(-) rename assets/js/{ => lunr}/lunr-en.js (60%) rename assets/js/{ => lunr}/lunr-gr.js (91%) create mode 100644 assets/js/lunr/lunr-store.js rename assets/js/{ => lunr}/lunr.min.js (100%) diff --git a/_config.yml b/_config.yml index d0bda8de..1a2694a1 100644 --- a/_config.yml +++ b/_config.yml @@ -8,7 +8,7 @@ minimal_mistakes_skin : "default" # "air", "aqua", "contrast", "dark", "dirt", "neon", "mint", "plum", "sunrise" # Site Settings -locale : "en" +locale : "en-US" title : "Site Title" title_separator : "-" name : "Your Name" @@ -52,6 +52,7 @@ reCaptcha: atom_feed: path : # blank (default) uses feed.xml search : # true, false (default) +search_full_content : # true, false (default) # SEO Related google_site_verification : diff --git a/_includes/scripts.html b/_includes/scripts.html index ba45b6fc..cfdb7863 100644 --- a/_includes/scripts.html +++ b/_includes/scripts.html @@ -20,8 +20,9 @@ {% else %} {% assign lang = "en" %} {% endcase %} - - + + + {% endif %} {% include analytics.html %} diff --git a/assets/js/lunr-en.js b/assets/js/lunr/lunr-en.js similarity index 60% rename from assets/js/lunr-en.js rename to assets/js/lunr/lunr-en.js index 04ca480b..c0a7c0ed 100644 --- a/assets/js/lunr-en.js +++ b/assets/js/lunr/lunr-en.js @@ -11,50 +11,19 @@ var idx = lunr(function () { this.pipeline.remove(lunr.trimmer) - {% assign count = 0 %} - {% for c in site.collections %} - {% assign docs = c.docs | where_exp:'doc','doc.search != false' %} - {% for doc in docs %} - this.add({ - title: {{ doc.title | jsonify }}, - excerpt: {{ doc.content | strip_html | truncatewords: 20 | jsonify }}, - categories: {{ doc.categories | jsonify }}, - tags: {{ doc.tags | jsonify }}, - id: {{ count }} - }) - {% assign count = count | plus: 1 %} - {% endfor %} - {% endfor %} + for (var item in store) { + this.add({ + title: store[item].title, + excerpt: store[item].excerpt, + categories: store[item].categories, + tags: store[item].tags, + id: item + }) + } }); console.log( jQuery.type(idx) ); -var store = [ - {% for c in site.collections %} - {% if forloop.last %} - {% assign l = true %} - {% endif %} - {% assign docs = c.docs | where_exp:'doc','doc.search != false' %} - {% for doc in docs %} - {% if doc.header.teaser %} - {% capture teaser %}{{ doc.header.teaser }}{% endcapture %} - {% else %} - {% assign teaser = site.teaser %} - {% endif %} - { - "title": {{ doc.title | jsonify }}, - "url": {{ doc.url | absolute_url | jsonify }}, - "excerpt": {{ doc.content | strip_html | truncatewords: 20 | jsonify }}, - "teaser": - {% if teaser contains "://" %} - {{ teaser | jsonify }} - {% else %} - {{ teaser | absolute_url | jsonify }} - {% endif %} - }{% unless forloop.last and l %},{% endunless %} - {% endfor %} - {% endfor %}] - $(document).ready(function() { $('input#search').on('keyup', function () { var resultdiv = $('#results'); @@ -62,7 +31,7 @@ $(document).ready(function() { var result = idx.query(function (q) { query.split(lunr.tokenizer.separator).forEach(function (term) { - q.term(term, { boost: 100 }) + q.term(term, { boost: 100 }) if(query.lastIndexOf(" ") != query.length-1){ q.term(term, { usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING, boost: 10 }) } @@ -85,7 +54,7 @@ $(document).ready(function() { '
'+ ''+ '
'+ - '

'+store[ref].excerpt+'

'+ + '

'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...

'+ ''+ ''; } @@ -96,7 +65,7 @@ $(document).ready(function() { '

'+ ''+store[ref].title+''+ '

'+ - '

'+store[ref].excerpt+'

'+ + '

'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...

'+ ''+ ''; } diff --git a/assets/js/lunr-gr.js b/assets/js/lunr/lunr-gr.js similarity index 91% rename from assets/js/lunr-gr.js rename to assets/js/lunr/lunr-gr.js index a335aca0..ef540cdb 100644 --- a/assets/js/lunr-gr.js +++ b/assets/js/lunr/lunr-gr.js @@ -464,50 +464,19 @@ var idx = lunr(function () { this.pipeline.add(greekStemmer) this.pipeline.remove(lunr.stemmer) - {% assign count = 0 %} - {% for c in site.collections %} - {% assign docs = c.docs | where_exp:'doc','doc.search != false' %} - {% for doc in docs %} - this.add({ - title: {{ doc.title | jsonify }}, - excerpt: {{ doc.content | strip_html | truncatewords: 20 | jsonify }}, - categories: {{ doc.categories | jsonify }}, - tags: {{ doc.tags | jsonify }}, - id: {{ count }} - }) - {% assign count = count | plus: 1 %} - {% endfor %} - {% endfor %} + for (var item in store) { + this.add({ + title: store[item].title, + excerpt: store[item].excerpt, + categories: store[item].categories, + tags: store[item].tags, + id: item + }) + } }); console.log( jQuery.type(idx) ); -var store = [ - {% for c in site.collections %} - {% if forloop.last %} - {% assign l = true %} - {% endif %} - {% assign docs = c.docs | where_exp:'doc','doc.search != false' %} - {% for doc in docs %} - {% if doc.header.teaser %} - {% capture teaser %}{{ doc.header.teaser }}{% endcapture %} - {% else %} - {% assign teaser = site.teaser %} - {% endif %} - { - "title": {{ doc.title | jsonify }}, - "url": {{ doc.url | absolute_url | jsonify }}, - "excerpt": {{ doc.content | strip_html | truncatewords: 20 | jsonify }}, - "teaser": - {% if teaser contains "://" %} - {{ teaser | jsonify }} - {% else %} - {{ teaser | absolute_url | jsonify }} - {% endif %} - }{% unless forloop.last and l %},{% endunless %} - {% endfor %} - {% endfor %}] - $(document).ready(function() { $('input#search').on('keyup', function () { var resultdiv = $('#results'); @@ -538,7 +507,7 @@ $(document).ready(function() { '
'+ ''+ '
'+ - '

'+store[ref].excerpt+'

'+ + '

'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...

'+ ''+ ''; } @@ -549,7 +518,7 @@ $(document).ready(function() { '

'+ ''+store[ref].title+''+ '

'+ - '

'+store[ref].excerpt+'

'+ + '

'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...

'+ ''+ ''; } diff --git a/assets/js/lunr/lunr-store.js b/assets/js/lunr/lunr-store.js new file mode 100644 index 00000000..cc783c01 --- /dev/null +++ b/assets/js/lunr/lunr-store.js @@ -0,0 +1,36 @@ +--- +layout: null +--- + +var store = [ + {% for c in site.collections %} + {% if forloop.last %} + {% assign l = true %} + {% endif %} + {% assign docs = c.docs | where_exp:'doc','doc.search != false' %} + {% for doc in docs %} + {% if doc.header.teaser %} + {% capture teaser %}{{ doc.header.teaser }}{% endcapture %} + {% else %} + {% assign teaser = site.teaser %} + {% endif %} + { + "title": {{ doc.title | jsonify }}, + "excerpt": + {% if site.search_full_content == true %} + {{ doc.content | strip_html | strip_newlines | jsonify }}, + {% else %} + {{ doc.content | strip_html | strip_newlines | truncatewords: 50 | jsonify }}, + {% endif %} + "categories": {{ doc.categories | jsonify }}, + "tags": {{ doc.tags | jsonify }}, + "url": {{ doc.url | absolute_url | jsonify }}, + "teaser": + {% if teaser contains "://" %} + {{ teaser | jsonify }} + {% else %} + {{ teaser | absolute_url | jsonify }} + {% endif %} + }{% unless forloop.last and l %},{% endunless %} + {% endfor %} + {% endfor %}] diff --git a/assets/js/lunr.min.js b/assets/js/lunr/lunr.min.js similarity index 100% rename from assets/js/lunr.min.js rename to assets/js/lunr/lunr.min.js diff --git a/docs/_config.yml b/docs/_config.yml index 292f5728..3f09e694 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -56,6 +56,7 @@ atom_feed: path : # blank (default) uses feed.xml search : true # true, false (default) +search_full_content : true # true, false (default) # SEO Related google_site_verification : "UQj93ERU9zgECodaaXgVpkjrFn9UrDMEzVamacSoQ8Y" # Replace this with your ID, or delete diff --git a/test/_config.yml b/test/_config.yml index 39e6ecae..4222bc93 100644 --- a/test/_config.yml +++ b/test/_config.yml @@ -9,7 +9,7 @@ theme : "minimal-mistakes-jekyll" minimal_mistakes_skin : "default" # "air", "aqua", "contrast", "dark", "dirt", "neon", "mint", "plum", "sunrise" # Site Settings -locale : "en" +locale : "en-US" title : "Minimal Mistakes Development Test Site" title_separator : "-" name : "Your Name" @@ -50,6 +50,7 @@ staticman: atom_feed: path : # blank (default) uses feed.xml search : true # true, false (default) +search_full_content : true # true, false (default) # SEO Related google_site_verification :