strip unused exports from config

This commit is contained in:
madrilene 2022-12-20 15:51:50 +01:00
parent 69313d6c4f
commit 55be1e15e4
4 changed files with 15 additions and 154 deletions

View file

@ -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
};

View file

@ -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, '<br>');
};
/** 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
};

View file

@ -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
};

View file

@ -1,6 +1,6 @@
{
"name": "eleventy-excellent",
"version": "1.1.5",
"version": "1.1.6",
"engines": {
"node": ">=16.x.x"
},