From bf5acb097c1347e4692cc0cb14ec5e19b14d34cb Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Mon, 25 Jul 2022 12:32:54 +0200 Subject: [PATCH] remove lunr dependencies in favor of https://pagefind.app/ --- README.md | 14 ++---- package.json | 3 +- src/index.js | 4 -- src/lunr/index-builder.js | 48 -------------------- test/lunr/index-builder.test.js | 24 ---------- test/lunr/postsstub1/2021/03/some-article.md | 8 ---- test/lunr/postsstub2/2021/03/some-article.md | 10 ---- yarn.lock | 8 ---- 8 files changed, 4 insertions(+), 115 deletions(-) delete mode 100644 src/lunr/index-builder.js delete mode 100644 test/lunr/index-builder.test.js delete mode 100644 test/lunr/postsstub1/2021/03/some-article.md delete mode 100644 test/lunr/postsstub2/2021/03/some-article.md diff --git a/README.md b/README.md index 71067fe..128af5f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ These simple scripts **enrich your Jamstack-site** by adding/manipulating/whatev Usage: 1. `yarn add jam-my-stack` -2. `const { mastodon, goodreads, lunr } = require('jam-my-stack')` +2. `const { mastodon, goodreads } = require('jam-my-stack')` ### 1. Mastodon @@ -102,18 +102,10 @@ Usage example: ### 3. Lunr -#### 3.1 `buildIndex` +As of version `1.0.30`, Lunr functionality was removed in favor of https://pagefind.app/ -An async function that reads all `.md` files of certain locations (pass as an array `[]`), generating a [Lunr.js](https://lunrjs.com/) `.json` index object. Serialize it yourself wherever you'd like it to go. +With Pagefind, there's no need to integrate it into jam-my-stack, greatly simplifying things _and_ reducing the index file size. -Usage example: - -```js - const index = await lunr.buildIndex([ - `${__dirname}/content/post`, - `${__dirname}/content/notes`]) - await fsp.writeFile(`${__dirname}/static/js/brainbaking-post.json`, JSON.stringify(index), 'utf-8') -``` ### 4. Howlongtobeat diff --git a/package.json b/package.json index 9a8220e..b80cb41 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.29", + "version": "1.0.30", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" @@ -19,7 +19,6 @@ "got": "11.8.3", "howlongtobeat": "^1.5.1", "imagemagick": "^0.1.3", - "lunr": "^2.3.9", "parser-front-matter": "^1.6.4", "youtube-dl-exec": "^1.2.4" }, diff --git a/src/index.js b/src/index.js index fed70d4..dcb466c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,5 @@ const { parseMastoFeed } = require('./mastodon/feed-parser') const { widgetify } = require('./goodreads/widgetify.js') -const { buildIndex } = require('./lunr/index-builder.js') const { howlong } = require('./howlongtobeat/howlong.js') const { thumbify } = require('./youtube/thumbify.js') @@ -15,9 +14,6 @@ module.exports = { goodreads: { createWidget: widgetify }, - lunr: { - buildIndex: buildIndex - }, howlongtobeat: { howlong: howlong }, diff --git a/src/lunr/index-builder.js b/src/lunr/index-builder.js deleted file mode 100644 index cb54f39..0000000 --- a/src/lunr/index-builder.js +++ /dev/null @@ -1,48 +0,0 @@ -const fs = require('fs').promises; -const { getFiles } = require('./../file-utils'); -const { promisify } = require('util'); -const frontMatterParser = require('parser-front-matter'); - -const parse = promisify(frontMatterParser.parse.bind(frontMatterParser)); - - -async function loadPostsWithFrontMatter(postsDirectoryPath) { - const postNames = await getFiles(postsDirectoryPath); - const posts = await Promise.all( - // could be .DS_Store stuff found using recursive function above... - postNames.filter(name => name.endsWith('.md')).map(async fileName => { - const fileContent = await fs.readFile(fileName, 'utf8'); - const {content, data} = await parse(fileContent); - return { - content: content.slice(0, 3000), - ...data - }; - }) - ); - return posts; -} - -const lunrjs = require('lunr'); - -function makeIndex(posts) { - return lunrjs(function() { - this.ref('title'); - this.field('title'); - this.field('content'); - this.field('tags'); - posts.forEach(p => { - this.add(p); - }); - }); -} - -async function run(contentDirs) { - const posts = await Promise.all(contentDirs.map(async (dir) => { - return await loadPostsWithFrontMatter(dir) - })) - return makeIndex(posts.flat()); -} - -module.exports = { - buildIndex: run -} diff --git a/test/lunr/index-builder.test.js b/test/lunr/index-builder.test.js deleted file mode 100644 index c97b2c7..0000000 --- a/test/lunr/index-builder.test.js +++ /dev/null @@ -1,24 +0,0 @@ -const { buildIndex } = require('../../src/lunr/index-builder') - -let result = null - -beforeEach(async () => { - result = await buildIndex([ - `${__dirname}/postsstub1`, - `${__dirname}/postsstub2`]) -}) - -test('lunr inverted index stuffed with loads of goodies from both dirs', async() => { - expect(result.invertedIndex.cool).not.toBe(undefined) - expect(result.invertedIndex.gravediggaz).not.toBe(undefined) - expect(result.invertedIndex.wu).not.toBe(undefined) - expect(result.invertedIndex.tang).not.toBe(undefined) - expect(result.invertedIndex.east).not.toBe(undefined) - expect(result.invertedIndex.side).not.toBe(undefined) -}) - -test('lunr index builder fields are title, content, tags', async () => { - // Do not forget to add JSON.Stringify() when calling this in production - expect(result.fields).toEqual(["title", "content", "tags"]) -}) - diff --git a/test/lunr/postsstub1/2021/03/some-article.md b/test/lunr/postsstub1/2021/03/some-article.md deleted file mode 100644 index da48c66..0000000 --- a/test/lunr/postsstub1/2021/03/some-article.md +++ /dev/null @@ -1,8 +0,0 @@ ---- -title: some cool article -date: 2021-03-02 ---- - -Hi gayz whas goin' on gravediggaz style! - -kkthxxbbye diff --git a/test/lunr/postsstub2/2021/03/some-article.md b/test/lunr/postsstub2/2021/03/some-article.md deleted file mode 100644 index ab67f40..0000000 --- a/test/lunr/postsstub2/2021/03/some-article.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -title: another cool article east-side -date: 2021-03-03 ---- - -It's just a hobby that I picked up in the lobby - ---- - -The W, Wu-tang Clan yoo diff --git a/yarn.lock b/yarn.lock index ce4fbca..6085a9b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3216,7 +3216,6 @@ fsevents@^2.1.2: howlongtobeat: ^1.5.1 imagemagick: ^0.1.3 jest: ^26.6.3 - lunr: ^2.3.9 mockdate: ^3.0.2 parser-front-matter: ^1.6.4 youtube-dl-exec: ^1.2.4 @@ -3929,13 +3928,6 @@ fsevents@^2.1.2: languageName: node linkType: hard -"lunr@npm:^2.3.9": - version: 2.3.9 - resolution: "lunr@npm:2.3.9" - checksum: a2b66320c2f6632322b91c43621eed9e623c0f1b81e7c2d74c238884a02229698baa4478bd238e9b32d7abf0db5ae9233b311c28e1edbcc481774e1ce83bec6f - languageName: node - linkType: hard - "make-dir@npm:^3.0.0": version: 3.1.0 resolution: "make-dir@npm:3.1.0"