mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-25 06:56:56 +02:00
feat: siteConfig.markdown.parseFrontMatter hook (#9624)
This commit is contained in:
parent
28e7298211
commit
affca7a9a2
27 changed files with 486 additions and 133 deletions
|
@ -8,6 +8,7 @@
|
|||
import {jest} from '@jest/globals';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import {DEFAULT_PARSE_FRONT_MATTER} from '@docusaurus/utils';
|
||||
import {DEFAULT_OPTIONS} from '../options';
|
||||
import {generateBlogPosts} from '../blogUtils';
|
||||
import {createBlogFeedFiles} from '../feed';
|
||||
|
@ -31,6 +32,8 @@ const DefaultI18N: I18n = {
|
|||
},
|
||||
};
|
||||
|
||||
const markdown = {parseFrontMatter: DEFAULT_PARSE_FRONT_MATTER};
|
||||
|
||||
function getBlogContentPaths(siteDir: string): BlogContentPaths {
|
||||
return {
|
||||
contentPath: path.resolve(siteDir, 'blog'),
|
||||
|
@ -72,6 +75,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
|
|||
baseUrl: '/',
|
||||
url: 'https://docusaurus.io',
|
||||
favicon: 'image/favicon.ico',
|
||||
markdown,
|
||||
};
|
||||
const outDir = path.join(siteDir, 'build-snap');
|
||||
|
||||
|
@ -110,6 +114,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
|
|||
baseUrl: '/myBaseUrl/',
|
||||
url: 'https://docusaurus.io',
|
||||
favicon: 'image/favicon.ico',
|
||||
markdown,
|
||||
};
|
||||
|
||||
// Build is quite difficult to mock, so we built the blog beforehand and
|
||||
|
@ -152,6 +157,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
|
|||
baseUrl: '/myBaseUrl/',
|
||||
url: 'https://docusaurus.io',
|
||||
favicon: 'image/favicon.ico',
|
||||
markdown,
|
||||
};
|
||||
|
||||
// Build is quite difficult to mock, so we built the blog beforehand and
|
||||
|
@ -204,6 +210,7 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
|
|||
baseUrl: '/myBaseUrl/',
|
||||
url: 'https://docusaurus.io',
|
||||
favicon: 'image/favicon.ico',
|
||||
markdown,
|
||||
};
|
||||
|
||||
// Build is quite difficult to mock, so we built the blog beforehand and
|
||||
|
|
|
@ -16,6 +16,7 @@ import type {
|
|||
LoadContext,
|
||||
I18n,
|
||||
Validate,
|
||||
MarkdownConfig,
|
||||
} from '@docusaurus/types';
|
||||
import type {
|
||||
BlogPost,
|
||||
|
@ -24,6 +25,24 @@ import type {
|
|||
EditUrlFunction,
|
||||
} from '@docusaurus/plugin-content-blog';
|
||||
|
||||
const markdown: MarkdownConfig = {
|
||||
format: 'mdx',
|
||||
mermaid: true,
|
||||
mdx1Compat: {
|
||||
comments: true,
|
||||
headingIds: true,
|
||||
admonitions: true,
|
||||
},
|
||||
parseFrontMatter: async (params) => {
|
||||
// Reuse the default parser
|
||||
const result = await params.defaultParseFrontMatter(params);
|
||||
if (result.frontMatter.title === 'Complex Slug') {
|
||||
result.frontMatter.custom_frontMatter = 'added by parseFrontMatter';
|
||||
}
|
||||
return result;
|
||||
},
|
||||
};
|
||||
|
||||
function findByTitle(
|
||||
blogPosts: BlogPost[],
|
||||
title: string,
|
||||
|
@ -81,6 +100,7 @@ const getPlugin = async (
|
|||
title: 'Hello',
|
||||
baseUrl: '/',
|
||||
url: 'https://docusaurus.io',
|
||||
markdown,
|
||||
} as DocusaurusConfig;
|
||||
return pluginContentBlog(
|
||||
{
|
||||
|
@ -242,6 +262,7 @@ describe('blog plugin', () => {
|
|||
slug: '/hey/my super path/héllô',
|
||||
title: 'Complex Slug',
|
||||
tags: ['date', 'complex'],
|
||||
custom_frontMatter: 'added by parseFrontMatter',
|
||||
},
|
||||
tags: [
|
||||
{
|
||||
|
|
|
@ -11,7 +11,7 @@ import _ from 'lodash';
|
|||
import logger from '@docusaurus/logger';
|
||||
import readingTime from 'reading-time';
|
||||
import {
|
||||
parseMarkdownString,
|
||||
parseMarkdownFile,
|
||||
normalizeUrl,
|
||||
aliasedSitePath,
|
||||
getEditUrl,
|
||||
|
@ -29,7 +29,7 @@ import {
|
|||
} from '@docusaurus/utils';
|
||||
import {validateBlogPostFrontMatter} from './frontMatter';
|
||||
import {type AuthorsMap, getAuthorsMap, getBlogPostAuthors} from './authors';
|
||||
import type {LoadContext} from '@docusaurus/types';
|
||||
import type {LoadContext, ParseFrontMatter} from '@docusaurus/types';
|
||||
import type {
|
||||
PluginOptions,
|
||||
ReadingTimeFunction,
|
||||
|
@ -180,10 +180,19 @@ function formatBlogPostDate(
|
|||
}
|
||||
}
|
||||
|
||||
async function parseBlogPostMarkdownFile(blogSourceAbsolute: string) {
|
||||
const markdownString = await fs.readFile(blogSourceAbsolute, 'utf-8');
|
||||
async function parseBlogPostMarkdownFile({
|
||||
filePath,
|
||||
parseFrontMatter,
|
||||
}: {
|
||||
filePath: string;
|
||||
parseFrontMatter: ParseFrontMatter;
|
||||
}) {
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
try {
|
||||
const result = parseMarkdownString(markdownString, {
|
||||
const result = await parseMarkdownFile({
|
||||
filePath,
|
||||
fileContent,
|
||||
parseFrontMatter,
|
||||
removeContentTitle: true,
|
||||
});
|
||||
return {
|
||||
|
@ -191,7 +200,7 @@ async function parseBlogPostMarkdownFile(blogSourceAbsolute: string) {
|
|||
frontMatter: validateBlogPostFrontMatter(result.frontMatter),
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error`Error while parsing blog post file path=${blogSourceAbsolute}.`;
|
||||
logger.error`Error while parsing blog post file path=${filePath}.`;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +216,10 @@ async function processBlogSourceFile(
|
|||
authorsMap?: AuthorsMap,
|
||||
): Promise<BlogPost | undefined> {
|
||||
const {
|
||||
siteConfig: {baseUrl},
|
||||
siteConfig: {
|
||||
baseUrl,
|
||||
markdown: {parseFrontMatter},
|
||||
},
|
||||
siteDir,
|
||||
i18n,
|
||||
} = context;
|
||||
|
@ -228,7 +240,10 @@ async function processBlogSourceFile(
|
|||
const blogSourceAbsolute = path.join(blogDirPath, blogSourceRelative);
|
||||
|
||||
const {frontMatter, content, contentTitle, excerpt} =
|
||||
await parseBlogPostMarkdownFile(blogSourceAbsolute);
|
||||
await parseBlogPostMarkdownFile({
|
||||
filePath: blogSourceAbsolute,
|
||||
parseFrontMatter,
|
||||
});
|
||||
|
||||
const aliasedSource = aliasedSitePath(blogSourceAbsolute, siteDir);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue