From 55be1e15e461012797c4b74372afb7b59c5303d5 Mon Sep 17 00:00:00 2001 From: madrilene Date: Tue, 20 Dec 2022 15:51:50 +0100 Subject: [PATCH] strip unused exports from config --- config/constants/index.js | 26 -------------- config/filters/index.js | 67 ++--------------------------------- config/utils/index.js | 74 +++++++-------------------------------- package.json | 2 +- 4 files changed, 15 insertions(+), 154 deletions(-) delete mode 100644 config/constants/index.js diff --git a/config/constants/index.js b/config/constants/index.js deleted file mode 100644 index 684c99a..0000000 --- a/config/constants/index.js +++ /dev/null @@ -1,26 +0,0 @@ -const path = require('path'); - -const dir = { - input: 'src/', - output: 'dist', - includes: '_includes', - layouts: '_layouts', - data: '_data', - assets: 'assets' -}; - -const imagePaths = { - source: path.join(dir.input, dir.assets, 'images'), - output: path.join(dir.output, dir.assets, 'images') -}; - -const scriptDirs = { - source: path.join(dir.input, dir.assets, 'scripts'), - output: path.join(dir.output, dir.assets, 'scripts') -}; - -module.exports = { - dir, - imagePaths, - scriptDirs -}; diff --git a/config/filters/index.js b/config/filters/index.js index 935789f..6b0446b 100644 --- a/config/filters/index.js +++ b/config/filters/index.js @@ -14,50 +14,15 @@ const limit = (array, limit) => { return array.slice(0, limit); }; -/** Sorts the given array of objects by a string denoting chained key paths. */ -const sortByKey = (arrayOfObjects, keyPath, order = 'ASC') => { - const sorted = lodash.sortBy(arrayOfObjects, object => lodash.get(object, keyPath)); - if (order === 'ASC') return sorted; - if (order === 'DESC') return sorted.reverse(); - throw new Error(`Invalid sort order: ${order}`); -}; - /** Returns all entries from the given array that match the specified key:value pair. */ const where = (arrayOfObjects, keyPath, value) => arrayOfObjects.filter(object => lodash.get(object, keyPath) === value); -/** Returns the word count of the given string. */ -const wordCount = str => { - throwIfNotType(str, 'string'); - const matches = str.match(/[\w\d’'-]+/gi); - return matches?.length ?? 0; -}; - /** Converts the given markdown string to HTML, returning it as a string. */ const toHtml = markdownString => { return markdownLib.renderInline(markdownString); }; -/** Divides the first argument by the second. */ -const dividedBy = (numerator, denominator) => { - if (denominator === 0) { - throw new Error(`Cannot divide by zero: ${numerator} / ${denominator}`); - } - return numerator / denominator; -}; - -/** Replaces every newline with a line break. */ -const newlineToBr = str => { - throwIfNotType(str, 'string'); - return str.replace(/\n/g, '
'); -}; - -/** Removes every newline from the given string. */ -const stripNewlines = str => { - throwIfNotType(str, 'string'); - return str.replace(/\n/g, ''); -}; - /** Removes all tags from an HTML string. */ const stripHtml = str => { throwIfNotType(str, 'string'); @@ -81,28 +46,6 @@ const toISOString = dateString => dayjs(dateString).toISOString(); /** Formats a date using dayjs's conventions: https://day.js.org/docs/en/display/format */ const formatDate = (date, format) => dayjs(date).format(format); -/** - * @param {*} collection - an array of collection items that are assumed to have either data.lastUpdated or a date property - * @returns the most recent date of update or publication among the given collection items, or undefined if the array is empty. - */ -const getLatestCollectionItemDate = collection => { - const itemsSortedByLatestDate = collection - .filter(item => !!item.data?.lastUpdated || !!item.date) - .sort((item1, item2) => { - const date1 = item1.data?.lastUpdated ?? item1.date; - const date2 = item2.data?.lastUpdated ?? item2.date; - if (dayjs(date1).isAfter(date2)) { - return -1; - } - if (dayjs(date2).isAfter(date1)) { - return 1; - } - return 0; - }); - const latestItem = itemsSortedByLatestDate[0]; - return latestItem?.data?.lastUpdated ?? latestItem?.date; -}; - const minifyCss = code => new CleanCSS({}).minify(code).styles; /** @@ -135,18 +78,12 @@ const mdInline = (content, opts) => { module.exports = { limit, - sortByKey, - where, - wordCount, toHtml, + where, toISOString, formatDate, - dividedBy, - newlineToBr, - stripNewlines, - stripHtml, toAbsoluteUrl, - getLatestCollectionItemDate, + stripHtml, minifyCss, mdInline }; diff --git a/config/utils/index.js b/config/utils/index.js index e4b81a5..3cdbf2e 100644 --- a/config/utils/index.js +++ b/config/utils/index.js @@ -1,74 +1,24 @@ -const sanitize = require('sanitize-html'); const slugify = require('slugify'); -/** - * Returns an array of all unique values from the given collection under the specified key. - * Credit: https://www.webstoemp.com/blog/basic-custom-taxonomies-with-eleventy/. - * @param {*} collectionItems - an array of collection items to map to their unique values under a key - * @param {*} key - the key to look up in the item's data object - * @returns - */ -const getAllUniqueKeyValues = (collectionItems, key) => { - // First map each collection item (e.g., blog post) to the value it holds under key. - let values = collectionItems.map(item => item.data[key] ?? []); - // Recursively flatten it to a 1D array - values = values.flat(); - // Remove duplicates - values = [...new Set(values)]; - // Sort alphabetically - values = values.sort((key1, key2) => - key1.localeCompare(key2, 'en', {sensitivity: 'base'}) - ); - return values; -}; - /** Converts the given string to a slug form. */ const slugifyString = str => { - return slugify(str, { - replacement: '-', - remove: /[#,&,+()$~%.'":*?<>{}]/g, - lower: true - }); + return slugify(str, { + replacement: '-', + remove: /[#,&,+()$~%.'":*?<>{}]/g, + lower: true + }); }; /** Helper to throw an error if the provided argument is not of the expected. */ const throwIfNotType = (arg, expectedType) => { - if (typeof arg !== expectedType) { - throw new Error( - `Expected argument of type ${expectedType} but instead got ${arg} (${typeof arg})` - ); - } -}; - -/** Maps a config of attribute-value pairs to an HTML string representing those same attribute-value pairs. - * There's also this, but it's ESM only: https://github.com/sindresorhus/stringify-attributes - */ -const stringifyAttributes = attributeMap => { - return Object.entries(attributeMap) - .map(([attribute, value]) => `${attribute}="${value}"`) - .join(' '); -}; - -/** Sanitizes an HTML string. */ -const sanitizeHtml = html => { - return sanitize(html, { - allowedAttributes: { - ...sanitize.defaults.allowedAttributes, - // Syntax highlighting - pre: ['class'], - code: ['class'], - span: ['class'], - // Styled lists - ol: ['class'], - ul: ['class'] - } - }); + if (typeof arg !== expectedType) { + throw new Error( + `Expected argument of type ${expectedType} but instead got ${arg} (${typeof arg})` + ); + } }; module.exports = { - getAllUniqueKeyValues, - slugifyString, - throwIfNotType, - stringifyAttributes, - sanitizeHtml + slugifyString, + throwIfNotType }; diff --git a/package.json b/package.json index 1b553df..6442624 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eleventy-excellent", - "version": "1.1.5", + "version": "1.1.6", "engines": { "node": ">=16.x.x" },