-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blog): warn duplicate and inline authors (#10224)
Co-authored-by: OzakIOne <[email protected]> Co-authored-by: sebastien <[email protected]>
- Loading branch information
1 parent
1405b25
commit de59621
Showing
16 changed files
with
299 additions
and
21 deletions.
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.