Skip to content

Commit

Permalink
Merge pull request #16 from CloudCannon/hugo/datadir
Browse files Browse the repository at this point in the history
Use Hugo dataDir from config
  • Loading branch information
rphillips-cc authored Sep 4, 2024
2 parents 22dfaed + 6583de3 commit 2037039
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 20 deletions.
8 changes: 4 additions & 4 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 {{ basePath?: string; }=}
* @param options {{ config?: Record<string, any>; basePath?: string; }=}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
Expand All @@ -79,11 +79,11 @@ export default class Eleventy extends Ssg {
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param basePath {string}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, basePath) {
const dataPath = joinPaths([basePath, '_data']);
filterContentCollectionPaths(collectionPaths, options) {
const dataPath = joinPaths([options.basePath, '_data']);
return collectionPaths.filter((path) => path !== dataPath && !path.startsWith(`${dataPath}/`));
}

Expand Down
24 changes: 18 additions & 6 deletions src/ssgs/hugo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findBasePath } from '../collections.js';
import { decodeEntity, joinPaths } from '../utility.js';
import { decodeEntity, joinPaths, normalisePath } from '../utility.js';
import Ssg from './ssg.js';

export default class Hugo extends Ssg {
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class Hugo extends Ssg {
*
* @param key {string}
* @param path {string}
* @param options {{ basePath: string; }}
* @param options {{ config?: Record<string, any>; basePath: string; }}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
Expand All @@ -78,22 +78,34 @@ export default class Hugo extends Ssg {
collectionConfig.glob.push('!_index.md');
}

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

return collectionConfig;
}

/**
* Returns the path to the Hugo data folder, optionally prepended with a base path.
*
* @param config {Record<string, any> | undefined}
* @param basePath {string=}
* @returns {string}
*/
getHugoDataPath(config, basePath) {
return joinPaths([basePath, normalisePath(config?.dataDir ?? 'data') ?? 'data']);
}

/**
* Filters out collection paths that are collections, but exist in isolated locations.
* Used when a data folder (or similar) is causing all collections to group under one
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param basePath {string}
* @param options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, basePath) {
const dataPath = joinPaths([basePath, 'data']);
filterContentCollectionPaths(collectionPaths, options) {
const dataPath = this.getHugoDataPath(options.config, options.basePath);
return collectionPaths.filter((path) => path !== dataPath && !path.startsWith(`${dataPath}/`));
}

Expand Down
2 changes: 1 addition & 1 deletion src/ssgs/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default class Jekyll extends Ssg {
*
* @param key {string}
* @param path {string}
* @param options {{ basePath?: string; collection: Record<string, any> | undefined; }}
* @param options {{ config?: Record<string, any>; basePath?: string; collection: Record<string, any> | undefined; }}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, options) {
Expand Down
13 changes: 5 additions & 8 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 {{ basePath?: string; }=}
* @param _options {{ config?: Record<string, any>; basePath?: string; }=}
* @returns {import('@cloudcannon/configuration-types').CollectionConfig}
*/
generateCollectionConfig(key, path, _options) {
Expand Down Expand Up @@ -386,10 +386,10 @@ export default class Ssg {
* `collections_config` entry.
*
* @param collectionPaths {string[]}
* @param _basePath {string}
* @param _options {{ config?: Record<string, any>; source?: string; basePath: string; }}
* @returns {string[]}
*/
filterContentCollectionPaths(collectionPaths, _basePath) {
filterContentCollectionPaths(collectionPaths, _options) {
return collectionPaths;
}

Expand All @@ -401,10 +401,7 @@ export default class Ssg {
* @returns {import('../types').CollectionsConfig}
*/
generateCollectionsConfig(collectionPaths, options) {
const contentCollectionPaths = this.filterContentCollectionPaths(
collectionPaths,
options.basePath,
);
const contentCollectionPaths = this.filterContentCollectionPaths(collectionPaths, options);

const hasNonContentCollection =
collectionPaths.length && collectionPaths.length !== contentCollectionPaths.length;
Expand All @@ -430,7 +427,7 @@ export default class Ssg {
}

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

return collectionsConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/ssgs/ssgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const ssgValues = Object.values(ssgs);
* Finds the most likely SSG for a set of files.
*
* @param filePaths {string[]} A list of file paths.
* @returns {Bridgetown | Eleventy | Hugo | Jekyll | NextJs | Sveltekit | Ssg} The assumed SSG.
* @returns {Ssg} The assumed SSG.
*/
export function guessSsg(filePaths) {
/** @type {Record<import('@cloudcannon/configuration-types').SsgKey, number>} */
Expand Down
64 changes: 64 additions & 0 deletions toolproof_tests/hugo/data-dir.toolproof.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Hugo changed dataDir

steps:
- step: I have a "src/hugo.yaml" file with the content {yaml}
yaml: |-
dataDir: stats
- step: I have a "src/content/_index.md" file with the content ""
- step: I have a "src/content/blog/_index.md" file with the content ""
- step: I have a "src/content/blog/partying.md" file with the content ""
- step: I have a "src/content/blog/follow-up/no-more-partying.md" file with the content ""
- step: I have a "src/stats/party-locations.csv" file with the content ""
- step: I have a "src/exampleSite/content/please-ignore/no.md" file with the content ""
- ref: ./../core/run_gadget.toolproof.yml
- snapshot: stdout
snapshot_content: |-
╎{
╎ "ssg": "hugo",
╎ "config": {
╎ "collections_config": {
╎ "stats": {
╎ "path": "stats",
╎ "name": "Stats",
╎ "icon": "stars",
╎ "output": false,
╎ "glob": [
╎ "!_index.md"
╎ ]
╎ },
╎ "pages": {
╎ "path": "content",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "blog": {
╎ "path": "content/blog",
╎ "name": "Blog",
╎ "icon": "event_available",
╎ "output": true,
╎ "glob": [
╎ "!_index.md"
╎ ]
╎ }
╎ },
╎ "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
╎ }
╎ }
╎ }
╎}

0 comments on commit 2037039

Please sign in to comment.