-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[content collections] Handle file name spaces and capitalization (#5666)
* feat: slugify slug to handle capitals and spaces * docs: add not on Slugger issues * deps: bump to github-slugger 2.0 * refactor: new Slugger() -> slug util * fix: stop using URL.pathname * fix: `file://` prefix on isContentFlagImport * test: spaces in fixture file name * chore: add `test:unit:match` * refactor: handle collection errors from getEntryInfo * test: unit getEntryInfo * chore: changeset * chore: markdown-remark out of date * fix: correctly strip index on windows * fix: move to utils, fix slug regex * refactor: intermediate var * lint: `path` variable shadowing * chore: add not on allowFilesOutsideCollection
- Loading branch information
1 parent
19be918
commit bf210f7
Showing
11 changed files
with
158 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Correctly handle spaces and capitalization in `src/content/` file names. This introduces github-slugger for slug generation to ensure slugs are usable by `getStaticPaths`. Changes: | ||
- Resolve spaces and capitalization: `collection/Entry With Spaces.md` becomes `collection/entry-with-spaces`. | ||
- Truncate `/index` paths to base URL: `collection/index.md` becomes `collection` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/astro/test/units/content-collections/get-entry-info.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getEntryInfo } from '../../../dist/content/utils.js'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Content Collections - getEntryInfo', () => { | ||
const contentDir = new URL('src/content/', import.meta.url); | ||
|
||
it('Returns correct entry info', () => { | ||
const entry = new URL('blog/first-post.md', contentDir); | ||
const info = getEntryInfo({ entry, contentDir }); | ||
expect(info.id).to.equal('first-post.md'); | ||
expect(info.slug).to.equal('first-post'); | ||
expect(info.collection).to.equal('blog'); | ||
}); | ||
|
||
it('Returns correct slug when spaces used', () => { | ||
const entry = new URL('blog/first post.mdx', contentDir); | ||
const info = getEntryInfo({ entry, contentDir }); | ||
expect(info.slug).to.equal('first-post'); | ||
}); | ||
|
||
it('Returns correct slug when nested directories used', () => { | ||
const entry = new URL('blog/2021/01/01/index.md', contentDir); | ||
const info = getEntryInfo({ entry, contentDir }); | ||
expect(info.slug).to.equal('2021/01/01'); | ||
}); | ||
|
||
it('Returns correct collection when nested directories used', () => { | ||
const entry = new URL('blog/2021/01/01/index.md', contentDir); | ||
const info = getEntryInfo({ entry, contentDir }); | ||
expect(info.collection).to.equal('blog'); | ||
}); | ||
|
||
it('Returns error when outside collection directory', () => { | ||
const entry = new URL('blog.md', contentDir); | ||
expect(getEntryInfo({ entry, contentDir }) instanceof Error).to.equal(true); | ||
}); | ||
|
||
it('Silences error on `allowFilesOutsideCollection`', () => { | ||
const entry = new URL('blog.md', contentDir); | ||
const entryInfo = getEntryInfo({ entry, contentDir, allowFilesOutsideCollection: true }); | ||
expect(entryInfo instanceof Error).to.equal(false); | ||
expect(entryInfo.id).to.equal('blog.md'); | ||
}); | ||
}); |
Oops, something went wrong.