feat(v2): editUrl functions should receive md doc permalink (#4232)

This commit is contained in:
Sébastien Lorber 2021-02-17 11:48:33 +01:00 committed by GitHub
parent 2ff5d347ba
commit ae988d0eb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 79 additions and 63 deletions

View file

@ -184,26 +184,31 @@ describe('loadBlog', () => {
expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
blogPath: 'date-matter.md',
permalink: '/blog/date-matter',
locale: 'en',
});
expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
blogPath: 'draft.md',
permalink: '/blog/draft',
locale: 'en',
});
expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
blogPath: 'complex-slug.md',
permalink: '/blog/hey/my super path/héllô',
locale: 'en',
});
expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
blogPath: 'simple-slug.md',
permalink: '/blog/simple/slug',
locale: 'en',
});
expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'i18n/en/docusaurus-plugin-content-blog',
blogPath: '2018-12-14-Happy-First-Birthday-Slash.md',
permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
locale: 'en',
});
});

View file

@ -108,7 +108,7 @@ export async function generateBlogPosts(
routeBasePath,
truncateMarker,
showReadingTime,
editUrl: siteEditUrl,
editUrl,
} = options;
if (!fs.existsSync(contentPaths.contentPath)) {
@ -136,37 +136,6 @@ export async function generateBlogPosts(
const blogFileName = path.basename(blogSourceFile);
function getBlogEditUrl() {
const blogPathRelative = path.relative(
blogDirPath,
path.resolve(source),
);
if (typeof siteEditUrl === 'function') {
return siteEditUrl({
blogDirPath: posixPath(path.relative(siteDir, blogDirPath)),
blogPath: posixPath(blogPathRelative),
locale: i18n.currentLocale,
});
} else if (typeof siteEditUrl === 'string') {
const isLocalized = blogDirPath === contentPaths.contentPathLocalized;
const fileContentPath =
isLocalized && options.editLocalizedFiles
? contentPaths.contentPathLocalized
: contentPaths.contentPath;
const contentPathEditUrl = normalizeUrl([
siteEditUrl,
posixPath(path.relative(siteDir, fileContentPath)),
]);
return getEditUrl(blogPathRelative, contentPathEditUrl);
} else {
return undefined;
}
}
const editBlogUrl = getBlogEditUrl();
const {frontMatter, content, excerpt} = await parseMarkdownFile(source);
if (frontMatter.draft && process.env.NODE_ENV === 'production') {
@ -204,11 +173,44 @@ export async function generateBlogPosts(
frontMatter.slug || (match ? toUrl({date, link: linkName}) : linkName);
frontMatter.title = frontMatter.title || linkName;
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);
function getBlogEditUrl() {
const blogPathRelative = path.relative(
blogDirPath,
path.resolve(source),
);
if (typeof editUrl === 'function') {
return editUrl({
blogDirPath: posixPath(path.relative(siteDir, blogDirPath)),
blogPath: posixPath(blogPathRelative),
permalink,
locale: i18n.currentLocale,
});
} else if (typeof editUrl === 'string') {
const isLocalized = blogDirPath === contentPaths.contentPathLocalized;
const fileContentPath =
isLocalized && options.editLocalizedFiles
? contentPaths.contentPathLocalized
: contentPaths.contentPath;
const contentPathEditUrl = normalizeUrl([
editUrl,
posixPath(path.relative(siteDir, fileContentPath)),
]);
return getEditUrl(blogPathRelative, contentPathEditUrl);
} else {
return undefined;
}
}
blogPosts.push({
id: frontMatter.slug || frontMatter.title,
metadata: {
permalink: normalizeUrl([baseUrl, routeBasePath, slug]),
editUrl: editBlogUrl,
permalink,
editUrl: getBlogEditUrl(),
source: aliasedSource,
description: frontMatter.description || excerpt,
date,

View file

@ -27,6 +27,7 @@ export type FeedType = 'rss' | 'atom';
export type EditUrlFunction = (editUrlParams: {
blogDirPath: string;
blogPath: string;
permalink: string;
locale: string;
}) => string | undefined;