Skip to content

Commit

Permalink
feat: siteConfig.markdown.parseFrontMatter hook (#9624)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jan 5, 2024
1 parent 85e32fd commit 1a91145
Show file tree
Hide file tree
Showing 27 changed files with 486 additions and 133 deletions.
12 changes: 8 additions & 4 deletions packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fs from 'fs-extra';
import logger from '@docusaurus/logger';
import {
parseFrontMatter,
DEFAULT_PARSE_FRONT_MATTER,
escapePath,
getFileLoaderUtils,
getWebpackLoaderCompilerName,
Expand Down Expand Up @@ -133,7 +133,7 @@ function extractContentTitleData(data: {

export async function mdxLoader(
this: LoaderContext<Options>,
fileString: string,
fileContent: string,
): Promise<void> {
const compilerName = getWebpackLoaderCompilerName(this);
const callback = this.async();
Expand All @@ -143,11 +143,15 @@ export async function mdxLoader(

ensureMarkdownConfig(reqOptions);

const {frontMatter} = parseFrontMatter(fileString);
const {frontMatter} = await reqOptions.markdownConfig.parseFrontMatter({
filePath,
fileContent,
defaultParseFrontMatter: DEFAULT_PARSE_FRONT_MATTER,
});
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);

const preprocessedContent = preprocessor({
fileContent: fileString,
fileContent,
filePath,
admonitions: reqOptions.admonitions,
markdownConfig: reqOptions.markdownConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'),
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
LoadContext,
I18n,
Validate,
MarkdownConfig,
} from '@docusaurus/types';
import type {
BlogPost,
Expand All @@ -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,
Expand Down Expand Up @@ -81,6 +100,7 @@ const getPlugin = async (
title: 'Hello',
baseUrl: '/',
url: 'https://docusaurus.io',
markdown,
} as DocusaurusConfig;
return pluginContentBlog(
{
Expand Down Expand Up @@ -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: [
{
Expand Down
31 changes: 23 additions & 8 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import _ from 'lodash';
import logger from '@docusaurus/logger';
import readingTime from 'reading-time';
import {
parseMarkdownString,
parseMarkdownFile,
normalizeUrl,
aliasedSitePath,
getEditUrl,
Expand All @@ -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,
Expand Down Expand Up @@ -180,18 +180,27 @@ 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 {
...result,
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;
}
}
Expand All @@ -207,7 +216,10 @@ async function processBlogSourceFile(
authorsMap?: AuthorsMap,
): Promise<BlogPost | undefined> {
const {
siteConfig: {baseUrl},
siteConfig: {
baseUrl,
markdown: {parseFrontMatter},
},
siteDir,
i18n,
} = context;
Expand All @@ -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);

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ exports[`simple website content: data 1`] = `
"frontMatter": {
"title": "Custom Last Update",
"last_update": {
"author": "Custom Author",
"author": "Custom Author (processed by parseFrontMatter)",
"date": "1/1/2000"
}
}
Expand Down Expand Up @@ -686,7 +686,7 @@ exports[`simple website content: data 1`] = `
"frontMatter": {
"title": "Last Update Author Only",
"last_update": {
"author": "Custom Author"
"author": "Custom Author (processed by parseFrontMatter)"
}
}
}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,14 @@ describe('simple site', () => {
description: 'Custom last update',
frontMatter: {
last_update: {
author: 'Custom Author',
author: 'Custom Author (processed by parseFrontMatter)',
date: '1/1/2000',
},
title: 'Custom Last Update',
},
lastUpdatedAt: new Date('1/1/2000').getTime() / 1000,
formattedLastUpdatedAt: 'Jan 1, 2000',
lastUpdatedBy: 'Custom Author',
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
sidebarPosition: undefined,
tags: [],
unlisted: false,
Expand Down Expand Up @@ -607,13 +607,13 @@ describe('simple site', () => {
description: 'Only custom author, so it will still use the date from Git',
frontMatter: {
last_update: {
author: 'Custom Author',
author: 'Custom Author (processed by parseFrontMatter)',
},
title: 'Last Update Author Only',
},
lastUpdatedAt: 1539502055,
formattedLastUpdatedAt: 'Oct 14, 2018',
lastUpdatedBy: 'Custom Author',
lastUpdatedBy: 'Custom Author (processed by parseFrontMatter)',
sidebarPosition: undefined,
tags: [],
unlisted: false,
Expand Down Expand Up @@ -685,7 +685,7 @@ describe('simple site', () => {
description: 'Custom last update',
frontMatter: {
last_update: {
author: 'Custom Author',
author: 'Custom Author (processed by parseFrontMatter)',
date: '1/1/2000',
},
title: 'Custom Last Update',
Expand Down
16 changes: 13 additions & 3 deletions packages/docusaurus-plugin-content-docs/src/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getFolderContainingFile,
getContentPathList,
normalizeUrl,
parseMarkdownString,
parseMarkdownFile,
posixPath,
Globby,
normalizeFrontMatterTags,
Expand Down Expand Up @@ -140,13 +140,23 @@ async function doProcessDocMetadata({
env: DocEnv;
}): Promise<DocMetadataBase> {
const {source, content, contentPath, filePath} = docFile;
const {siteDir, i18n} = context;
const {
siteDir,
i18n,
siteConfig: {
markdown: {parseFrontMatter},
},
} = context;

const {
frontMatter: unsafeFrontMatter,
contentTitle,
excerpt,
} = parseMarkdownString(content);
} = await parseMarkdownFile({
filePath,
fileContent: content,
parseFrontMatter,
});
const frontMatter = validateDocFrontMatter(unsafeFrontMatter);

const {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ exports[`docusaurus-plugin-content-pages loads simple pages 1`] = `
},
{
"description": "Markdown index page",
"frontMatter": {},
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"permalink": "/hello/",
"source": "@site/src/pages/hello/index.md",
"title": "Index",
Expand All @@ -24,6 +26,7 @@ exports[`docusaurus-plugin-content-pages loads simple pages 1`] = `
{
"description": "my MDX page",
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
"description": "my MDX page",
"title": "MDX page",
},
Expand All @@ -40,7 +43,9 @@ exports[`docusaurus-plugin-content-pages loads simple pages 1`] = `
},
{
"description": "translated Markdown page",
"frontMatter": {},
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"permalink": "/hello/translatedMd",
"source": "@site/src/pages/hello/translatedMd.md",
"title": undefined,
Expand Down Expand Up @@ -69,7 +74,9 @@ exports[`docusaurus-plugin-content-pages loads simple pages with french translat
},
{
"description": "Markdown index page",
"frontMatter": {},
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"permalink": "/fr/hello/",
"source": "@site/src/pages/hello/index.md",
"title": "Index",
Expand All @@ -79,6 +86,7 @@ exports[`docusaurus-plugin-content-pages loads simple pages with french translat
{
"description": "my MDX page",
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
"description": "my MDX page",
"title": "MDX page",
},
Expand All @@ -95,7 +103,9 @@ exports[`docusaurus-plugin-content-pages loads simple pages with french translat
},
{
"description": "translated Markdown page (fr)",
"frontMatter": {},
"frontMatter": {
"custom_frontMatter": "added by parseFrontMatter",
},
"permalink": "/fr/hello/translatedMd",
"source": "@site/i18n/fr/docusaurus-plugin-content-pages/hello/translatedMd.md",
"title": undefined,
Expand Down

0 comments on commit 1a91145

Please sign in to comment.