diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts index fb38885eba..4a8874dfb8 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts @@ -108,4 +108,26 @@ describe('parseBlogFileName', () => { 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', + }); + }); }); diff --git a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts index 60306e5f6e..ddda9dc016 100644 --- a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts +++ b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts @@ -63,7 +63,7 @@ export function getBlogTags(blogPosts: BlogPost[]): BlogTags { } const DATE_FILENAME_REGEX = - /^(?\d{4}[-/]\d{1,2}[-/]\d{1,2})[-/]?(?.*?)(\/index)?.mdx?$/; + /^(?.*)(?\d{4}[-/]\d{1,2}[-/]\d{1,2})[-/]?(?.*?)(\/index)?.mdx?$/; type ParsedBlogFileName = { date: Date | undefined; @@ -76,12 +76,11 @@ export function parseBlogFileName( ): ParsedBlogFileName { const dateFilenameMatch = blogSourceRelative.match(DATE_FILENAME_REGEX); if (dateFilenameMatch) { - const dateString = dateFilenameMatch.groups!.date!; - const text = dateFilenameMatch.groups!.text!; + const {folder, text, date: dateString} = dateFilenameMatch.groups!; // Always treat dates as UTC by adding the `Z` const date = new Date(`${dateString}Z`); const slugDate = dateString.replace(/-/g, '/'); - const slug = `/${slugDate}/${text}`; + const slug = `/${slugDate}/${folder}${text}`; return {date, text, slug}; } else { const text = blogSourceRelative.replace(/(\/index)?\.mdx?$/, ''); diff --git a/website/_dogfooding/_blog tests/2021-08-22-no-author.md b/website/_dogfooding/_blog tests/2021-08-22-no-author.md index 4be979f1f1..497129fed0 100644 --- a/website/_dogfooding/_blog tests/2021-08-22-no-author.md +++ b/website/_dogfooding/_blog tests/2021-08-22-no-author.md @@ -1,3 +1,9 @@ # Hmmm! This is a blog post from an anonymous author! + +```mdx-code-block +import Partial from "./_partial.mdx" + + +``` diff --git a/website/_dogfooding/_blog tests/2020-08-03-second-blog-intro.md b/website/_dogfooding/_blog tests/demo/2020-08-03-second-blog-intro.md similarity index 79% rename from website/_dogfooding/_blog tests/2020-08-03-second-blog-intro.md rename to website/_dogfooding/_blog tests/demo/2020-08-03-second-blog-intro.md index 25f84187c0..391c91f496 100644 --- a/website/_dogfooding/_blog tests/2020-08-03-second-blog-intro.md +++ b/website/_dogfooding/_blog tests/demo/2020-08-03-second-blog-intro.md @@ -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! ::: - -```mdx-code-block -import Partial from "./_partial.mdx" - - -``` diff --git a/website/_dogfooding/dogfooding.config.js b/website/_dogfooding/dogfooding.config.js index 84832cbfc9..25635f1a8f 100644 --- a/website/_dogfooding/dogfooding.config.js +++ b/website/_dogfooding/dogfooding.config.js @@ -43,6 +43,7 @@ const dogfoodingPluginInstances = [ frontMatter.hide_reading_time ? undefined : defaultReadingTime({content, options: {wordsPerMinute: 5}}), + sortPosts: 'ascending', }), ], diff --git a/website/docs/blog.mdx b/website/docs/blog.mdx index f8cd8b7bdf..6f40c70902 100644 --- a/website/docs/blog.mdx +++ b/website/docs/blog.mdx @@ -78,14 +78,18 @@ This naming convention is optional, and you can provide the date as front matter
Example supported patterns -- `2021-05-28-my-blog-post-title.md` -- `2021-05-28-my-blog-post-title.mdx` -- `2021-05-28-my-blog-post-title/index.md` -- `2021-05-28/my-blog-post-title.md` -- `2021/05/28/my-blog-post-title.md` -- `2021/05-28-my-blog-post-title.md` -- `2021/05/28/my-blog-post-title/index.md` -- ... +| Pattern | Example | +| --- | --- | +| Single file | `2021-05-28-my-blog-post-title.md` | +| MDX file | `2021-05-28-my-blog-post-title.mdx` | +| Single folder + `index.md` | `2021-05-28-my-blog-post-title/index.md` | +| Folder named by date | `2021-05-28/my-blog-post-title.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.