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

search #23

Open
wants to merge 12 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
BROWSERIFY = node_modules/.bin/browserify
ESLINT = node_modules/.bin/eslint
NPM = npm
UGLIFY = node_modules/.bin/uglifyjs

CUSTOM = $(shell find custom -name '*.md' | sort)
VENDOR = ramda sanctuary sanctuary-def sanctuary-type-classes sanctuary-type-identifiers
VENDOR_CHECKS = $(patsubst %,check-%-version,$(VENDOR))
FILES = favicon.png index.html $(patsubst %,vendor/%.js,$(VENDOR))
FILES = favicon.png index.html search.js $(patsubst %,vendor/%.js,$(VENDOR))


.PHONY: all
Expand All @@ -22,6 +24,8 @@ vendor/ramda.js: node_modules/ramda/dist/ramda.js
vendor/%.js: node_modules/%/index.js
cp '$<' '$@'

search.js: src/search.js
$(BROWSERIFY) '$<' | $(UGLIFY) > '$@'

.PHONY: $(VENDOR_CHECKS)
$(VENDOR_CHECKS): check-%-version:
Expand All @@ -47,7 +51,7 @@ lint:
--config node_modules/sanctuary-style/eslint-es3.json \
--env es3 \
--env browser \
-- behaviour.js
-- behaviour.js src/search.js
make clean
make
git diff --exit-code
Expand Down
8 changes: 8 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
<div id="css-main">
<h1 id="sanctuary">Sanctuary <small>v0.12.2</small></h1>
<p id="tagline">Refuge from unsafe JavaScript</p>
<div>
<input type="text" id="search-input" placeholder="search" />
</div>
<div id="search-results">
<div id="no-matches" style="display:none;">No matches</div>
<div id="yes-matches"></div>
</div>

<ul id="toc">
<li>
Expand Down Expand Up @@ -3956,5 +3963,6 @@ <h4 id="splitOn"><code><a href="https://github.com/sanctuary-js/sanctuary/blob/v
window.S = window.sanctuary;
</script>
<script src="behaviour.js"></script>
<script src="search.js"></script>
</body>
</html>
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"sanctuary-type-identifiers": "1.0.0"
},
"devDependencies": {
"browserify": "14.0.x",
"envvar": "1.1.x",
"eslint": "3.19.x",
"hindley-milner-search": "0.2.1",
"marked": "0.3.5",
"sanctuary-style": "0.5.x"
"sanctuary-style": "0.5.x",
"uglify-js": "2.7.x"
}
}
8 changes: 8 additions & 0 deletions scripts/generate
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ def('toDocument',
<div id="css-main">
<h1 id="sanctuary">Sanctuary <small>v${version}</small></h1>
<p id="tagline">${tagline}</p>
<div>
<input type="text" id="search-input" placeholder="search" />
</div>
<div id="search-results">
<div id="no-matches" style="display:none;">No matches</div>
<div id="yes-matches"></div>
</div>
${toc(content)}
${content}
</div>
Expand All @@ -406,6 +413,7 @@ ${content}
window.S = window.sanctuary;
</script>
<script src="behaviour.js"></script>
<script src="search.js"></script>
</body>
</html>
`);
Expand Down
2 changes: 2 additions & 0 deletions search.js

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(function() {

'use strict';

/* global require */

var HMS = require('hindley-milner-search');

// initSigNodes :: Node -> [Node]
function initSigNodes(resultsContainer) {
return Array.prototype.reduce.call(
document.getElementById('toc').querySelectorAll('li a code'),
function(r, _el) {
var el = document.createElement('div');
el.appendChild(_el.parentNode.cloneNode(true));
el.className = 'search-result';
var k = el.childNodes[0].getAttribute('href');
if (!Object.prototype.hasOwnProperty.call(r.set, k)) {
r.set[k] = true;
r.uniqs.push(el);
}
return r;
},
{set: {}, uniqs: []}
).uniqs;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't much like the show and hide functions as they're impure. I'd prefer this mutation to appear in situ:

state.containerNo.style.display =
  matches.length === 0 && q.length > 0 ? 'block' : 'none';


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👾

window.addEventListener('DOMContentLoaded', function() {
var $container = document.getElementById('yes-matches');
var $containerNo = document.getElementById('no-matches');
var $els = initSigNodes(document.getElementById('search-results'));
var search = HMS.init(Array.prototype.map.call($els, function(el) {
return el.innerText.trim().split(/\s+/).join(' ');
}), {fuzzy: false}).search;
var $prevSearch = '';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does $ prefix mean? Why does $prevSearch have one but not q below?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 6e24452 I replaced the state object with various variables. I assumed each of these variables to be stateful, and used the $ prefix to remind myself of this.

var searchInput = document.getElementById('search-input');
searchInput.focus();
searchInput.addEventListener('keyup', function(event) {
var q = event.target.value;
if (q !== $prevSearch) {
$prevSearch = q;
var matches = search(q);
$containerNo.style.display =
matches.length === 0 && q !== '' ? 'block' : 'none';
while ($container.childNodes.length > 0) {
$container.removeChild($container.childNodes[0]);
}
// if there is no query, do not show any matches
if (q !== '') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had the empty string return all matches simply because I was culling results from the current DOM rather than building a new list. I'm not sure which would be more common, but I can add an option to return all or none on empty input and we wouldn't need this conditional in userland either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we'll be displaying results at /search/ rather than at /, displaying all results for an empty query seems right to me. I've removed this check. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to move this logic a little in the latest release. Not a million percent sure we're on the same page but just let me know if empty string is not doing what you want now.

matches.forEach(function(match) {
$container.appendChild($els[match.pointer]);
});
}
}
});
});

}());
4 changes: 4 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ a code {
top: -0.05em;
}

#search-results .search-result {
font-size: 75%;
}

pre {
margin: 1.334em 0;
padding: 0;
Expand Down