diff --git a/.eleventy.js b/.eleventy.js index 9eb1d76..4c1ee82 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -184,6 +184,9 @@ module.exports = function(eleventyConfig) { eleventyConfig.addPassthroughCopy("src/site/img"); eleventyConfig.addPlugin(faviconPlugin, { destination: 'dist' }); + eleventyConfig.addFilter('jsonify', function (variable) { + return JSON.stringify(variable); + }); return { dir: { diff --git a/.eleventyignore b/.eleventyignore new file mode 100644 index 0000000..bbbafeb --- /dev/null +++ b/.eleventyignore @@ -0,0 +1 @@ +netlify/functions diff --git a/.gitignore b/.gitignore index f06235c..9a963f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules dist +netlify/functions/search/data.json +netlify/functions/search/index.json diff --git a/netlify.toml b/netlify.toml index 0f8b4a4..6a5563a 100644 --- a/netlify.toml +++ b/netlify.toml @@ -2,7 +2,13 @@ publish = "dist" command = "npm install && npm run build" +[[redirects]] + from = "/api/*" + to = "/.netlify/functions/:splat" + status = 200 + [[redirects]] from = "/*" to = "/404" - status = 404 \ No newline at end of file + status = 404 + diff --git a/netlify/functions/search/search.js b/netlify/functions/search/search.js new file mode 100644 index 0000000..31958ce --- /dev/null +++ b/netlify/functions/search/search.js @@ -0,0 +1,45 @@ +const lunrjs = require('lunr'); + +const handler = async (event) => { + try { + + + const search = event.queryStringParameters.term; + if(!search) throw('Missing term query parameter'); + + const data = require('./data.json'); + const indexJson = require('./index.json'); + const index = lunrjs.Index.load(indexJson); + console.log('index made'); + + let results = index.search(search); + + results.forEach(r => { + r.title = data[r.ref].title; + r.content = truncate(data[r.ref].content, 400); + r.date = data[r.ref].date; + r.url = data[r.ref].url; + + delete r.ref; + }); + + return { + statusCode: 200, + body: JSON.stringify(results), + // // more keys you can return: + // headers: { "headerName": "headerValue", ... }, + // isBase64Encoded: true, + } + } catch (error) { + return { statusCode: 500, body: error.toString() } + } +} + +function truncate(str, size) { + //first, remove HTML + str = str.replace(/<.*?>/g, ''); + if(str.length < size) return str; + return str.substring(0, size-3) + '...'; +} + +module.exports = { handler } diff --git a/package-lock.json b/package-lock.json index 811ec54..7d0c043 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "eleventy-favicon": "^1.1.2", "fs-file-tree": "^1.1.1", "gray-matter": "^4.0.3", + "lunr": "^2.3.9", "markdown-it": "^12.3.2", "markdown-it-footnote": "^3.0.3", "markdown-it-mathjax3": "^4.3.1", @@ -3332,6 +3333,11 @@ "yallist": "^2.1.2" } }, + "node_modules/lunr": { + "version": "2.3.9", + "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz", + "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==" + }, "node_modules/luxon": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/luxon/-/luxon-2.3.1.tgz", @@ -8855,6 +8861,11 @@ "yallist": "^2.1.2" } }, + "lunr": { + "version": "2.3.9", + "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.9.tgz", + "integrity": "sha512-zTU3DaZaF3Rt9rhN3uBMGQD3dD2/vFQqnvZCDv4dl5iOzq2IZQqTxu90r4E5J+nP70J3ilqVCrbho2eWaeW8Ow==" + }, "luxon": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/luxon/-/luxon-2.3.1.tgz", diff --git a/package.json b/package.json index e829276..6c073af 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "watch:eleventy": "cross-env ELEVENTY_ENV=dev eleventy --serve", "build:eleventy": "cross-env ELEVENTY_ENV=prod NODE_OPTIONS=--max-old-space-size=4096 eleventy", "build:sass": "sass src/site/styles:dist/styles", - "build": "npm-run-all build:*" + "build": "npm-run-all build:*", + "postbuild": "node src/site/lunr-index.js" }, "keywords": [], "author": "", @@ -29,6 +30,7 @@ "eleventy-favicon": "^1.1.2", "fs-file-tree": "^1.1.1", "gray-matter": "^4.0.3", + "lunr": "^2.3.9", "markdown-it": "^12.3.2", "markdown-it-footnote": "^3.0.3", "markdown-it-mathjax3": "^4.3.1", diff --git a/src/site/_includes/components/wrapTagsScript.njk b/src/site/_includes/components/wrapTagsScript.njk new file mode 100644 index 0000000..1affc51 --- /dev/null +++ b/src/site/_includes/components/wrapTagsScript.njk @@ -0,0 +1,8 @@ + diff --git a/src/site/_includes/layouts/note.njk b/src/site/_includes/layouts/note.njk index 13e51b0..4b6e1cc 100644 --- a/src/site/_includes/layouts/note.njk +++ b/src/site/_includes/layouts/note.njk @@ -7,6 +7,7 @@ permalink: "notes/{{ page.fileSlug | slugify }}/" {{ page.fileSlug }} {%include "components/pageheader.njk"%} + {%include "components/wrapTagsScript.njk"%} {%include "components/notegrowthhistory.njk"%} @@ -27,5 +28,8 @@ permalink: "notes/{{ page.fileSlug | slugify }}/" {%include "components/sidebar.njk"%} {% endif %} + {% if dgShowBacklinks === true or dgShowLocalGraph === true%} + {%include "components/sidebar.njk"%} + {% endif %} diff --git a/src/site/index.njk b/src/site/index.njk index ee7847f..8758de9 100644 --- a/src/site/index.njk +++ b/src/site/index.njk @@ -26,4 +26,4 @@ {%endif%} - \ No newline at end of file + diff --git a/src/site/lunr-index.js b/src/site/lunr-index.js new file mode 100644 index 0000000..6fcc77b --- /dev/null +++ b/src/site/lunr-index.js @@ -0,0 +1,20 @@ +const lunrjs = require('lunr'); +const path = require('path'); + +function createIndex(posts) { + return lunrjs(function() { + this.ref('id'); + this.field('title'); + this.field('content'); + this.field('date'); + + posts.forEach((p,idx) => { + p.id = idx; + this.add(p); + }); + }); +} + +const data = require('../../netlify/functions/search/data.json'); +const index = createIndex(data); +require('fs').writeFileSync(path.join(__dirname, '../../netlify/functions/search/index.json'), JSON.stringify(index)); diff --git a/src/site/lunr.njk b/src/site/lunr.njk new file mode 100644 index 0000000..4582e1b --- /dev/null +++ b/src/site/lunr.njk @@ -0,0 +1,12 @@ +--- +permalink: netlify/functions/search/data.json +permalinkBypassOutputDir: true +--- +[{% for post in collections.note %} +{ + "title": {{post.fileSlug | jsonify | safe }}, + "date":"{{ post.date }}", + "url":"{{ post.url }}", + "content": {{ post.templateContent | striptags(true) | jsonify | safe }} +}{% if not loop.last %},{% endif %} +{% endfor %}] diff --git a/src/site/search.njk b/src/site/search.njk new file mode 100644 index 0000000..c32a224 --- /dev/null +++ b/src/site/search.njk @@ -0,0 +1,71 @@ +--- +title: "Search" +permalink: "search/" +--- + + + + + {{ collections.gardenEntry[0].fileSlug }} + {%include "components/pageheader.njk"%} + + +
+ {% if dgHomeLink !== false%} + 🏡 Back Home + {% endif %} +

Search

+

+ +
+ +

+ +
+ + + +
+ + +