Skip to content

Commit

Permalink
Add default author feature for Docusaurus conversion
Browse files Browse the repository at this point in the history
- Implement defaultAuthor in FrontMatterConverter
- Update DocusaurusSettings to include defaultAuthor
- Add UI setting for defaultAuthor in O2Settings
  • Loading branch information
doxxx93 committed Oct 14, 2024
1 parent 2a9fb3f commit 8f4c7d0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/FrontMatterConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ export class FrontMatterConverter implements Converter {
private readonly resourcePath: string;
private readonly isEnableBanner: boolean;
private readonly isEnableUpdateFrontmatterTimeOnEdit: boolean;
private readonly defaultAuthor: string;

constructor(
fileName: string,
resourcePath: string,
isEnableBanner = false,
isEnableUpdateFrontmatterTimeOnEdit = false,
defaultAuthor = '',
) {
this.fileName = fileName;
this.resourcePath = resourcePath;
this.isEnableBanner = isEnableBanner;
this.isEnableUpdateFrontmatterTimeOnEdit = isEnableUpdateFrontmatterTimeOnEdit;
this.defaultAuthor = defaultAuthor;
}

parseFrontMatter(content: string): [FrontMatter, string] {
Expand All @@ -92,6 +95,10 @@ export class FrontMatterConverter implements Converter {
frontMatter.mermaid = true.toString();
}

if (!frontMatter.authors && this.defaultAuthor) {
frontMatter.authors = this.defaultAuthor;
}

const result = convert(
convertImageFrontMatter(
this.isEnableBanner,
Expand Down Expand Up @@ -147,7 +154,7 @@ function replaceDateFrontMatter(frontMatter: FrontMatter, isEnable: boolean): Fr
return frontMatter;
}

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

if (!frontMatter.authors && defaultAuthor && defaultAuthor.trim() !== '') {
frontMatter.authors = defaultAuthor;
}

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.defaultAuthor,
),
),
),
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;
defaultAuthor: string;

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,7 @@ export class O2SettingTab extends PluginSettingTab {
});
this.addDocusaurusPathSetting();
this.dateExtractionPatternSetting();
this.addDefaultAuthorSetting(); // 새로운 설정 추가
}

private enableUpdateFrontmatterTimeOnEditSetting() {
Expand Down Expand Up @@ -202,6 +203,20 @@ export class O2SettingTab extends PluginSettingTab {
});
}

private addDefaultAuthorSetting() {
const docusaurus = this.plugin.docusaurus as DocusaurusSettings;
new Setting(this.containerEl)
.setName('Default Author')
.setDesc('The default author to be used in Docusaurus frontmatter.')
.addText(text => text
.setPlaceholder('Enter default author')
.setValue(docusaurus.defaultAuthor || '')
.onChange(async (value) => {
docusaurus.defaultAuthor = value;
await this.plugin.saveSettings();
}));
}

private enableAutoArchiveSetting() {
new Setting(this.containerEl)
.setName('Auto archive')
Expand Down
46 changes: 46 additions & 0 deletions src/tests/FrontMatterConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,49 @@ date: 2021-01-01 12:00:00 +0900
expect(result).toEqual(incompleteFrontMatterContents); // Assuming the function passes through incomplete front matter as is
});
});

describe('FrontMatterConverter with defaultAuthor', () => {
it('should not add authors when defaultAuthor is empty', () => {
const converter = new FrontMatterConverter('file.md', 'path', false, false, '');
const input = '---\ntitle: "Test"\n---\nContent';
const result = converter.convert(input);
expect(result).not.toContain('authors:');
});

it('should not override existing authors', () => {
const converter = new FrontMatterConverter('file.md', 'path', false, false, 'Default Author');
const input = '---\ntitle: "Test"\nauthors: Existing Author\n---\nContent';
const result = converter.convert(input);
expect(result).toContain('authors: Existing Author');
expect(result).not.toContain('authors: Default Author');
});

it('should add defaultAuthor when no authors exist', () => {
const converter = new FrontMatterConverter('file.md', 'path', false, false, 'Default Author');
const input = '---\ntitle: "Test"\n---\nContent';
const result = converter.convert(input);
expect(result).toContain('authors: Default Author');
});

it('should handle empty frontmatter', () => {
const converter = new FrontMatterConverter('file.md', 'path', false, false, 'Default Author');
const input = 'Content without frontmatter';
const result = converter.convert(input);
expect(result).toBe(input);
});
});

describe('convertFrontMatter with defaultAuthor', () => {
it('should add defaultAuthor when no authors exist', () => {
const input = '---\ntitle: "Test"\n---\nContent';
const result = convertFrontMatter(input, 'Default Author');
expect(result).toContain('authors: Default Author');
});

it('should not override existing authors', () => {
const input = '---\ntitle: "Test"\nauthors: Existing Author\n---\nContent';
const result = convertFrontMatter(input, 'Default Author');
expect(result).toContain('authors: Existing Author');
expect(result).not.toContain('authors: Default Author');
});
});

0 comments on commit 8f4c7d0

Please sign in to comment.