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

[WIP] implement-start-of-search #213

Open
wants to merge 38 commits into
base: prod
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d1859dc
implement-start-of-search
kkemple Jun 12, 2020
b65b1ac
Merge branch 'prod' into wip-search
kkemple Jun 12, 2020
59315c2
rename filter for removing markdown formatting
kkemple Jun 12, 2020
c4d6c7e
Merge branch 'wip-search' of https://github.com/kkemple/selfdefined i…
kkemple Jun 12, 2020
5e6a3a7
fix conflicts
kkemple Jun 12, 2020
dc9e6b8
remove unused dev dependencies
kkemple Jun 12, 2020
12de561
readd engine field
kkemple Jun 12, 2020
e908972
move search to serverless function
kkemple Jun 20, 2020
15ed80b
Merge branch 'prod' into wip-search
kkemple Jun 20, 2020
fd6e0a5
update styling for search
kkemple Jul 4, 2020
6b97a9e
Merge branch 'wip-search' of https://github.com/kkemple/selfdefined i…
kkemple Jul 4, 2020
30c3f7e
Merge branch 'prod' into wip-search
kkemple Jul 4, 2020
9ac06a3
update search to show snippets with searched text highlighted
kkemple Aug 1, 2020
9572418
Merge branch 'wip-search' of https://github.com/kkemple/selfdefined i…
kkemple Aug 1, 2020
302f6d5
Merge branch 'prod' into wip-search
kkemple Aug 1, 2020
52ddbc8
rename search json file and permalink
kkemple Aug 8, 2020
1954503
Merge branch 'wip-search' of https://github.com/kkemple/selfdefined i…
kkemple Aug 8, 2020
87866d7
moved search to search page
kkemple Aug 8, 2020
274f3b1
remove unused function in search component
kkemple Aug 8, 2020
0f63bf5
Update 11ty/search.njk
kkemple Aug 10, 2020
44f4c6d
Update 11ty/search.njk
kkemple Aug 10, 2020
4489ffa
refactor HTML, CSS, and add search to results page
kkemple Aug 15, 2020
a268118
move serverless function into project
kkemple Aug 15, 2020
fd6b361
update search url in search results page
kkemple Aug 15, 2020
202a22d
add missing dependencies for search function
kkemple Aug 15, 2020
f5ee15d
add variable declaration for definition in loop in search function
kkemple Aug 15, 2020
953df5d
fix callback signatures in search function
kkemple Aug 15, 2020
3ff2848
Merge branch 'prod' into wip-search
kkemple Aug 21, 2020
665af72
add search results table of contents
Aug 22, 2020
9a527d5
feat(search): make smooth scrolling global default
ovlb Aug 24, 2020
303fd28
feat(search): abstract button and input styles, mk focus more visible
ovlb Aug 24, 2020
b9680a7
Merge branch 'prod' into wip-search
ovlb Aug 24, 2020
5393ea1
🙈 revert to using netlify
ovlb Aug 24, 2020
4bd2312
Merge branch 'wip-search' of github.com:kkemple/selfdefined into wip-…
ovlb Aug 24, 2020
b8eaec3
Merge branch 'prod' into wip-search
ovlb Aug 28, 2020
7e01f98
feat(search): port dark mode, style clean ups
ovlb Aug 28, 2020
17903be
Merge branch 'prod' into wip-search
kkemple Aug 29, 2020
10b14b7
Merge branch 'prod' into wip-search
kkemple Aug 31, 2020
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
22 changes: 22 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const definitionPermalink = require('./11ty/helpers/definitionPermalink');
const renderDefinitionContentNextEntries = require('./11ty/shortcodes/renderDefinitionContentNextEntries');
const findExistingDefinition = require('./11ty/filters/helpers/findExistingDefinition');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const removeMd = require('remove-markdown');

module.exports = function(config) {
// Add a filter using the Config API
Expand Down Expand Up @@ -187,6 +188,27 @@ module.exports = function(config) {
});
});

config.addCollection('search', (collection) => {
return collection
.getFilteredByGlob('./11ty/definitions/*.md')
.sort((a, b) => {
// `localeCompare()` is super cool: http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
return a.data.title
.toLowerCase()
.localeCompare(b.data.title.toLowerCase());
});
});

// add filter to strip Markdown formatting from search text
config.addFilter('removeMarkdownFormatting', (value) => {
return removeMd(value).replace(/[\s\s+,\n]/g, ' ');
});

// add filter to extract text from sub_terms in search template
config.addFilter('extractSubTermText', (value) => {
return value ? value.map((subterm) => subterm.text).join(',') : '';
});

const mdIt = require('markdown-it')({
html: true
});
Expand Down
71 changes: 71 additions & 0 deletions 11ty/_includes/components/search.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<section class="search">
<label for="search">Search</label>
<input type="search" id="search" class="search" name="search" />
<ul id="search-results-list" class="u-no-padding-left list" aria-labelledby="search" />
</section>

{% block pageScript%}
<script src="https://unpkg.com/lunr/lunr.js"></script>
<script>
// don't use es6+ here because it will not be transpiled
window.addEventListener('DOMContentLoaded', async function() {
function buildResultListItem(result) {
return `<h4>${result.title}</h4><p>${result.content.substr(0, 100)}</p>`
}

try {
// load definitions in as JSON
const result = await fetch('/search.json')
const definitions = await result.json()

// create a map based on title for faster retrieval in search results
const definitionMap = definitions.reduce((map, definition) => {
map[definition.title] = definition
return map
}, [])

// create the full text search
const idx = lunr(function() {
const self = this
this.ref('title')
this.field('title')
this.field('subterms')
this.field('content')

// load definitions into lunr index
for (definition in definitions) {
self.add(definitions[definition])
}
})

const $search = document.querySelector('#search')
const $resultsContainer = document.querySelector('#search-results-list')

$search.addEventListener('change', event => {
const value = event.target.value

if (!event.target.value) {
$resultsContainer.innerHTML = ''
return
}

const matches = idx.search(`*${event.target.value}*`)
const results = matches.map(match => definitionMap[match.ref])
console.log({matches, results })

$resultsContainer.innerHTML = ''
results.forEach((result) => {
let $childElement = document.createElement('li')
$childElement.innerHTML = buildResultListItem(result)
$resultsContainer.appendChild($childElement)
})

})
} catch (error) {
console.log('error in search!', error)
}


})
</script>
{% endblock %}
1 change: 0 additions & 1 deletion 11ty/_includes/layouts/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<main>
{{ content | safe }}
{% include 'components/table-of-content.njk' %}

</main>

{% endblock %}
Expand Down
1 change: 1 addition & 0 deletions 11ty/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ layout: layouts/index.njk
<li>Follow us on Twitter <a href="https://www.twitter.com/selfdefinedapp">@SelfDefinedApp</a>.</li>
</ol>
</div>
{% include 'components/search.njk' %}
</header>
13 changes: 13 additions & 0 deletions 11ty/search.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
permalink: search.json
---
[
{%- for item in collections.search -%}
{
"url" : "{{ item.url }}",
"title" : "{{ item.data.title }}",
"subterms": "{{item.data.sub_terms | extractSubTermText }}",
"content" : "{{ item.templateContent | removeMarkdownFormatting }}"
}{% if not loop.last %},{% else %}{%- endif -%}
{%- endfor -%}
]
2 changes: 1 addition & 1 deletion assets/css/base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// 5. Components
@import 'components/word', 'components/lists', 'components/definitions',
'components/flag';
'components/flag', 'components/search';

// 6. Page-specific styles
@import './layouts/pages';
Expand Down
4 changes: 4 additions & 0 deletions assets/css/components/_search.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.search {
background-color: #000000;
color: #ffffff;
}
62 changes: 34 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"build": "concurrently npm:build:*",
"build:css": "parcel build assets/css/base.scss --out-dir dist/assets/css/",
"build:site": "eleventy",
"create-definitions": "bin/create-definitions",
"lint": "concurrently npm:lint:*",
"lint:css": "stylelint \"assets/css/**/*.scss\" --fix",
"lint:js": "eslint \"**/*.js\" --fix",
Expand All @@ -19,9 +18,6 @@
"watch": "eleventy --watch",
"watch:css": "parcel watch assets/css/base.scss --out-dir dist/assets/css/"
},
"bin": {
"create-definitions": "bin/create-definitions"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tatianamac/selfdefined.git"
Expand Down Expand Up @@ -71,6 +67,7 @@
"markdownlint-cli": "^0.23.1",
"parcel-bundler": "^1.12.4",
"prettier": "^1.19.1",
"remove-markdown": "^0.3.0",
"sass": "^1.26.7",
"slugify": "^1.4.0",
"stylelint": "^13.5.0",
Expand Down