Skip to content

Commit bcd4903

Browse files
authored
feat: o2-23 support obsidian footnote syntax (#59)
1 parent 4362bbd commit bcd4903

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/ObsidianRegex.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export const ObsidianRegex = {
22
IMAGE_LINK: /!\[\[([^|\]]+)\|?(\d*)]]/g,
33
WIKI_LINK: /(?<!!)\[\[([^|\]]+)\|?(.*)]]/g,
44
CALLOUT: /> \[!(.*)].*?\n(>.*)/ig,
5+
SIMPLE_FOOTNOTE: /\[\^(\d+)]/g
56
} as const;

src/jekyll/FootnotesConverter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { AbstractConverter } from "../core/Converter";
2+
import { ObsidianRegex } from "../ObsidianRegex";
3+
4+
export class FootnotesConverter extends AbstractConverter {
5+
6+
convert(input: string): string {
7+
const result = input.replace(ObsidianRegex.SIMPLE_FOOTNOTE, (match, key) => {
8+
return `[^fn-nth-${key}]`;
9+
});
10+
return super.convert(result);
11+
}
12+
13+
}

src/jekyll/chirpy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Notice, TFile } from "obsidian";
88
import { CalloutConverter } from "./CalloutConverter";
99
import { FrontMatterConverter } from "./FrontMatterConverter";
1010
import { vaultAbsolutePath } from "../utils";
11+
import { FootnotesConverter } from "./FootnotesConverter";
1112

1213
export async function convertToChirpy(plugin: O2Plugin) {
1314
// validation
@@ -35,10 +36,12 @@ export async function convertToChirpy(plugin: O2Plugin) {
3536
plugin.settings.jekyllSetting().jekyllRelativeResourcePath
3637
);
3738
const calloutConverter = new CalloutConverter();
39+
const footnotesConverter = new FootnotesConverter();
3840

3941
frontMatterConverter.setNext(wikiLinkConverter)
4042
.setNext(resourceLinkConverter)
41-
.setNext(calloutConverter);
43+
.setNext(calloutConverter)
44+
.setNext(footnotesConverter);
4245

4346
const result = frontMatterConverter.convert(await plugin.app.vault.read(file));
4447
await plugin.app.vault.modify(file, result);

src/tests/FootnotesConverter.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { FootnotesConverter } from "../jekyll/FootnotesConverter";
2+
3+
const converter = new FootnotesConverter();
4+
5+
describe("FootnotesConverter", () => {
6+
it("should convert simple footnotes", () => {
7+
const contents = `
8+
# Hello World
9+
10+
This is a simple footnote[^1]. next footnote[^2].
11+
12+
[^1]: meaningful
13+
14+
[^2]: meaningful 2
15+
16+
`;
17+
18+
const expected = `
19+
# Hello World
20+
21+
This is a simple footnote[^fn-nth-1]. next footnote[^fn-nth-2].
22+
23+
[^fn-nth-1]: meaningful
24+
25+
[^fn-nth-2]: meaningful 2
26+
27+
`;
28+
const actual = converter.convert(contents);
29+
expect(actual).toEqual(expected);
30+
});
31+
32+
it.todo("should convert footnotes with multiple lines");
33+
it.todo("should convert inline footnotes");
34+
});

0 commit comments

Comments
 (0)