-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(blog): warn duplicate and inline authors #10224
Changes from 3 commits
1eb2af6
373ab74
5833974
8d71167
83bf814
7f59a6e
4e33812
dde8cde
ea1d1b7
4795cd0
b0eca61
61d165f
aff176c
534a7e9
564f2b5
9a487bc
4586ade
22ea279
4ef5096
042eaaf
9cbabbe
e4af303
489c3e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,9 +220,11 @@ describe('blog plugin', () => { | |
authors: [ | ||
{ | ||
name: 'Yangshun Tay (translated)', | ||
inline: true, | ||
}, | ||
{ | ||
email: '[email protected]', | ||
imageURL: undefined, | ||
key: 'slorber', | ||
name: 'Sébastien Lorber (translated)', | ||
title: 'Docusaurus maintainer (translated)', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import {getDataFileData, normalizeUrl} from '@docusaurus/utils'; | ||
import {Joi, URISchema} from '@docusaurus/utils-validation'; | ||
import logger from '@docusaurus/logger'; | ||
import type {BlogContentPaths} from './types'; | ||
import type { | ||
Author, | ||
|
@@ -123,6 +124,11 @@ function normalizeFrontMatterAuthors( | |
// we only support keys, otherwise, a typo in a key would fallback to | ||
// becoming a name and may end up unnoticed | ||
return {key: authorInput}; | ||
} else if (typeof authorInput === 'object' && !('key' in authorInput)) { | ||
return { | ||
...authorInput, | ||
inline: true, | ||
}; | ||
} | ||
return authorInput; | ||
} | ||
|
@@ -171,7 +177,7 @@ ${Object.keys(authorsMap) | |
function fixAuthorImageBaseURL( | ||
authors: Author[], | ||
{baseUrl}: {baseUrl: string}, | ||
) { | ||
): Author[] { | ||
return authors.map((author) => ({ | ||
...author, | ||
imageURL: normalizeImageUrl({imageURL: author.imageURL, baseUrl}), | ||
|
@@ -196,5 +202,33 @@ Don't mix 'authors' with other existing 'author_*' front matter. Choose one or t | |
return [authorLegacy]; | ||
} | ||
|
||
const inlineAuthors = updatedAuthors.filter((author) => author.inline); | ||
|
||
const duplicateList = updatedAuthors.filter( | ||
(author, index, self) => | ||
index !== self.findIndex((t) => t.name === author.name), | ||
); | ||
|
||
// TODO need title check otherwise reports weird cases | ||
if (inlineAuthors.length > 0 && params.frontMatter.title) { | ||
logger.warn( | ||
`Inline authors found in blog [${ | ||
params.frontMatter.title | ||
}] ${inlineAuthors.map((author) => author.name).join(', ')}`, | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need an option
What are those cases exactly? We need to handle those and cover with unit test edge cases Overall the code should look quite similar to the tags code: export function reportInlineTags({
tags,
source,
options,
}: {
tags: TagMetadata[];
source: string;
options: TagsPluginOptions;
}): void {
if (options.onInlineTags === 'ignore') {
return;
}
const inlineTags = tags.filter((tag) => tag.inline);
if (inlineTags.length > 0) {
const uniqueUnknownTags = [...new Set(inlineTags.map((tag) => tag.label))];
const tagListString = uniqueUnknownTags.join(', ');
logger.report(options.onInlineTags)(
`Tags [${tagListString}] used in ${source} are not defined in ${
options.tags ?? 'tags.yml'
}`,
);
}
} I'd prefer if the side effect (logging) wasn't performed inside const authors = getBlogPostAuthors({authorsMap, frontMatter, baseUrl});
reportAuthorProblems(paramsYouNeed) If logging requires more info about the blog (like the title/source/whatever) you can inject more as params. |
||
|
||
// TODO need title check otherwise reports weird cases | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reports authors from changelog, maybe there is a better way to ignore that |
||
if (duplicateList.length > 0 && params.frontMatter.title) { | ||
OzakIOne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log('duplicateList', duplicateList); | ||
logger.error( | ||
`Duplicate authors found in blog post ${params.frontMatter.title} [${ | ||
params.frontMatter.slug | ||
}] front matter: ${duplicateList | ||
.map((author) => author.name) | ||
.join(', ')}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. front matter might have no title, no slug, and author might have no name All these cases should be covered with unit tests |
||
); | ||
} | ||
|
||
return updatedAuthors; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Author might have no name
What happens if we have 2 authors with just an image? Are they considered as duplicate?
What happens if one predefined author has key "seb" and name "Seb", and there's another inline author named "Seb", are they considered as duplicate?
The current implementation is not good enough, and the method to detect duplicates is complex enough to deserve its own unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case should work and not warn
https://deploy-preview-10224--docusaurus-2.netlify.app/tests/blog/2022/01/20/image-only-authors/