Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent creating collections for hugo folders with only branch index files #20

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function generateConfiguration(filePaths, options) {
source,
config,
basePath: findBasePath(collectionPaths),
filePaths,
}),
),
paths: options?.config?.paths ?? undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/ssgs/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class Eleventy extends Ssg {
*
* @param key {string}
* @param path {string}
* @param options {{ config?: Record<string, any>; basePath?: string; }=}
* @param options {import('../types').GenerateCollectionConfigOptions}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
Expand All @@ -86,7 +86,7 @@ export default class Eleventy extends Ssg {
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, options) {
Expand Down
25 changes: 21 additions & 4 deletions src/ssgs/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ export default class Hugo extends Ssg {
*
* @param key {string}
* @param path {string}
* @param options {{ config?: Record<string, any>; basePath: string; }}
* @param options {import('../types').GenerateCollectionConfigOptions}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
const collectionConfig = super.generateCollectionConfig(key, path, options);

if (path !== options.basePath) {
const hasParentCollectionPath = options.collectionPaths.some((otherCollectionPath) => {
return joinPaths([options.source, path]).startsWith(`${otherCollectionPath}/`);
});

if (hasParentCollectionPath) {
collectionConfig.glob =
typeof collectionConfig.glob === 'string'
? [collectionConfig.glob]
Expand Down Expand Up @@ -107,7 +111,7 @@ export default class Hugo extends Ssg {
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, options) {
Expand All @@ -119,10 +123,23 @@ export default class Hugo extends Ssg {
* Generates collections config from a set of paths.
*
* @param collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {import('../types').CollectionsConfig}
*/
generateCollectionsConfig(collectionPaths, options) {
// Remove collection paths with a parent collection path and only a branch index file
collectionPaths = collectionPaths.filter((collectionPath) => {
const hasNonBranchIndexFile = options.filePaths.some(
(filePath) => filePath.startsWith(`${collectionPath}/`) && !filePath.endsWith('/_index.md'),
);

const hasParentCollectionPath = collectionPaths.some((otherCollectionPath) =>
collectionPath.startsWith(`${otherCollectionPath}/`),
);

return hasNonBranchIndexFile || !hasParentCollectionPath;
});

const collectionPathsOutsideExampleSite = collectionPaths.filter(
(path) => !path.includes('exampleSite/'),
);
Expand Down
26 changes: 20 additions & 6 deletions src/ssgs/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ export default class Jekyll extends Ssg {
*
* @param key {string}
* @param path {string}
* @param options {{ config?: Record<string, any>; basePath?: string; collection: Record<string, any> | undefined; }}
* @param options {import('../types').GenerateCollectionConfigOptionsJekyll}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
const collectionConfig = super.generateCollectionConfig(key, path);
const collectionConfig = super.generateCollectionConfig(key, path, options);

const isKnownOutputCollection =
key === 'pages' ||
Expand Down Expand Up @@ -204,7 +204,7 @@ export default class Jekyll extends Ssg {
* Generates collections config from a set of paths.
*
* @param collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {import('../types').CollectionsConfig}
*/
generateCollectionsConfig(collectionPaths, options) {
Expand All @@ -220,6 +220,8 @@ export default class Jekyll extends Ssg {
const collection = collections[key];

collectionsConfig[collectionKey] = this.generateCollectionConfig(collectionKey, path, {
...options,
collectionPaths,
collection,
});
}
Expand Down Expand Up @@ -255,7 +257,11 @@ export default class Jekyll extends Ssg {
const pathInCollectionsDir = stripTopPath(path, collectionsDir);
const key = this.generateCollectionsConfigKey(pathInCollectionsDir, collectionsConfig);
const collection = collections[stripTopPath(path, collectionsDir).replace(/^\/?_/, '')];
collectionsConfig[key] = this.generateCollectionConfig(key, path, { collection });
collectionsConfig[key] = this.generateCollectionConfig(key, path, {
...options,
collectionPaths,
collection,
});
}

// Add matching post/draft collections
Expand All @@ -271,15 +277,23 @@ export default class Jekyll extends Ssg {
collectionsConfig[postsKey] ||= this.generateCollectionConfig(
postsKey,
toPostsPath(collectionConfig.path),
{ collection: collections?.posts },
{
...options,
collectionPaths,
collection: collections?.posts,
},
);
} else if (isPostsPath(collectionConfig.path)) {
// Ensure there is a matching drafts collection
const draftsKey = toDraftsKey(key);
collectionsConfig[draftsKey] ||= this.generateCollectionConfig(
draftsKey,
toDraftsPath(collectionConfig.path),
{ collection: collections?.drafts || collections?.posts },
{
...options,
collectionPaths,
collection: collections?.drafts || collections?.posts,
},
);
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/ssgs/ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export default class Ssg {
*
* @param key {string}
* @param path {string}
* @param _options {{ config?: Record<string, any>; basePath?: string; }=}
* @param _options {import('../types').GenerateCollectionConfigOptions}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, _options) {
Expand Down Expand Up @@ -385,7 +385,7 @@ export default class Ssg {
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param _options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param _options {import('../types').GenerateCollectionsConfigOptions}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, _options) {
Expand All @@ -396,7 +396,7 @@ export default class Ssg {
* Generates collections config from a set of paths.
*
* @param collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {import('../types').CollectionsConfig}
*/
generateCollectionsConfig(collectionPaths, options) {
Expand Down Expand Up @@ -426,7 +426,11 @@ export default class Ssg {
}

const key = this.generateCollectionsConfigKey(pathInBasePath, collectionsConfig);
collectionsConfig[key] = this.generateCollectionConfig(key, path, { ...options, basePath });
collectionsConfig[key] = this.generateCollectionConfig(key, path, {
...options,
collectionPaths,
basePath,
});
}

return collectionsConfig;
Expand Down
11 changes: 7 additions & 4 deletions src/ssgs/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ export default class Static extends Ssg {
/**
* Generates collections config from a set of paths.
*
* @param _collectionPaths {string[]}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string }}
* @param collectionPaths {string[]}
* @param options {import('../types').GenerateCollectionsConfigOptions}
* @returns {import('../types').CollectionsConfig}
*/
generateCollectionsConfig(_collectionPaths, options) {
generateCollectionsConfig(collectionPaths, options) {
return {
pages: this.generateCollectionConfig('pages', options?.source ?? ''),
pages: this.generateCollectionConfig('pages', options?.source ?? '', {
...options,
collectionPaths,
}),
};
}
}
16 changes: 16 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ export interface BuildCommands {
environment: Record<string, BuildCommandSuggestion>;
preserved: BuildCommandSuggestion[];
}

export interface GenerateCollectionsConfigOptions {
config?: Record<string, any>;
source?: string;
basePath: string;
filePaths: string[];
}

export interface GenerateCollectionConfigOptions extends GenerateCollectionsConfigOptions {
collectionPaths: string[];
}

export interface GenerateCollectionConfigOptionsJekyll extends GenerateCollectionConfigOptions {
/** The matching Jekyll collection from _config.yml */
collection: Record<string, any> | undefined;
}
3 changes: 0 additions & 3 deletions toolproof_tests/hugo/base.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ steps:
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true,
╎ "disable_add": true,
╎ "disable_add_folder": true,
Expand Down
3 changes: 0 additions & 3 deletions toolproof_tests/hugo/data-dir.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ steps:
╎ "path": "stats",
╎ "name": "Stats",
╎ "icon": "stars",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true,
╎ "disable_add": true,
╎ "disable_add_folder": true,
Expand Down
3 changes: 0 additions & 3 deletions toolproof_tests/hugo/data-only.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ steps:
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true,
╎ "disable_add": true,
╎ "disable_add_folder": true,
Expand Down
3 changes: 0 additions & 3 deletions toolproof_tests/hugo/example-site-only.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ steps:
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true,
╎ "disable_add": true,
╎ "disable_add_folder": true,
Expand Down
38 changes: 38 additions & 0 deletions toolproof_tests/hugo/one-branch-index.toolproof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Hugo one branch index

steps:
- step: I have a "src/hugo.yaml" file with the content ""
- step: I have a "src/content/_index.md" file with the content ""
- ref: ./../core/run_gadget.toolproof.yml
- snapshot: stdout
snapshot_content: |-
╎{
╎ "ssg": "hugo",
╎ "config": {
╎ "collections_config": {
╎ "content": {
╎ "path": "content",
╎ "name": "Content",
╎ "icon": "wysiwyg"
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
╎ "markdown": {
╎ "engine": "commonmark",
╎ "options": {
╎ "gfm": true,
╎ "linkify": false,
╎ "table": false,
╎ "strikethrough": false,
╎ "subscript": false,
╎ "superscript": false,
╎ "heading_ids": false,
╎ "breaks": false,
╎ "xhtml": false,
╎ "attributes": false,
╎ "typographer": false,
╎ "treat_indentation_as_code": true
╎ }
╎ }
╎ }
╎}
38 changes: 38 additions & 0 deletions toolproof_tests/hugo/one-nested-branch-index.toolproof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Hugo one nested branch index

steps:
- step: I have a "src/hugo.yaml" file with the content ""
- step: I have a "src/content/blog/_index.md" file with the content ""
- ref: ./../core/run_gadget.toolproof.yml
- snapshot: stdout
snapshot_content: |-
╎{
╎ "ssg": "hugo",
╎ "config": {
╎ "collections_config": {
╎ "content_blog": {
╎ "path": "content/blog",
╎ "name": "Content Blog",
╎ "icon": "content_copy"
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
╎ "markdown": {
╎ "engine": "commonmark",
╎ "options": {
╎ "gfm": true,
╎ "linkify": false,
╎ "table": false,
╎ "strikethrough": false,
╎ "subscript": false,
╎ "superscript": false,
╎ "heading_ids": false,
╎ "breaks": false,
╎ "xhtml": false,
╎ "attributes": false,
╎ "typographer": false,
╎ "treat_indentation_as_code": true
╎ }
╎ }
╎ }
╎}
5 changes: 1 addition & 4 deletions toolproof_tests/hugo/one-nested-page.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ steps:
╎ "content_blog": {
╎ "path": "content/blog",
╎ "name": "Content Blog",
╎ "icon": "content_copy",
╎ "glob": [
╎ "!_index.md"
╎ ]
╎ "icon": "content_copy"
╎ }
╎ },
╎ "timezone": "Pacific/Auckland",
Expand Down
3 changes: 0 additions & 3 deletions toolproof_tests/hugo/subfolder-source.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ steps:
╎ "path": "data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "glob": [
╎ "!_index.md"
╎ ],
╎ "disable_url": true,
╎ "disable_add": true,
╎ "disable_add_folder": true,
Expand Down
Loading
Loading