-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1eb2af6
feat(blog): warn duplicate and inline authors
OzakIOne 373ab74
fix tests
OzakIOne 5833974
fix test
OzakIOne 8d71167
feat: tests
OzakIOne 83bf814
refactor
OzakIOne 7f59a6e
feat: tests
OzakIOne 4e33812
refactor: apply lint autofix
OzakIOne dde8cde
ignore inline on duplicate check
OzakIOne ea1d1b7
refactor: apply lint autofix
OzakIOne 4795cd0
remove typo
slorber b0eca61
Merge branch 'main' into ozaki/inlineAuthorWarn
slorber 61d165f
remove typo
slorber aff176c
refactor authors duplicate algo
slorber 534a7e9
remove inline attribute in favor of key
slorber 564f2b5
extract and refactor logic to authorsProblems.ts
slorber 9a487bc
improve tests for reportAuthorProblems
slorber 4586ade
fix website blog author warnings
slorber 22ea279
fix website blog author warnings
slorber 4ef5096
fix website blog author warnings + polish
slorber 042eaaf
revert useless tests changes
slorber 9cbabbe
revert useless tests changes
slorber e4af303
revert useless diff
slorber 489c3e0
fix tests
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
packages/docusaurus-plugin-content-blog/src/__tests__/authorsProblems.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {jest} from '@jest/globals'; | ||
import {reportDuplicateAuthors, reportInlineAuthors} from '../authorsProblems'; | ||
import type {Author} from '@docusaurus/plugin-content-blog'; | ||
|
||
const blogSourceRelative = 'doc.md'; | ||
|
||
describe('duplicate authors', () => { | ||
function testReport({authors}: {authors: Author[]}) { | ||
reportDuplicateAuthors({ | ||
authors, | ||
blogSourceRelative, | ||
}); | ||
} | ||
|
||
it('allows duplicated inline authors', () => { | ||
const authors: Author[] = [ | ||
{ | ||
name: 'Sébastien Lorber', | ||
}, | ||
{ | ||
name: 'Sébastien Lorber', | ||
}, | ||
]; | ||
|
||
expect(() => | ||
testReport({ | ||
authors, | ||
}), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('rejects duplicated key authors', () => { | ||
const authors: Author[] = [ | ||
{ | ||
key: 'slorber', | ||
name: 'Sébastien Lorber 1', | ||
title: 'some title', | ||
}, | ||
{ | ||
key: 'slorber', | ||
name: 'Sébastien Lorber 2', | ||
imageURL: '/slorber.png', | ||
}, | ||
]; | ||
|
||
expect(() => | ||
testReport({ | ||
authors, | ||
}), | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"Duplicate blog post authors were found in blog post "doc.md" front matter: | ||
- {"key":"slorber","name":"Sébastien Lorber 2","imageURL":"/slorber.png"}" | ||
`); | ||
}); | ||
}); | ||
|
||
describe('inline authors', () => { | ||
type Options = Parameters<typeof reportInlineAuthors>[0]['options']; | ||
|
||
function testReport({ | ||
authors, | ||
options = {}, | ||
}: { | ||
authors: Author[]; | ||
options?: Partial<Options>; | ||
}) { | ||
const defaultOptions: Options = { | ||
onInlineAuthors: 'throw', | ||
authorsMapPath: 'authors.yml', | ||
}; | ||
|
||
reportInlineAuthors({ | ||
authors, | ||
options: { | ||
...defaultOptions, | ||
...options, | ||
}, | ||
blogSourceRelative, | ||
}); | ||
} | ||
|
||
it('allows predefined authors', () => { | ||
const authors: Author[] = [ | ||
{ | ||
key: 'slorber', | ||
name: 'Sébastien Lorber', | ||
}, | ||
{ | ||
key: 'ozaki', | ||
name: 'Clément Couriol', | ||
}, | ||
]; | ||
|
||
expect(() => | ||
testReport({ | ||
authors, | ||
}), | ||
).not.toThrow(); | ||
}); | ||
|
||
it('rejects inline authors', () => { | ||
const authors: Author[] = [ | ||
{ | ||
key: 'slorber', | ||
name: 'Sébastien Lorber', | ||
}, | ||
{name: 'Inline author 1'}, | ||
{ | ||
key: 'ozaki', | ||
name: 'Clément Couriol', | ||
}, | ||
{imageURL: '/inline-author2.png'}, | ||
]; | ||
|
||
expect(() => | ||
testReport({ | ||
authors, | ||
}), | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"Some blog authors used in "doc.md" are not defined in "authors.yml": | ||
- {"name":"Inline author 1"} | ||
- {"imageURL":"/inline-author2.png"} | ||
|
||
Note that we recommend to declare authors once in a "authors.yml" file and reference them by key in blog posts front matter to avoid author info duplication. | ||
But if you want to allow inline blog authors, you can disable this message by setting onInlineAuthors: 'ignore' in your blog plugin options. | ||
More info at https://docusaurus.io/docs/blog | ||
" | ||
`); | ||
}); | ||
|
||
it('warn inline authors', () => { | ||
const authors: Author[] = [ | ||
{ | ||
key: 'slorber', | ||
name: 'Sébastien Lorber', | ||
}, | ||
{name: 'Inline author 1'}, | ||
{ | ||
key: 'ozaki', | ||
name: 'Clément Couriol', | ||
}, | ||
{imageURL: '/inline-author2.png'}, | ||
]; | ||
|
||
const consoleMock = jest | ||
.spyOn(console, 'warn') | ||
.mockImplementation(() => {}); | ||
|
||
expect(() => | ||
testReport({ | ||
authors, | ||
options: { | ||
onInlineAuthors: 'warn', | ||
}, | ||
}), | ||
).not.toThrow(); | ||
expect(consoleMock).toHaveBeenCalledTimes(1); | ||
expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(` | ||
[ | ||
"[WARNING] Some blog authors used in "doc.md" are not defined in "authors.yml": | ||
- {"name":"Inline author 1"} | ||
- {"imageURL":"/inline-author2.png"} | ||
|
||
Note that we recommend to declare authors once in a "authors.yml" file and reference them by key in blog posts front matter to avoid author info duplication. | ||
But if you want to allow inline blog authors, you can disable this message by setting onInlineAuthors: 'ignore' in your blog plugin options. | ||
More info at https://docusaurus.io/docs/blog | ||
", | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/docusaurus-plugin-content-blog/src/authorsProblems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import _ from 'lodash'; | ||
import logger from '@docusaurus/logger'; | ||
import type {Author, PluginOptions} from '@docusaurus/plugin-content-blog'; | ||
|
||
export function reportAuthorsProblems(params: { | ||
authors: Author[]; | ||
blogSourceRelative: string; | ||
options: Pick<PluginOptions, 'onInlineAuthors' | 'authorsMapPath'>; | ||
}): void { | ||
reportInlineAuthors(params); | ||
reportDuplicateAuthors(params); | ||
} | ||
|
||
export function reportInlineAuthors({ | ||
authors, | ||
blogSourceRelative, | ||
options: {onInlineAuthors, authorsMapPath}, | ||
}: { | ||
authors: Author[]; | ||
blogSourceRelative: string; | ||
options: Pick<PluginOptions, 'onInlineAuthors' | 'authorsMapPath'>; | ||
}): void { | ||
if (onInlineAuthors === 'ignore') { | ||
return; | ||
} | ||
const inlineAuthors = authors.filter((author) => !author.key); | ||
if (inlineAuthors.length > 0) { | ||
logger.report(onInlineAuthors)( | ||
logger.interpolate`Some blog authors used in path=${blogSourceRelative} are not defined in path=${authorsMapPath}: | ||
- ${inlineAuthors.map(authorToString).join('\n- ')} | ||
|
||
Note that we recommend to declare authors once in a path=${authorsMapPath} file and reference them by key in blog posts front matter to avoid author info duplication. | ||
But if you want to allow inline blog authors, you can disable this message by setting onInlineAuthors: 'ignore' in your blog plugin options. | ||
More info at url=${'https://docusaurus.io/docs/blog'} | ||
`, | ||
); | ||
} | ||
} | ||
|
||
export function reportDuplicateAuthors({ | ||
authors, | ||
blogSourceRelative, | ||
}: { | ||
authors: Author[]; | ||
blogSourceRelative: string; | ||
}): void { | ||
const duplicateAuthors = _(authors) | ||
// for now we only check for predefined authors duplicates | ||
.filter((author) => !!author.key) | ||
.groupBy((author) => author.key) | ||
.pickBy((authorsByKey) => authorsByKey.length > 1) | ||
// We only keep the "test" of all the duplicate groups | ||
// The first author of a group is not really a duplicate... | ||
.flatMap(([, ...rest]) => rest) | ||
.value(); | ||
|
||
if (duplicateAuthors.length > 0) { | ||
throw new Error(logger.interpolate`Duplicate blog post authors were found in blog post path=${blogSourceRelative} front matter: | ||
- ${duplicateAuthors.map(authorToString).join('\n- ')}`); | ||
} | ||
} | ||
|
||
function authorToString(author: Author) { | ||
return JSON.stringify(author); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
--- | ||
tags: | ||
- b | ||
- label: d | ||
permalink: d-custom-permalink | ||
- d | ||
--- | ||
|
||
# Standalone doc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
upcoming conflict: will be refactored when updating in #10216