Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add Docusaurus authors support #397

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/FrontMatterConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const convert = (frontMatter: FrontMatter) => {
}`;
}

if (fm.authors) {
const authorList = fm.authors.split(',').map(author => author.trim());
fm.authors = authorList.length > 1 ? `[${authorList.join(', ')}]` : authorList[0];
}

// if fm.tags is array
if (fm.tags) {
fm.tags = Array.isArray(fm.tags) ? `[${fm.tags.join(', ')}]` : `[${fm.tags}]`;
Expand Down Expand Up @@ -147,7 +152,7 @@ function replaceDateFrontMatter(frontMatter: FrontMatter, isEnable: boolean): Fr
return frontMatter;
}

export const convertFrontMatter = (input: string) => {
export const convertFrontMatter = (input: string, authors?: string) => {
const [frontMatter, body] = parseFrontMatter(input);
if (Object.keys(frontMatter).length === 0) {
return input;
Expand All @@ -161,6 +166,12 @@ export const convertFrontMatter = (input: string) => {
delete frontMatter['aliases'];
delete frontMatter['published'];

if (authors) {
delete frontMatter['authors'];
delete frontMatter['author'];
frontMatter.authors = authors;
}

return join(
convert({ ...frontMatter }),
body,
Expand Down
1 change: 1 addition & 0 deletions src/docusaurus/docusaurus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const convertToDocusaurus = async (plugin: O2Plugin) => {
convertWikiLink(
convertFrontMatter(
contents,
plugin.docusaurus.authors,
),
),
),
Expand Down
1 change: 1 addition & 0 deletions src/docusaurus/settings/DocusaurusSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DateExtractionPattern } from '../DateExtractionPattern';
export default class DocusaurusSettings implements O2PluginSettings {
docusaurusPath: string;
dateExtractionPattern: string;
authors: string;
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider enhancing the authors property implementation.

The addition of the authors property is a good step towards supporting Docusaurus authors. However, there are a few potential improvements to consider:

  1. The current type string might not be flexible enough for multiple authors. Consider using string[] instead to support an array of authors.
  2. Adding JSDoc comments would improve the documentation and provide more context about the property's usage.
  3. It might be beneficial to initialize the property with a default value (e.g., an empty array if using string[]) to prevent potential null/undefined issues.

Here's a suggested implementation with these improvements:

/**
 * The authors of the Docusaurus content.
 * This can be a single author or multiple authors.
 */
authors: string[] = [];

Could you please clarify if there's a specific reason for using string instead of string[] for the authors property? This would help in understanding if the current implementation aligns with the intended use case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user will fill in the authors through the input window, so can this value be recognized as array? I think we should first receive the value and then convert it into array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@songkg7, thank you for the clarification.

You're absolutely right; since users fill in the authors through the input window as a string, keeping the authors property as a string in DocusaurusSettings makes sense. Converting it into an array during the front matter conversion process is a suitable approach to handle multiple authors. If you need any assistance with the conversion logic or ensuring that multiple authors are parsed correctly, please let me know.

(_/)
(o.o)?
(>💡<)


✏️ Learnings added
Learnt from: songkg7
PR: songkg7/o2#397
File: src/docusaurus/settings/DocusaurusSettings.ts:7-7
Timestamp: 2024-10-19T11:32:28.774Z
Learning: In `DocusaurusSettings.ts`, the `authors` property is a `string` because users input authors as a string through the settings UI. The string is then converted into an array during the front matter conversion to handle multiple authors.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


targetPath(): string {
return `${this.docusaurusPath}/blog`;
Expand Down
15 changes: 15 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ export class O2SettingTab extends PluginSettingTab {
});
this.addDocusaurusPathSetting();
this.dateExtractionPatternSetting();
this.addDocusaurusAuthorsSetting();
}

private addDocusaurusAuthorsSetting() {
const docusaurus = this.plugin.docusaurus as DocusaurusSettings;
new Setting(this.containerEl)
.setName('Docusaurus authors')
.setDesc('Author(s) for Docusaurus front matter. For multiple authors, separate with commas.')
.addText(text => text
.setPlaceholder('jmarcey, slorber')
.setValue(docusaurus.authors)
.onChange(async (value) => {
docusaurus.authors = value;
await this.plugin.saveSettings();
}));
}

private enableUpdateFrontmatterTimeOnEditSetting() {
Expand Down
75 changes: 75 additions & 0 deletions src/tests/FrontMatterConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,78 @@ date: 2021-01-01 12:00:00 +0900
expect(result).toEqual(incompleteFrontMatterContents); // Assuming the function passes through incomplete front matter as is
});
});

describe('convertFrontMatter with author settings', () => {
it('should add single author to front matter', () => {
const input = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
---

Content here
`;
const expected = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
authors: jmarcey
---

Content here
`;
const result = convertFrontMatter(input, 'jmarcey');
expect(result).toBe(expected);
});

it('should add multiple authors to front matter', () => {
const input = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
---

Content here
`;
const expected = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
authors: [jmarcey, slorber]
---

Content here
`;
const result = convertFrontMatter(input, 'jmarcey, slorber');
expect(result).toBe(expected);
});

it('should replace existing authors in front matter', () => {
const input = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
authors: oldauthor
---

Content here
`;
const expected = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
authors: [jmarcey, slorber]
---

Content here
`;
const result = convertFrontMatter(input, 'jmarcey, slorber');
expect(result).toBe(expected);
});

it('should not add authors if not provided', () => {
const input = `---
title: "Test Post"
date: 2021-01-01 12:00:00 +0900
---

Content here
`;
const result = convertFrontMatter(input);
expect(result).toBe(input);
});
});
Loading