Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
songkg7 committed Jun 28, 2024
1 parent 127566a commit eb1cd0c
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 61 deletions.
160 changes: 100 additions & 60 deletions src/jekyll/FrontMatterConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,56 @@ interface FrontMatter {
[key: string]: string;
}

const parseFrontMatter = (content: string): [FrontMatter, string] => {
if (!content.startsWith('---')) {
return [{}, content];
}

// for define front matter boundary
const endOfFrontMatter = content.indexOf('---', 3);
if (endOfFrontMatter === -1) {
return [{}, content];
}

const frontMatterLines = content.substring(3, endOfFrontMatter);
const body = content.substring(endOfFrontMatter + 3).trimStart();

const frontMatter = yaml.load(frontMatterLines) as FrontMatter;
return [frontMatter, body];
};

const join = (result: FrontMatter, body: string) => `---
${Object.entries(result)
.map(([key, value]) => `${key}: ${value}`)
.join('\n')}
---
${body}`;

const convert = (frontMatter: FrontMatter) => {
// if not around front matter title using double quote, add double quote
if (frontMatter.title && !frontMatter.title.startsWith('"')) {
frontMatter.title = `"${frontMatter.title}"`;
}

// if not around front matter categories using an array, add an array
if (frontMatter.categories && JSON.stringify(frontMatter.categories).startsWith('[')) {
frontMatter.categories = `${JSON.stringify(frontMatter.categories)
.replace(/,/g, ', ')
.replace(/"/g, '')
}`;
}

// if frontMatter.tags is array
if (frontMatter.tags && Array.isArray(frontMatter.tags)) {
frontMatter.tags = `[${frontMatter.tags}]`.replace(/,/g, ', ');
} else if (frontMatter.tags && !JSON.stringify(frontMatter.tags).startsWith('[')) {
frontMatter.tags = `[${frontMatter.tags}]`;
}

return frontMatter;
};

export class FrontMatterConverter implements Converter {

private readonly fileName: string;
Expand All @@ -26,21 +76,7 @@ export class FrontMatterConverter implements Converter {
}

parseFrontMatter(content: string): [FrontMatter, string] {
if (!content.startsWith('---')) {
return [{}, content];
}

// for define front matter boundary
const endOfFrontMatter = content.indexOf('---', 3);
if (endOfFrontMatter === -1) {
return [{}, content];
}

const frontMatterLines = content.substring(3, endOfFrontMatter);
const body = content.substring(endOfFrontMatter + 3).trimStart();

const frontMatter = yaml.load(frontMatterLines) as FrontMatter;
return [frontMatter, body];
return parseFrontMatter(content);
}

convert(input: string): string {
Expand All @@ -50,64 +86,52 @@ export class FrontMatterConverter implements Converter {
return input;
}

// if not around front matter title using double quote, add double quote
if (frontMatter.title && !frontMatter.title.startsWith('"')) {
frontMatter.title = `"${frontMatter.title}"`;
}

// if not around front matter categories using an array, add an array
if (frontMatter.categories && JSON.stringify(frontMatter.categories).startsWith('[')) {
frontMatter.categories = `${JSON.stringify(frontMatter.categories)
.replace(/,/g, ', ')
.replace(/"/g, '')
}`;
}

// if frontMatter.tags is array
if (frontMatter.tags && Array.isArray(frontMatter.tags)) {
frontMatter.tags = `[${frontMatter.tags}]`.replace(/,/g, ', ');
} else if (frontMatter.tags && !JSON.stringify(frontMatter.tags).startsWith('[')) {
frontMatter.tags = `[${frontMatter.tags}]`;
}

if (body.match(/```mermaid/)) {
frontMatter.mermaid = true.toString();
}

// FIXME: abstraction, like chain of responsibility
const convertedFrontMatter = this.convertImageFrontMatter({ ...frontMatter });
const result = replaceDateFrontMatter({ ...convertedFrontMatter }, this.isEnableUpdateFrontmatterTimeOnEdit);

return `---
${Object.entries(result)
.map(([key, value]) => `${key}: ${value}`)
.join('\n')}
---
${body}`;
const result = convert(
convertImageFrontMatter(
this.isEnableBanner,
this.fileName,
this.resourcePath,
replaceDateFrontMatter(
{ ...frontMatter },
this.isEnableUpdateFrontmatterTimeOnEdit,
),
),
);

return join(result, body);
}

convertImageFrontMatter(frontMatter: FrontMatter) {
if (!this.isEnableBanner) {
return frontMatter;
}
}

if (!frontMatter.image) {
return frontMatter;
}
function convertImageFrontMatter(
isEnable: boolean,
fileName: string,
resourcePath: string,
frontMatter: FrontMatter
) {
if (!isEnable) {
return frontMatter;
}

if (ObsidianRegex.ATTACHMENT_LINK.test(frontMatter.image)) {
const match = frontMatter.image.match(ObsidianRegex.ATTACHMENT_LINK);
if (match) {
frontMatter.image = frontMatter.image.replace(ObsidianRegex.ATTACHMENT_LINK, '$1.$2');
}
}
frontMatter.image = convertImagePath(this.fileName, frontMatter.image, this.resourcePath);
if (!frontMatter.image) {
return frontMatter;
}

if (ObsidianRegex.ATTACHMENT_LINK.test(frontMatter.image)) {
const match = frontMatter.image.match(ObsidianRegex.ATTACHMENT_LINK);
if (match) {
frontMatter.image = frontMatter.image.replace(ObsidianRegex.ATTACHMENT_LINK, '$1.$2');
}
}
frontMatter.image = convertImagePath(fileName, frontMatter.image, resourcePath);
return frontMatter;
}


function convertImagePath(postTitle: string, imagePath: string, resourcePath: string): string {
return `/${resourcePath}/${postTitle}/${imagePath}`;
}
Expand All @@ -124,4 +148,20 @@ function replaceDateFrontMatter(frontMatter: FrontMatter, isEnable: boolean): Fr
}

// TODO: "tag: mise, something" -> "tag: [mise, something]"
export const convertFrontMatter = (input: string) => {
const [frontMatter, body] = parseFrontMatter(input);
if (Object.keys(frontMatter).length === 0) {
return input;
}

// remove unnecessary frontMatter like `aliases: ""`
delete frontMatter['aliases'];
// Object.keys(frontMatter).forEach((key) => {
// if (frontMatter[key] === 'aliases') {
// delete frontMatter[key];
// }
// });

return join(frontMatter, body);
};

61 changes: 60 additions & 1 deletion src/tests/FrontMatterConverter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrontMatterConverter } from '../jekyll/FrontMatterConverter';
import { convertFrontMatter, FrontMatterConverter } from '../jekyll/FrontMatterConverter';

const frontMatterConverter = new FrontMatterConverter('2023-01-01-test-title', 'assets/img', true);
const disableImageConverter = new FrontMatterConverter('2023-01-01-test-title', 'assets/img', false);
Expand Down Expand Up @@ -293,5 +293,64 @@ tags: test1, test2
const result = frontMatterConverter.convert(contents);
expect(result).toEqual(expected);
});
});

describe('convertFrontMatter', () => {
it('should passthroughs', () => {
const mockContents = `---
title: "test"
date: 2021-01-01 12:00:00 +0900
categories: [test]
---
# test
`;
const result = convertFrontMatter(mockContents);
expect(result).toEqual(mockContents);
},
);

it('should converted tags', () => {
const contents = `---
title: "test"
date: 2021-01-01 12:00:00 +0900
tags: test1, test2
---
# test
`;
const result = convertFrontMatter(contents);
expect(result).toEqual(
`---
title: "test"
date: 2021-01-01 12:00:00 +0900
tags: [test1, test2]
---
# test
`,
);
});

it('should delete aliases', () => {
const contents = `---
title: "test"
date: 2021-01-01 12:00:00 +0900
aliases: ""
---
# test
`;
const result = convertFrontMatter(contents);
expect(result).toEqual(
`---
title: "test"
date: 2021-01-01 12:00:00 +0900
---
# test
`,
);
});

});

0 comments on commit eb1cd0c

Please sign in to comment.