Skip to content

Commit de84c5d

Browse files
committed
refactor: update frontmatter block removal function
1 parent d116565 commit de84c5d

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

src/main.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MarkdownView, Notice, Plugin, TFile, normalizePath } from "obsidian";
2-
import { escapeInvalidFileNameChars, findFrontmatterEndIndex, trimForFileName } from "./utils";
2+
import { escapeInvalidFileNameChars, removeFrontmatterBlock, trimForFileName } from "./utils";
33
import NoteSplitterSettingsTab from "./obsidian/note-splitter-settings-tab";
44

55
interface NoteSplitterSettings {
@@ -61,14 +61,9 @@ export default class NoteSplitterPlugin extends Plugin {
6161
return;
6262
}
6363

64-
const fileData = await this.app.vault.cachedRead(file);
65-
const frontmatterEndIndex = findFrontmatterEndIndex(fileData);
64+
const data = await this.app.vault.cachedRead(file);
6665

67-
let dataWithoutFrontmatter = fileData;
68-
//Ignore frontmatter
69-
if (frontmatterEndIndex !== -1) {
70-
dataWithoutFrontmatter = dataWithoutFrontmatter.slice(frontmatterEndIndex + 1);
71-
}
66+
const dataWithoutFrontmatter = removeFrontmatterBlock(data);
7267
if (dataWithoutFrontmatter === "") {
7368
new Notice("No content to split.");
7469
return;

src/utils.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
export const findFrontmatterEndIndex = (value: string) => {
1+
export const removeFrontmatterBlock = (data: string) => {
22
// Define the regular expression for the frontmatter block
3-
const regex = /^---\n([\s\S]*?)\n---/;
4-
5-
// Execute the regex on the string
6-
const match = regex.exec(value);
7-
8-
// If a match is found, return the index where the block ends
9-
if (match) {
10-
// The ending index is the starting index of the match plus its length
11-
return match[0].length;
12-
}
13-
// If no match is found, return -1
14-
return -1;
3+
const FRONTMATTER_REGEX = /^---\n([\s\S]*?)\n---/;
4+
return data.replace(FRONTMATTER_REGEX, "").trim();
155
};
166

177
/**

0 commit comments

Comments
 (0)