Skip to content

Commit

Permalink
wip: refactor review
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Nov 27, 2023
1 parent 59494e0 commit f008026
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions packages/docusaurus-plugin-content-blog/src/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import {getDataFileData} from '@docusaurus/utils';
import {getDataFileData, normalizeUrl} from '@docusaurus/utils';
import {Joi, URISchema} from '@docusaurus/utils-validation';
import type {BlogContentPaths} from './types';
import type {
Expand Down Expand Up @@ -72,6 +71,18 @@ type AuthorsParam = {
baseUrl: string;
};

function normalizeImageUrl({
imageURL,
baseUrl,
}: {
imageURL: string | undefined;
baseUrl: string;
}) {
return imageURL?.startsWith('/')
? normalizeUrl([baseUrl, imageURL])
: imageURL;
}

// Legacy v1/early-v2 front matter fields
// We may want to deprecate those in favor of using only frontMatter.authors
function getFrontMatterAuthorLegacy({
Expand All @@ -84,11 +95,10 @@ function getFrontMatterAuthorLegacy({
const name = frontMatter.author;
const title = frontMatter.author_title ?? frontMatter.authorTitle;
const url = frontMatter.author_url ?? frontMatter.authorURL;
let imageURL = frontMatter.author_image_url ?? frontMatter.authorImageURL;

if (baseUrl !== '/' && imageURL && !imageURL.startsWith('http')) {
imageURL = path.join(baseUrl, imageURL);
}
const imageURL = normalizeImageUrl({
imageURL: frontMatter.author_image_url ?? frontMatter.authorImageURL,
baseUrl,
});

if (name || title || url || imageURL) {
return {
Expand Down Expand Up @@ -162,28 +172,22 @@ function fixAuthorImageBaseURL(
authors: Author[],
{baseUrl}: {baseUrl: string},
) {
authors.forEach((author) => {
if (
!author.imageURL ||
baseUrl === '/' ||
author.imageURL.startsWith('http')
) {
return;
}
author.imageURL = path.join(baseUrl, author.imageURL);
});
return authors.map((author) => ({
...author,
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}),
}));
}

export function getBlogPostAuthors(params: AuthorsParam): Author[] {
const authorLegacy = getFrontMatterAuthorLegacy(params);
const authors = getFrontMatterAuthors(params);

fixAuthorImageBaseURL(authors, params);
const updatedAuthors = fixAuthorImageBaseURL(authors, params);

if (authorLegacy) {
// Technically, we could allow mixing legacy/authors front matter, but do we
// really want to?
if (authors.length > 0) {
if (updatedAuthors.length > 0) {
throw new Error(
`To declare blog post authors, use the 'authors' front matter in priority.
Don't mix 'authors' with other existing 'author_*' front matter. Choose one or the other, not both at the same time.`,
Expand All @@ -192,5 +196,5 @@ Don't mix 'authors' with other existing 'author_*' front matter. Choose one or t
return [authorLegacy];
}

return authors;
return updatedAuthors;
}

0 comments on commit f008026

Please sign in to comment.