Skip to content

Commit

Permalink
feat: o2-23 support obsidian footnote syntax (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
songkg7 authored Mar 11, 2023
1 parent 4362bbd commit bcd4903
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ObsidianRegex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const ObsidianRegex = {
IMAGE_LINK: /!\[\[([^|\]]+)\|?(\d*)]]/g,
WIKI_LINK: /(?<!!)\[\[([^|\]]+)\|?(.*)]]/g,
CALLOUT: /> \[!(.*)].*?\n(>.*)/ig,
SIMPLE_FOOTNOTE: /\[\^(\d+)]/g
} as const;
13 changes: 13 additions & 0 deletions src/jekyll/FootnotesConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AbstractConverter } from "../core/Converter";
import { ObsidianRegex } from "../ObsidianRegex";

export class FootnotesConverter extends AbstractConverter {

convert(input: string): string {
const result = input.replace(ObsidianRegex.SIMPLE_FOOTNOTE, (match, key) => {
return `[^fn-nth-${key}]`;
});
return super.convert(result);
}

}
5 changes: 4 additions & 1 deletion src/jekyll/chirpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Notice, TFile } from "obsidian";
import { CalloutConverter } from "./CalloutConverter";
import { FrontMatterConverter } from "./FrontMatterConverter";
import { vaultAbsolutePath } from "../utils";
import { FootnotesConverter } from "./FootnotesConverter";

export async function convertToChirpy(plugin: O2Plugin) {
// validation
Expand Down Expand Up @@ -35,10 +36,12 @@ export async function convertToChirpy(plugin: O2Plugin) {
plugin.settings.jekyllSetting().jekyllRelativeResourcePath
);
const calloutConverter = new CalloutConverter();
const footnotesConverter = new FootnotesConverter();

frontMatterConverter.setNext(wikiLinkConverter)
.setNext(resourceLinkConverter)
.setNext(calloutConverter);
.setNext(calloutConverter)
.setNext(footnotesConverter);

const result = frontMatterConverter.convert(await plugin.app.vault.read(file));
await plugin.app.vault.modify(file, result);
Expand Down
34 changes: 34 additions & 0 deletions src/tests/FootnotesConverter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { FootnotesConverter } from "../jekyll/FootnotesConverter";

const converter = new FootnotesConverter();

describe("FootnotesConverter", () => {
it("should convert simple footnotes", () => {
const contents = `
# Hello World
This is a simple footnote[^1]. next footnote[^2].
[^1]: meaningful
[^2]: meaningful 2
`;

const expected = `
# Hello World
This is a simple footnote[^fn-nth-1]. next footnote[^fn-nth-2].
[^fn-nth-1]: meaningful
[^fn-nth-2]: meaningful 2
`;
const actual = converter.convert(contents);
expect(actual).toEqual(expected);
});

it.todo("should convert footnotes with multiple lines");
it.todo("should convert inline footnotes");
});

0 comments on commit bcd4903

Please sign in to comment.