diff --git a/.gitignore b/.gitignore index f6ad6b3..bf148f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ data public/assets logs public/main.js +node_modules +.git diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8dbbe88 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# Build image +FROM node:12.18-alpine AS build +WORKDIR /build + +# Install only the production dependencies +RUN npm i + +COPY . /build + +USER node + +ENV PORT=3000 +EXPOSE 3000 +CMD ["npm", "start"] diff --git a/package.json b/package.json index 9ff1cf4..38ec79a 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "setup": "./scripts/setup.sh", + "start": "node app.js", "server": "webpack --config webpack.config.js && nodemon app.js", "dev": "webpack --config webpack.config.js --watch" }, diff --git a/src/search.js b/src/search.js index 65ec7cd..8bce5ba 100644 --- a/src/search.js +++ b/src/search.js @@ -1,48 +1,54 @@ let resultContainer; let searchBox; let allNotes; -let resultElements = [] -let noteMap = {} +let resultElements = []; +let noteMap = {}; const search = () => { - const searchQuery = searchBox.value + const searchQuery = searchBox.value; if (searchQuery.length < 2) { - return resultContainer.innerHTML = '' + return (resultContainer.innerHTML = ""); } - fetch(`/search/${searchQuery}`).then( r=> r.text() ).then(data => { - // invalidate old requests - if( searchBox.value !== searchQuery) return - const dataArr = data.split('\n').filter( d => d ).map( d => { - const result = d.substr(5) // get rid of data/ prefix - const resultArr = result.split(':') - const title = resultArr.shift() - const match = resultArr.join(':') - return { - title, match - } - }) - resultContainer.innerHTML = '' - const newTitleMap = {...noteMap} - resultElements = dataArr.map( (obj, idx) => { - delete newTitleMap[obj.title.toLowerCase()] - return new Result(obj.title, obj.match, idx) - }) - Object.keys(newTitleMap).forEach( title => { - if (!title.includes(searchQuery.toLowerCase())) { - return - } - resultElements.push( - new Result(newTitleMap[title], '', resultElements.length) - ) - }) - }) -} + fetch(`/search/${searchQuery}`) + .then((r) => r.text()) + .then((data) => { + // invalidate old requests + if (searchBox.value !== searchQuery) return; + const dataArr = data + .split("\n") + .filter((d) => d) + .map((d) => { + const result = d.replace("./data/", ""); // get rid of ./data/ prefix + const resultArr = result.split(":"); + const title = resultArr.shift(); + const match = resultArr.join(":"); + return { + title, + match, + }; + }); + resultContainer.innerHTML = ""; + const newTitleMap = { ...noteMap }; + resultElements = dataArr.map((obj, idx) => { + delete newTitleMap[obj.title.toLowerCase()]; + return new Result(obj.title, obj.match, idx); + }); + Object.keys(newTitleMap).forEach((title) => { + if (!title.includes(searchQuery.toLowerCase())) { + return; + } + resultElements.push( + new Result(newTitleMap[title], "", resultElements.length) + ); + }); + }); +}; function Result(name, value, idx) { - const div = document.createElement('div') - div.className = 'resultContainer' + const div = document.createElement("div"); + div.className = "resultContainer"; if (!idx) { - div.className += ' selected' + div.className += " selected"; } div.innerHTML = `
@@ -51,31 +57,29 @@ function Result(name, value, idx) { ${value}
- ` - div.addEventListener('click', () => { - this.select() - }) + `; + div.addEventListener("click", () => { + this.select(); + }); - resultContainer.append(div) + resultContainer.append(div); this.select = () => { - window.location = `/notes/${name}` - } + window.location = `/notes/${name}`; + }; } const initSearch = (titleMap) => { - resultContainer = document.querySelector('#resultListContainer') - searchBox = document.querySelector('#searchBox') - allNotes = document.querySelectorAll('.card-columns .card-title') - searchBox.focus() - noteMap = titleMap - searchBox.addEventListener('keyup', (e) => { - console.log(e.key) - if (e.key === 'Enter') { - return resultElements[0].select() + resultContainer = document.querySelector("#resultListContainer"); + searchBox = document.querySelector("#searchBox"); + allNotes = document.querySelectorAll(".card-columns .card-title"); + searchBox.focus(); + noteMap = titleMap; + searchBox.addEventListener("keyup", (e) => { + if (e.key === "Enter") { + return resultElements[0].select(); } - search() - }) - -} + search(); + }); +}; -module.exports = initSearch +module.exports = initSearch;