docs/node_modules/@docusaurus/plugin-content-blog/lib/index.js
// Fetches blog contents and returns metadata for the necessary routes.
Fetches blog contents and returns metadata for the necessary routes.
Fetches blog contents and returns metadata for the necessary routes .
async loadContent() {
            const { postsPerPage, routeBasePath } = options;
            blogPosts = await blogUtils_1.generateBlogPosts(contentPaths, context, options);
            if (!blogPosts.length) {
                return null;
            }
            blogPosts.forEach((blogPost, index) => {
                const prevItem = index > 0 ? blogPosts[index - 1] : null;
                if (prevItem) {
                    blogPost.metadata.prevItem = {
                        title: prevItem.metadata.title,
                        permalink: prevItem.metadata.permalink,
                    };
                }
                const nextItem = index < blogPosts.length - 1 ? blogPosts[index + 1] : null;
                if (nextItem) {
                    blogPost.metadata.nextItem = {
                        title: nextItem.metadata.title,
                        permalink: nextItem.metadata.permalink,
                    };
                }
            });
            const totalCount = blogPosts.length;
            const numberOfPages = Math.ceil(totalCount / postsPerPage);
            const { siteConfig: { baseUrl = '' }, } = context;
            const basePageUrl = utils_1.normalizeUrl([baseUrl, routeBasePath]);
            const blogListPaginated = [];
            function blogPaginationPermalink(page) {
                return page > 0
                    ? utils_1.normalizeUrl([basePageUrl, `page/${page + 1}`])
                    : basePageUrl;
            }
            for (let page = 0; page < numberOfPages; page += 1) {
                blogListPaginated.push({
                    metadata: {
                        permalink: blogPaginationPermalink(page),
                        page: page + 1,
                        postsPerPage,
                        totalPages: numberOfPages,
                        totalCount,
                        previousPage: page !== 0 ? blogPaginationPermalink(page - 1) : null,
                        nextPage: page < numberOfPages - 1
                            ? blogPaginationPermalink(page + 1)
                            : null,
                        blogDescription: options.blogDescription,
                        blogTitle: options.blogTitle,
                    },
                    items: blogPosts
                        .slice(page * postsPerPage, (page + 1) * postsPerPage)
                        .map((item) => item.id),
                });
            }
            const blogTags = {};
            const tagsPath = utils_1.normalizeUrl([basePageUrl, 'tags']);
            blogPosts.forEach((blogPost) => {
                const { tags } = blogPost.metadata;
                if (!tags || tags.length === 0) {
                    blogPost.metadata.tags = [];
                    return;
                }
                blogPost.metadata.tags = tags.map((tag) => {
                    if (typeof tag === 'string') {
                        const normalizedTag = lodash_1.kebabCase(tag);
                        const permalink = utils_1.normalizeUrl([tagsPath, normalizedTag]);
                        if (!blogTags[normalizedTag]) {
                            blogTags[normalizedTag] = {
                                name: tag.toLowerCase(),
                                items: [],
                                permalink,
                            };
                        }
                        blogTags[normalizedTag].items.push(blogPost.id);
                        return {
                            label: tag,
                            permalink,
                        };
                    }
                    return tag;
                });
            });
            const blogTagsListPath = Object.keys(blogTags).length > 0 ? tagsPath : null;
            return {
                blogPosts,
                blogListPaginated,
                blogTags,
                blogTagsListPath,
            };
        }
async loadContent ( ) { const { postsPerPage , routeBasePath } = options ; blogPosts = await blogUtils_1 . generateBlogPosts ( contentPaths , context , options ) ; if ( ! blogPosts . length ) { return null ; } blogPosts . forEach ( ( blogPost , index ) => { const prevItem = index > 0 ? blogPosts [ index - 1 ] : null ; if ( prevItem ) { blogPost . metadata . prevItem = { title : prevItem . metadata . title , permalink : prevItem . metadata . permalink , } ; } const nextItem = index < blogPosts . length - 1 ? blogPosts [ index + 1 ] : null ; if ( nextItem ) { blogPost . metadata . nextItem = { title : nextItem . metadata . title , permalink : nextItem . metadata . permalink , } ; } } ) ; const totalCount = blogPosts . length ; const numberOfPages = Math . ceil ( totalCount / postsPerPage ) ; const { siteConfig : { baseUrl = '' } , } = context ; const basePageUrl = utils_1 . normalizeUrl ( [ baseUrl , routeBasePath ] ) ; const blogListPaginated = [ ] ; function blogPaginationPermalink ( page ) { return page > 0 ? utils_1 . normalizeUrl ( [ basePageUrl , ` ${ page + 1 } ` ] ) : basePageUrl ; } for ( let page = 0 ; page < numberOfPages ; page += 1 ) { blogListPaginated . push ( { metadata : { permalink : blogPaginationPermalink ( page ) , page : page + 1 , postsPerPage , totalPages : numberOfPages , totalCount , previousPage : page !== 0 ? blogPaginationPermalink ( page - 1 ) : null , nextPage : page < numberOfPages - 1 ? blogPaginationPermalink ( page + 1 ) : null , blogDescription : options . blogDescription , blogTitle : options . blogTitle , } , items : blogPosts . slice ( page * postsPerPage , ( page + 1 ) * postsPerPage ) . map ( ( item ) => item . id ) , } ) ; } const blogTags = { } ; const tagsPath = utils_1 . normalizeUrl ( [ basePageUrl , 'tags' ] ) ; blogPosts . forEach ( ( blogPost ) => { const { tags } = blogPost . metadata ; if ( ! tags || tags . length === 0 ) { blogPost . metadata . tags = [ ] ; return ; } blogPost . metadata . tags = tags . map ( ( tag ) => { if ( typeof tag === 'string' ) { const normalizedTag = lodash_1 . kebabCase ( tag ) ; const permalink = utils_1 . normalizeUrl ( [ tagsPath , normalizedTag ] ) ; if ( ! blogTags [ normalizedTag ] ) { blogTags [ normalizedTag ] = { name : tag . toLowerCase ( ) , items : [ ] , permalink , } ; } blogTags [ normalizedTag ] . items . push ( blogPost . id ) ; return { label : tag , permalink , } ; } return tag ; } ) ; } ) ; const blogTagsListPath = Object . keys ( blogTags ) . length > 0 ? tagsPath : null ; return { blogPosts , blogListPaginated , blogTags , blogTagsListPath , } ; }
Fetches blog contents and returns metadata for the necessary routes.
Fetches blog contents and returns metadata for the necessary routes .
// Colocate next and prev metadata. // Blog pagination routes. // Example: `/blog`, `/blog/page/1`, `/blog/page/2` // TODO: Extract tags out into a separate plugin. // eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign // Will only use the name of the first occurrence of the tag.