From 07605be84263f1a4c22e6b9ffb9fe0cc8cae9d16 Mon Sep 17 00:00:00 2001 From: Ross Phillips <12723297+rphillips-cc@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:40:13 +1200 Subject: [PATCH] Use defined collections for Jekyll --- src/ssgs/jekyll.js | 55 ++++++++++----- test_interactive.sh | 2 +- toolproof_tests/jekyll/base.toolproof.yml | 67 +++++++++++-------- .../jekyll/default-collections.toolproof.yml | 22 +++--- .../jekyll/empty-collection.toolproof.yml | 2 + toolproof_tests/jekyll/sort.toolproof.yml | 4 +- toolproof_tests/vonge_jekyll.toolproof.yml | 42 ++++++------ 7 files changed, 117 insertions(+), 77 deletions(-) diff --git a/src/ssgs/jekyll.js b/src/ssgs/jekyll.js index 7007ef9..d635b9a 100644 --- a/src/ssgs/jekyll.js +++ b/src/ssgs/jekyll.js @@ -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 }; } @@ -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), @@ -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]; diff --git a/test_interactive.sh b/test_interactive.sh index 8385e9c..78b88b6 100755 --- a/test_interactive.sh +++ b/test_interactive.sh @@ -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 diff --git a/toolproof_tests/jekyll/base.toolproof.yml b/toolproof_tests/jekyll/base.toolproof.yml index 1cc5882..fab38ed 100644 --- a/toolproof_tests/jekyll/base.toolproof.yml +++ b/toolproof_tests/jekyll/base.toolproof.yml @@ -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: |- --- @@ -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 @@ -37,11 +38,23 @@ steps: - Home + Home -

Home

-

Hello.

+

Home

+

Hello.

+ + + - step: I have a "src/about/index.html" file with the content {html} + html: |- + + + + About + + +

About

+

Hello.

- step: I have a "src/_data/tags.yml" file with the content {yaml} @@ -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", @@ -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", @@ -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", diff --git a/toolproof_tests/jekyll/default-collections.toolproof.yml b/toolproof_tests/jekyll/default-collections.toolproof.yml index a5703a6..e42fe05 100644 --- a/toolproof_tests/jekyll/default-collections.toolproof.yml +++ b/toolproof_tests/jekyll/default-collections.toolproof.yml @@ -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", @@ -71,12 +71,6 @@ steps: ╎ } ╎ ] ╎ }, - ╎ "pages": { - ╎ "path": "", - ╎ "name": "Pages", - ╎ "icon": "wysiwyg", - ╎ "output": true - ╎ }, ╎ "news_posts": { ╎ "path": "news/_posts", ╎ "name": "Posts", @@ -95,6 +89,12 @@ steps: ╎ } ╎ ] ╎ }, + ╎ "data_animals": { + ╎ "path": "_data/animals", + ╎ "name": "Animals", + ╎ "icon": "animation", + ╎ "output": false + ╎ }, ╎ "drafts": { ╎ "path": "_drafts", ╎ "name": "Drafts", diff --git a/toolproof_tests/jekyll/empty-collection.toolproof.yml b/toolproof_tests/jekyll/empty-collection.toolproof.yml index 8a0104d..15a1493 100644 --- a/toolproof_tests/jekyll/empty-collection.toolproof.yml +++ b/toolproof_tests/jekyll/empty-collection.toolproof.yml @@ -15,6 +15,8 @@ steps: ╎ "collections_config": { ╎ "has_no_files": { ╎ "path": "_has_no_files", + ╎ "name": "Has No Files", + ╎ "icon": "audio_file", ╎ "output": true ╎ } ╎ }, diff --git a/toolproof_tests/jekyll/sort.toolproof.yml b/toolproof_tests/jekyll/sort.toolproof.yml index 9bc292e..6d62310 100644 --- a/toolproof_tests/jekyll/sort.toolproof.yml +++ b/toolproof_tests/jekyll/sort.toolproof.yml @@ -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 @@ -24,7 +24,7 @@ steps: ╎ "config": { ╎ "collections_config": { ╎ "staff": { - ╎ "path": "staff", + ╎ "path": "_staff", ╎ "name": "Staff", ╎ "icon": "people", ╎ "output": false, diff --git a/toolproof_tests/vonge_jekyll.toolproof.yml b/toolproof_tests/vonge_jekyll.toolproof.yml index 8009d7e..027f5af 100644 --- a/toolproof_tests/vonge_jekyll.toolproof.yml +++ b/toolproof_tests/vonge_jekyll.toolproof.yml @@ -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", @@ -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": {