Skip to content

Commit

Permalink
Merge pull request #18 from CloudCannon/feat/collections-config-sorting
Browse files Browse the repository at this point in the history
Sort collection_config entries by name and output, update output/disable_url
  • Loading branch information
Tate-CC authored Sep 11, 2024
2 parents 0750a83 + 8402627 commit 6d5af02
Show file tree
Hide file tree
Showing 25 changed files with 281 additions and 263 deletions.
54 changes: 33 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
},
"author": "CloudCannon <[email protected]>",
"devDependencies": {
"@cloudcannon/configuration-types": "0.0.12",
"@cloudcannon/configuration-types": "^0.0.13",
"@types/he": "^1.2.3",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.5.2",
"@types/node": "^22.5.4",
"ava": "^6.1.3",
"c8": "^10.1.2",
"eslint": "^9.9.1",
"eslint": "^9.10.0",
"prettier": "^3.3.3",
"typescript": "^5.5.4"
"typescript": "^5.6.2"
},
"license": "MIT",
"dependencies": {
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export async function generateConfiguration(filePaths, options) {
source,
collections_config:
options?.config?.collections_config ||
ssg.generateCollectionsConfig(collectionPaths, {
source,
config,
basePath: findBasePath(collectionPaths),
}),
ssg.sortCollectionsConfig(
ssg.generateCollectionsConfig(collectionPaths, {
source,
config,
basePath: findBasePath(collectionPaths),
}),
),
paths: options?.config?.paths ?? undefined,
timezone: options?.config?.timezone ?? ssg.getTimezone(),
markdown: options?.config?.markdown ?? ssg.generateMarkdown(config),
Expand Down
6 changes: 5 additions & 1 deletion src/ssgs/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export default class Eleventy extends Ssg {
*/
generateCollectionConfig(key, path, options) {
const collectionConfig = super.generateCollectionConfig(key, path, options);
collectionConfig.output = !(path === '_data' || path.endsWith('/_data'));

if (path === '_data' || path.endsWith('/_data')) {
collectionConfig.disable_url = true;
}

return collectionConfig;
}

Expand Down
5 changes: 4 additions & 1 deletion src/ssgs/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export default class Hugo extends Ssg {
}

const dataPath = this.getHugoDataPath(options.config);
collectionConfig.output = !(path === dataPath || path.endsWith(`/${dataPath}`));

if (path === dataPath || path.endsWith(`/${dataPath}`)) {
collectionConfig.disable_url = true;
}

return collectionConfig;
}
Expand Down
26 changes: 10 additions & 16 deletions src/ssgs/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,6 @@ function getJekyllCollections(collections) {
return formatted;
}

/**
* Checks if a Jekyll collection is output.
*
* @param key {string}
* @param collection {Record<string, any> | undefined}
* @returns {boolean}
*/
function isCollectionOutput(key, collection) {
if (key === 'data' || key === 'drafts' || key.endsWith('_drafts')) {
return false;
}

return key === 'pages' || key === 'posts' || key.endsWith('_posts') || !!collection?.output;
}

export default class Jekyll extends Ssg {
constructor() {
super('jekyll');
Expand Down Expand Up @@ -145,7 +130,16 @@ export default class Jekyll extends Ssg {
generateCollectionConfig(key, path, options) {
const collectionConfig = super.generateCollectionConfig(key, path);

collectionConfig.output = isCollectionOutput(key, options.collection);
const isKnownOutputCollection =
key === 'pages' ||
key === 'posts' ||
key.endsWith('_posts') ||
key === 'drafts' ||
key.endsWith('_drafts');

if (key === 'data' || (!options.collection?.output && !isKnownOutputCollection)) {
collectionConfig.disable_url = true;
}

if (options.collection?.sort_by && typeof options.collection?.sort_by === 'string') {
collectionConfig.sort = { key: options.collection.sort_by };
Expand Down
41 changes: 40 additions & 1 deletion src/ssgs/ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ export default class Ssg {
path,
name,
icon: findIcon(name.toLowerCase()),
output: true,
};
}

Expand Down Expand Up @@ -433,6 +432,46 @@ export default class Ssg {
return collectionsConfig;
}

/**
* Generates collections config from a set of paths.
*
* @param collectionsConfig {import('../types').CollectionsConfig}
* @returns {import('../types').CollectionsConfig}
*/
sortCollectionsConfig(collectionsConfig) {
/** @type {import('../types').CollectionsConfig} */
const sorted = {};

const sortedKeys = Object.keys(collectionsConfig).sort((a, b) => {
const aCollectionConfig = collectionsConfig[a];
const bCollectionConfig = collectionsConfig[b];

if (
a === 'pages' ||
aCollectionConfig.path === '' ||
(!aCollectionConfig.disable_url && bCollectionConfig.disable_url)
) {
return -1;
}

if (
b === 'pages' ||
bCollectionConfig.path === '' ||
(!bCollectionConfig.disable_url && aCollectionConfig.disable_url)
) {
return 1;
}

return a.localeCompare(b);
});

for (const key of sortedKeys) {
sorted[key] = collectionsConfig[key];
}

return sorted;
}

/**
* @param _config {Record<string, any> | undefined}
* @returns {import('@cloudcannon/configuration-types').MarkdownSettings}
Expand Down
8 changes: 3 additions & 5 deletions toolproof_tests/eleventy/base.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ steps:
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ "icon": "wysiwyg"
╎ },
╎ "blog": {
╎ "path": "blog",
╎ "name": "Blog",
╎ "icon": "event_available",
╎ "output": true
╎ "icon": "event_available"
╎ },
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ "disable_url": true
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
Expand Down
8 changes: 3 additions & 5 deletions toolproof_tests/eleventy/subfolder-source.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ steps:
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ "icon": "wysiwyg"
╎ },
╎ "blog": {
╎ "path": "blog",
╎ "name": "Blog",
╎ "icon": "event_available",
╎ "output": true
╎ "icon": "event_available"
╎ },
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ "disable_url": true
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
Expand Down
18 changes: 8 additions & 10 deletions toolproof_tests/eleventy/templates-in-source.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@ steps:
╎ "config": {
╎ "source": "sauce",
╎ "collections_config": {
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "pages": {
╎ "path": "content",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ "icon": "wysiwyg"
╎ },
╎ "blog": {
╎ "path": "content/blog",
╎ "name": "Blog",
╎ "icon": "event_available",
╎ "output": true
╎ "icon": "event_available"
╎ },
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "disable_url": true
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
Expand Down
22 changes: 10 additions & 12 deletions toolproof_tests/hugo/base.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,27 @@ steps:
╎ "ssg": "hugo",
╎ "config": {
╎ "collections_config": {
╎ "data": {
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false,
╎ "glob": [
╎ "!_index.md"
╎ ]
╎ },
╎ "pages": {
╎ "path": "content",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ "icon": "wysiwyg"
╎ },
╎ "blog": {
╎ "path": "content/blog",
╎ "name": "Blog",
╎ "icon": "event_available",
╎ "output": true,
╎ "glob": [
╎ "!_index.md"
╎ ]
╎ },
╎ "data": {
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
Expand Down
Loading

0 comments on commit 6d5af02

Please sign in to comment.