From d9e20edaea978c3646e8722e222b98fc60f3f2e7 Mon Sep 17 00:00:00 2001 From: Chi Kim Date: Thu, 15 Oct 2020 06:40:35 -0700 Subject: [PATCH 1/4] updates #1 - creates a dockerfile --- Dockerfile | 10 ++++++++++ package.json | 1 + 2 files changed, 11 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cd3fe94 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +# Build image +FROM node:12.18-alpine AS build + +# Install only the production dependencies +RUN npm i + +USER node + +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" }, From 3be704fdf1cbacb4ca0f773de0364fc78a1e8022 Mon Sep 17 00:00:00 2001 From: Chi Kim Date: Thu, 15 Oct 2020 06:57:43 -0700 Subject: [PATCH 2/4] updates #1 - copies files into build folder --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index cd3fe94..c28b3d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,12 @@ # Build image FROM node:12.18-alpine AS build +WORKDIR /build # Install only the production dependencies RUN npm i +COPY . /build + USER node EXPOSE 3000 From 1b344f3971472edb1dcd869d55e7020f83633ec3 Mon Sep 17 00:00:00 2001 From: Chi Kim Date: Thu, 15 Oct 2020 07:00:43 -0700 Subject: [PATCH 3/4] updates #1 - env port 3000 --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index c28b3d4..8dbbe88 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,5 +9,6 @@ COPY . /build USER node +ENV PORT=3000 EXPOSE 3000 CMD ["npm", "start"] From e28e54d7ec641ed6e1e8c0329fd10ecae8e863df Mon Sep 17 00:00:00 2001 From: Chi Kim Date: Sun, 6 Dec 2020 15:36:25 -0800 Subject: [PATCH 4/4] fixes an issue in search where data results is parsed incorrectly --- .gitignore | 2 + src/search.js | 116 ++++++++++++++++++++++++++------------------------ 2 files changed, 62 insertions(+), 56 deletions(-) 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/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;