feat(content-blog): parse date from middle of file path (#6245)

This commit is contained in:
Joshua Chen 2022-01-20 16:43:00 +08:00 committed by GitHub
parent 29ecf22409
commit d133910cb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 18 deletions

View file

@ -108,4 +108,26 @@ describe('parseBlogFileName', () => {
slug: '/2021/05/12/announcing-docusaurus-two-beta/subfolder/subfile', slug: '/2021/05/12/announcing-docusaurus-two-beta/subfolder/subfile',
}); });
}); });
test('parse date in the middle of path', () => {
expect(
parseBlogFileName('team-a/2021/05/12/announcing-docusaurus-two-beta.md'),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'announcing-docusaurus-two-beta',
slug: '/2021/05/12/team-a/announcing-docusaurus-two-beta',
});
});
test('parse date in the middle of a folder name', () => {
expect(
parseBlogFileName(
'team-a-2021-05-12-hey/announcing-docusaurus-two-beta.md',
),
).toEqual({
date: new Date('2021-05-12Z'),
text: 'hey/announcing-docusaurus-two-beta',
slug: '/2021/05/12/team-a-hey/announcing-docusaurus-two-beta',
});
});
}); });

View file

@ -63,7 +63,7 @@ export function getBlogTags(blogPosts: BlogPost[]): BlogTags {
} }
const DATE_FILENAME_REGEX = const DATE_FILENAME_REGEX =
/^(?<date>\d{4}[-/]\d{1,2}[-/]\d{1,2})[-/]?(?<text>.*?)(\/index)?.mdx?$/; /^(?<folder>.*)(?<date>\d{4}[-/]\d{1,2}[-/]\d{1,2})[-/]?(?<text>.*?)(\/index)?.mdx?$/;
type ParsedBlogFileName = { type ParsedBlogFileName = {
date: Date | undefined; date: Date | undefined;
@ -76,12 +76,11 @@ export function parseBlogFileName(
): ParsedBlogFileName { ): ParsedBlogFileName {
const dateFilenameMatch = blogSourceRelative.match(DATE_FILENAME_REGEX); const dateFilenameMatch = blogSourceRelative.match(DATE_FILENAME_REGEX);
if (dateFilenameMatch) { if (dateFilenameMatch) {
const dateString = dateFilenameMatch.groups!.date!; const {folder, text, date: dateString} = dateFilenameMatch.groups!;
const text = dateFilenameMatch.groups!.text!;
// Always treat dates as UTC by adding the `Z` // Always treat dates as UTC by adding the `Z`
const date = new Date(`${dateString}Z`); const date = new Date(`${dateString}Z`);
const slugDate = dateString.replace(/-/g, '/'); const slugDate = dateString.replace(/-/g, '/');
const slug = `/${slugDate}/${text}`; const slug = `/${slugDate}/${folder}${text}`;
return {date, text, slug}; return {date, text, slug};
} else { } else {
const text = blogSourceRelative.replace(/(\/index)?\.mdx?$/, ''); const text = blogSourceRelative.replace(/(\/index)?\.mdx?$/, '');

View file

@ -1,3 +1,9 @@
# Hmmm! # Hmmm!
This is a blog post from an anonymous author! This is a blog post from an anonymous author!
```mdx-code-block
import Partial from "./_partial.mdx"
<Partial />
```

View file

@ -13,9 +13,3 @@ Did you know you can use multiple instances of the same plugin?
Using twice the blog plugin permits you to create more than one blog on the same Docusaurus website! Using twice the blog plugin permits you to create more than one blog on the same Docusaurus website!
::: :::
```mdx-code-block
import Partial from "./_partial.mdx"
<Partial />
```

View file

@ -43,6 +43,7 @@ const dogfoodingPluginInstances = [
frontMatter.hide_reading_time frontMatter.hide_reading_time
? undefined ? undefined
: defaultReadingTime({content, options: {wordsPerMinute: 5}}), : defaultReadingTime({content, options: {wordsPerMinute: 5}}),
sortPosts: 'ascending',
}), }),
], ],

View file

@ -78,14 +78,18 @@ This naming convention is optional, and you can provide the date as front matter
<details> <details>
<summary>Example supported patterns</summary> <summary>Example supported patterns</summary>
- `2021-05-28-my-blog-post-title.md` | Pattern | Example |
- `2021-05-28-my-blog-post-title.mdx` | --- | --- |
- `2021-05-28-my-blog-post-title/index.md` | Single file | `2021-05-28-my-blog-post-title.md` |
- `2021-05-28/my-blog-post-title.md` | MDX file | `2021-05-28-my-blog-post-title.mdx` |
- `2021/05/28/my-blog-post-title.md` | Single folder + `index.md` | `2021-05-28-my-blog-post-title/index.md` |
- `2021/05-28-my-blog-post-title.md` | Folder named by date | `2021-05-28/my-blog-post-title.md` |
- `2021/05/28/my-blog-post-title/index.md` | Nested folders by date | `2021/05/28/my-blog-post-title.md` |
- ... | Partially nested folders by date | `2021/05-28-my-blog-post-title.md` |
| Nested folders + `index.md` | `2021/05/28/my-blog-post-title/index.md` |
| Date in the middle of path | `category/2021/05-28-my-blog-post-title.md` |
The date will be excised from the path and appended to the beginning of the URL slug.
</details> </details>