eleventy-plugin-cloudcannon
Advanced tools
Comparing version 0.0.16 to 0.0.17
const pkginfo = require('pkginfo')(module, 'version'); | ||
const safeStringify = require('fast-safe-stringify'); | ||
@@ -18,17 +19,19 @@ const version = module.exports.version; | ||
} | ||
} | ||
}; | ||
const jsonify = (obj, fallback) => { | ||
try { | ||
return safeStringify(obj); | ||
} catch (e) { | ||
console.warn('eleventy-plugin-cloudcannon failed to jsonify:', e, obj); | ||
} | ||
return fallback; | ||
}; | ||
eleventyConfig.addNunjucksShortcode('ccTime', () => time); | ||
eleventyConfig.addNunjucksShortcode('ccPath', (key) => (config.dir[key] ?? '').replace(/^\.\/?/, '')); | ||
eleventyConfig.addNunjucksShortcode('ccConfig', (key) => JSON.stringify(config[key] ?? '')); | ||
eleventyConfig.addNunjucksShortcode('ccVersion', () => version); | ||
eleventyConfig.addFilter('ccJsonify', (obj) => { | ||
try { | ||
return JSON.stringify(obj); | ||
} catch { | ||
console.warn('eleventy-plugin-cloudcannon: failed to JSON.stringify'); | ||
} | ||
return null; | ||
}); | ||
eleventyConfig.addNunjucksShortcode('ccConfig', (key) => jsonify(config[key] || '')); | ||
eleventyConfig.addFilter('ccJsonify', jsonify); | ||
}; |
@@ -1,4 +0,10 @@ | ||
const { dirname, basename } = require('path'); | ||
const { dirname, basename, extname } = require('path'); | ||
const isEqual = require('lodash.isequal'); | ||
const safeStringify = require('fast-safe-stringify') | ||
const STATIC_PAGE_EXTENSIONS = { | ||
'.html': true, | ||
'.htm': true | ||
}; | ||
function isTopPath(basePath, index, basePaths) { | ||
@@ -9,11 +15,14 @@ return !basePaths.some((other) => other !== basePath && basePath.startsWith(`${other}/`)); | ||
function isStaticPage(item) { | ||
return !item.template._layoutKey && !item.data.tags?.length; | ||
return item.template | ||
&& !item.template?._layoutKey | ||
&& !item.data?.tags?.length | ||
&& STATIC_PAGE_EXTENSIONS[extname(item.inputPath || '')]; | ||
} | ||
function isPage(item) { | ||
return item.template._layoutKey && !item.data.tags?.length; | ||
return item.template?._layoutKey && !item.data?.tags?.length; | ||
} | ||
function isUnlisted(item) { | ||
if (item.data._unlisted === true) { | ||
if (item.data?._unlisted === true || !item.inputPath) { | ||
return true; | ||
@@ -35,5 +44,42 @@ } | ||
return IGNORED_ITEM_KEYS[key] | ||
|| isEqual(item.template?.templateData?.globalData?.[key], item.data[key]); | ||
|| isEqual(item.template?.templateData?.globalData?.[key], item.data?.[key]); | ||
} | ||
function processItem(item, tag) { | ||
if (!item.inputPath) { | ||
return; | ||
} | ||
const data = item.data || {}; | ||
const combinedData = Object.keys(data).reduce((memo, key) => { | ||
if (!isIgnoredItemKey(item, key)) { | ||
memo[key] = data[key]; | ||
} | ||
return memo; | ||
}, {}); | ||
const processed = { | ||
...combinedData, | ||
path: item.inputPath.replace(/^\.\//, ''), | ||
url: item.url || '', | ||
output: item.url !== false | ||
}; | ||
if (tag) { | ||
processed.collection = tag; | ||
} | ||
if (item.template?._layoutKey) { | ||
processed.layout = item.template?._layoutKey; | ||
} | ||
if (isUnlisted(item)) { | ||
processed._unlisted = true; | ||
} | ||
return processed; | ||
} | ||
module.exports = { | ||
@@ -70,30 +116,20 @@ environment: process.env.ELEVENTY_ENV, | ||
processItem: function (item, tag) { | ||
const combinedData = Object.keys(item.data).reduce((memo, key) => { | ||
if (!isIgnoredItemKey(item, key)) { | ||
memo[key] = item.data[key]; | ||
jsonifyItems: function (items, tag) { | ||
const processedItems = items?.reduce?.((memo, item) => { | ||
// Stringified individually to avoid one item breaking it | ||
try { | ||
const json = safeStringify(processItem(item, tag)); | ||
memo.push(json); | ||
} catch (e) { | ||
console.warn('eleventy-plugin-cloudcannon failed to jsonify item:', e); | ||
} | ||
return memo; | ||
}, {}); | ||
}, []) || []; | ||
const processed = { | ||
...combinedData, | ||
path: item.inputPath.replace(/^\.\//, ''), | ||
url: item.url || '', | ||
collection: tag, | ||
output: item.url !== false | ||
}; | ||
return `[${processedItems.join(',\n')}]`; | ||
}, | ||
if (item.template?._layoutKey) { | ||
processed.layout = item.template._layoutKey; | ||
} | ||
processItem: processItem, // TODO: Remove this after changing test references | ||
if (isUnlisted(item)) { | ||
processed._unlisted = true; | ||
} | ||
return processed; | ||
}, | ||
getCollectionsConfig: function (collections, cloudcannon, dataPath) { | ||
@@ -108,5 +144,5 @@ if (cloudcannon?.collections) { | ||
const collectionsMeta = all.reduce((memo, item) => { | ||
const tag = item.data.tags?.[0]; | ||
const tag = item.data?.tags?.[0]; | ||
if (tag) { | ||
if (tag && item.inputPath) { | ||
memo[tag] = memo[tag] ?? { basePaths: new Set(), outputOffset: 0 }; | ||
@@ -113,0 +149,0 @@ // Map tags to basePaths, items with same tags can exist in separate folders |
{ | ||
"name": "eleventy-plugin-cloudcannon", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "Eleventy plugin to create CloudCannon editor details", | ||
@@ -28,2 +28,3 @@ "main": ".eleventy.js", | ||
"dependencies": { | ||
"fast-safe-stringify": "^2.0.8", | ||
"lodash.isequal": "^4.5.0", | ||
@@ -30,0 +31,0 @@ "pkginfo": "^0.4.1" |
Sorry, the diff of this file is not supported yet
15482
184
3
+ Addedfast-safe-stringify@^2.0.8
+ Addedfast-safe-stringify@2.1.1(transitive)