From 8f4c7d0d180b53d8131e46f225de0f00ab969915 Mon Sep 17 00:00:00 2001 From: doxxx93 Date: Mon, 14 Oct 2024 10:17:31 +0900 Subject: [PATCH] Add default author feature for Docusaurus conversion - Implement defaultAuthor in FrontMatterConverter - Update DocusaurusSettings to include defaultAuthor - Add UI setting for defaultAuthor in O2Settings --- src/FrontMatterConverter.ts | 13 +++++- src/docusaurus/docusaurus.ts | 1 + src/docusaurus/settings/DocusaurusSettings.ts | 1 + src/settings.ts | 15 ++++++ src/tests/FrontMatterConverter.test.ts | 46 +++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/FrontMatterConverter.ts b/src/FrontMatterConverter.ts index 39970ae..5699142 100644 --- a/src/FrontMatterConverter.ts +++ b/src/FrontMatterConverter.ts @@ -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] { @@ -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, @@ -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; @@ -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, diff --git a/src/docusaurus/docusaurus.ts b/src/docusaurus/docusaurus.ts index e545188..cc1d77c 100644 --- a/src/docusaurus/docusaurus.ts +++ b/src/docusaurus/docusaurus.ts @@ -53,6 +53,7 @@ export const convertToDocusaurus = async (plugin: O2Plugin) => { convertWikiLink( convertFrontMatter( contents, + plugin.docusaurus.defaultAuthor, ), ), ), diff --git a/src/docusaurus/settings/DocusaurusSettings.ts b/src/docusaurus/settings/DocusaurusSettings.ts index 7289cb7..6828d36 100644 --- a/src/docusaurus/settings/DocusaurusSettings.ts +++ b/src/docusaurus/settings/DocusaurusSettings.ts @@ -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`; diff --git a/src/settings.ts b/src/settings.ts index 8389b03..c0d83ce 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -64,6 +64,7 @@ export class O2SettingTab extends PluginSettingTab { }); this.addDocusaurusPathSetting(); this.dateExtractionPatternSetting(); + this.addDefaultAuthorSetting(); // 새로운 설정 추가 } private enableUpdateFrontmatterTimeOnEditSetting() { @@ -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') diff --git a/src/tests/FrontMatterConverter.test.ts b/src/tests/FrontMatterConverter.test.ts index d9e4cd2..a80290e 100644 --- a/src/tests/FrontMatterConverter.test.ts +++ b/src/tests/FrontMatterConverter.test.ts @@ -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'); + }); +});