refactor(mdx-loader): read metadata from memory (loaded content) instead of fs (#10457)

* mdx loader shouldn't read metadata from file system but from memory

* comments

* refactor: apply lint autofix

* apply same for blog

* apply same for blog

* refactor: apply lint autofix

* apply same for pages
This commit is contained in:
Sébastien Lorber 2024-08-30 08:02:26 +02:00 committed by GitHub
parent 2aef92cb9e
commit a4329d3388
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 182 additions and 80 deletions

View file

@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {BlogContent, BlogPost} from '@docusaurus/plugin-content-blog';
function indexBlogPostsBySource(content: BlogContent): Map<string, BlogPost> {
return new Map(
content.blogPosts.map((blogPost) => [blogPost.metadata.source, blogPost]),
);
}
// TODO this is bad, we should have a better way to do this (new lifecycle?)
// The source to blog/permalink is a mutable map passed to the mdx loader
// See https://github.com/facebook/docusaurus/pull/10457
// See https://github.com/facebook/docusaurus/pull/10185
export function createContentHelpers() {
const sourceToBlogPost = new Map<string, BlogPost>();
const sourceToPermalink = new Map<string, string>();
// Mutable map update :/
function updateContent(content: BlogContent): void {
sourceToBlogPost.clear();
sourceToPermalink.clear();
indexBlogPostsBySource(content).forEach((value, key) => {
sourceToBlogPost.set(key, value);
sourceToPermalink.set(key, value.metadata.permalink);
});
}
return {updateContent, sourceToBlogPost, sourceToPermalink};
}