Skip to content

Commit

Permalink
Merge pull request #8 from CloudCannon/jekyll/use-defined-collections
Browse files Browse the repository at this point in the history
Use defined collections for Jekyll
  • Loading branch information
bglw authored Aug 27, 2024
2 parents 81ed8d0 + 07605be commit 75a7768
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 77 deletions.
55 changes: 40 additions & 15 deletions src/ssgs/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default class Jekyll extends Ssg {

collectionConfig.output = isCollectionOutput(key, options.collection);

if (options.collection?.sort_by) {
if (options.collection?.sort_by && typeof options.collection?.sort_by === 'string') {
collectionConfig.sort = { key: options.collection.sort_by };
}

Expand All @@ -199,9 +199,7 @@ export default class Jekyll extends Ssg {
{ name: `Add ${collectionConfig.singular_name || 'Post'}` },
{ name: 'Add Draft', collection: toDraftsKey(key) },
];
}

if (isDraftsPath(collectionConfig.path)) {
} else if (isDraftsPath(collectionConfig.path)) {
collectionConfig.create ||= {
path: '', // TODO: this should not be required if publish_to is set
publish_to: toPostsKey(key),
Expand All @@ -224,23 +222,50 @@ export default class Jekyll extends Ssg {
const collectionsDir = options?.config?.collections_dir || '';
const collections = getJekyllCollections(options?.config?.collections);

// Content folder to collections_config mapping.
for (const fullPath of collectionPaths.paths) {
// Handle defined collections.
for (const key of Object.keys(collections)) {
const collectionKey = key === 'pages' || key === 'data' ? `collection_${key}` : key;
const path = joinPaths([collectionsDir, `_${key}`]);
const collection = collections[key];

collectionsConfig[collectionKey] = this.generateCollectionConfig(collectionKey, path, {
collection,
});
}

const sortedPaths = collectionPaths.paths.sort((a, b) => a.length - b.length);

// Use detected content folders to handle automatic/default collections.
for (const fullPath of sortedPaths) {
const path = stripTopPath(fullPath, options?.source);
const pathInCollectionsDir = collectionsDir ? stripTopPath(path, collectionsDir) : path;

const isDefaultCollection =
path === '' ||
path === '_data' ||
path.startsWith('_data/') ||
path === '_posts' ||
path.endsWith('/_posts') ||
path === '_drafts' ||
path.endsWith('/_drafts');

if (!isDefaultCollection) {
continue;
}

const exists = Object.keys(collectionsConfig).some((key) => {
return collectionsConfig[key].path === path;
});

if (exists) {
continue;
}

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

// Handle defined collections without files.
for (const key of Object.keys(collections)) {
collectionsConfig[key] ||= {
path: joinPaths([collectionsDir, `_${key}`]),
output: isCollectionOutput(key, collections[key]),
};
}

// Add matching post/draft collections
for (const key of Object.keys(collectionsConfig)) {
const collectionConfig = collectionsConfig[key];
Expand Down
2 changes: 1 addition & 1 deletion test_interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ cd $SCRIPT_DIR
GADGET_DIR=$(realpath "$SCRIPT_DIR/src/")
TEST_SITES=$(realpath "$SCRIPT_DIR/toolproof_tests/test_sites")

npx -y toolproof --placeholders gadget_dir="$GADGET_DIR" test_sites="$TEST_SITES" --all -i
npx -y toolproof --placeholders gadget_dir="$GADGET_DIR" test_sites="$TEST_SITES" -i
67 changes: 40 additions & 27 deletions toolproof_tests/jekyll/base.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ steps:
collections_dir: collections
collections:
- staff
- data
- step: I have a "src/collections/_posts/1999-12-31-partying.md" file with the content {md}
md: |-
---
Expand All @@ -17,12 +18,12 @@ steps:
---
title: No more partying
---
- step: I have a "src/collections/data/clash.md" file with the content {md}
- step: I have a "src/collections/_data/clash.md" file with the content {md}
md: |-
---
title: Not site.data
---
- step: I have a "src/collections/staff/jane-doe.md" file with the content {md}
- step: I have a "src/collections/_staff/jane-doe.md" file with the content {md}
md: |-
---
_uuid: 05589684-8d33-4d2f-8fde-460f9922d319
Expand All @@ -37,11 +38,23 @@ steps:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<p>Hello.</p>
<h1>Home</h1>
<p>Hello.</p>
</body>
</html>
- step: I have a "src/about/index.html" file with the content {html}
html: |-
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>About</h1>
<p>Hello.</p>
</body>
</html>
- step: I have a "src/_data/tags.yml" file with the content {yaml}
Expand All @@ -62,18 +75,36 @@ steps:
╎ "config": {
╎ "source": "",
╎ "collections_config": {
╎ "data_animals": {
╎ "path": "_data/animals",
╎ "name": "Animals",
╎ "icon": "animation",
╎ "staff": {
╎ "path": "collections/_staff",
╎ "name": "Staff",
╎ "icon": "people",
╎ "output": false
╎ },
╎ "collection_data": {
╎ "path": "collections/_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "data_animals": {
╎ "path": "_data/animals",
╎ "name": "Animals",
╎ "icon": "animation",
╎ "output": false
╎ },
╎ "posts": {
╎ "path": "collections/_posts",
╎ "name": "Posts",
Expand All @@ -92,12 +123,6 @@ steps:
╎ }
╎ ]
╎ },
╎ "data_1": {
╎ "path": "collections/data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "news_posts": {
╎ "path": "collections/news/_posts",
╎ "name": "Posts",
Expand All @@ -116,18 +141,6 @@ steps:
╎ }
╎ ]
╎ },
╎ "staff": {
╎ "path": "collections/staff",
╎ "name": "Staff",
╎ "icon": "people",
╎ "output": false
╎ },
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "drafts": {
╎ "path": "collections/_drafts",
╎ "name": "Drafts",
Expand Down
22 changes: 11 additions & 11 deletions toolproof_tests/jekyll/default-collections.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ steps:
╎ "config": {
╎ "source": "",
╎ "collections_config": {
╎ "data_animals": {
╎ "path": "_data/animals",
╎ "name": "Animals",
╎ "icon": "animation",
╎ "output": false
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "data": {
╎ "path": "_data",
Expand All @@ -71,12 +71,6 @@ steps:
╎ }
╎ ]
╎ },
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "news_posts": {
╎ "path": "news/_posts",
╎ "name": "Posts",
Expand All @@ -95,6 +89,12 @@ steps:
╎ }
╎ ]
╎ },
╎ "data_animals": {
╎ "path": "_data/animals",
╎ "name": "Animals",
╎ "icon": "animation",
╎ "output": false
╎ },
╎ "drafts": {
╎ "path": "_drafts",
╎ "name": "Drafts",
Expand Down
2 changes: 2 additions & 0 deletions toolproof_tests/jekyll/empty-collection.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ steps:
╎ "collections_config": {
╎ "has_no_files": {
╎ "path": "_has_no_files",
╎ "name": "Has No Files",
╎ "icon": "audio_file",
╎ "output": true
╎ }
╎ },
Expand Down
4 changes: 2 additions & 2 deletions toolproof_tests/jekyll/sort.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ steps:
collections:
staff:
sort_by: credentials
- step: I have a "src/staff/jane-doe.md" file with the content {md}
- step: I have a "src/_staff/jane-doe.md" file with the content {md}
md: |-
---
_uuid: 05589684-8d33-4d2f-8fde-460f9922d319
Expand All @@ -24,7 +24,7 @@ steps:
╎ "config": {
╎ "collections_config": {
╎ "staff": {
╎ "path": "staff",
╎ "path": "_staff",
╎ "name": "Staff",
╎ "icon": "people",
╎ "output": false,
Expand Down
42 changes: 21 additions & 21 deletions toolproof_tests/vonge_jekyll.toolproof.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ steps:
╎ "config": {
╎ "source": "site",
╎ "collections_config": {
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "pages": {
╎ "collection_pages": {
╎ "path": "collections/_pages",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "projects": {
╎ "path": "collections/_projects",
╎ "name": "Projects",
╎ "icon": "eject",
╎ "output": true
╎ },
╎ "testimonials": {
╎ "path": "collections/_testimonials",
╎ "name": "Testimonials",
╎ "icon": "festival",
╎ "output": false
╎ },
╎ "posts": {
╎ "path": "collections/_posts",
╎ "name": "Posts",
Expand All @@ -40,22 +46,16 @@ steps:
╎ }
╎ ]
╎ },
╎ "projects": {
╎ "path": "collections/_projects",
╎ "name": "Projects",
╎ "icon": "eject",
╎ "pages": {
╎ "path": "",
╎ "name": "Pages",
╎ "icon": "wysiwyg",
╎ "output": true
╎ },
╎ "testimonials": {
╎ "path": "collections/_testimonials",
╎ "name": "Testimonials",
╎ "icon": "festival",
╎ "output": false
╎ },
╎ "pages_1": {
╎ "path": "",
╎ "name": "Pages 1",
╎ "icon": "pages",
╎ "data": {
╎ "path": "_data",
╎ "name": "Data",
╎ "icon": "data_usage",
╎ "output": false
╎ },
╎ "drafts": {
Expand Down

0 comments on commit 75a7768

Please sign in to comment.