Skip to content

Commit

Permalink
feat: add configuration settings
Browse files Browse the repository at this point in the history
  • Loading branch information
decaf-dev committed Dec 13, 2023
1 parent 8b0e2c5 commit 8b3500e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 19 deletions.
97 changes: 78 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,123 @@
import { Editor, MarkdownView, Notice, Plugin } from "obsidian";
import { findFrontmatterEndIndex } from "./utils";
import NoteSplitterSettingsTab from "./setting-tab";

interface NoteSplitterSettings {
saveFolderPath: string;
groupFolderName: string;
useContentAsTitle: boolean;
delimiter: string;
}

const DEFAULT_SETTINGS: NoteSplitterSettings = {
saveFolderPath: "note-splitter",
groupFolderName: "group",
useContentAsTitle: false,
delimiter: "\\n",
}


export default class NoteSplitterPlugin extends Plugin {
settings: NoteSplitterSettings;

async onload() {
await this.loadSettings();

this.addSettingTab(new NoteSplitterSettingsTab(this.app, this));

this.addCommand({
id: "split-by-line",
name: "Split by line",
editorCallback: async (_editor: Editor, view: MarkdownView) => {
const file = view.file;
if (file) {
let data = await this.app.vault.cachedRead(file);
const frontmatterEndIndex = findFrontmatterEndIndex(data);
let delimeter = this.settings.delimiter;
//Obsidian will store `\n`` as `\\n` in the settings
delimeter = delimeter.replace(/\\n/g, "\n");

if (delimeter === "") {
new Notice("No delimiter set. Please set a delimiter in the settings");
return;
}

const fileData = await this.app.vault.cachedRead(file);
const frontmatterEndIndex = findFrontmatterEndIndex(fileData);

let dataWithoutFrontmatter = fileData;
//Ignore frontmatter
if (frontmatterEndIndex !== -1) {
data = data.slice(frontmatterEndIndex + 1);
dataWithoutFrontmatter = dataWithoutFrontmatter.slice(frontmatterEndIndex + 1);
}
if (data === ""){
if (dataWithoutFrontmatter === "") {
new Notice("No content to split");
return;
}

const split = data.split("\n").filter((line) =>
line !== ""
const splitLines = dataWithoutFrontmatter.split(delimeter).filter((line) =>
line !== ""
);

if (split.length === 0) {

if (splitLines.length === 0) {
new Notice("No content to split");
return;
}

if (split.length === 1) {
new Notice("No need to split");
if (splitLines.length === 1) {
new Notice("Only one line found. Nothing to split");
return;
}

const currentTime = Date.now();
const folderPath = this.settings.saveFolderPath;

try {
await this.app.vault.createFolder("note-splitter");
} catch (err){
await this.app.vault.createFolder(folderPath);
} catch (err) {
//Folder already exists
}

const currentTime = Date.now();
const groupFolderName = this.settings.groupFolderName;
const groupFolderPath = `${folderPath}/${groupFolderName}-${currentTime}`;

try {
await this.app.vault.createFolder(`note-splitter/group-${currentTime}`);
}catch(err){
await this.app.vault.createFolder(groupFolderPath);
} catch (err) {
//Folder already exists
}

for (let i = 0; i < split.length; i++) {
const line = split[i].trim();
for (let i = 0; i < splitLines.length; i++) {
const line = splitLines[i].trim();

let fileName = "";
if (this.settings.useContentAsTitle) {
fileName = line;
//If the line has a period, use the text before the period as the title
if (line.includes(". ")) {
fileName = line.split(". ")[0];
}
} else {
fileName = `split-note-${Date.now() + i}`;
}

const filePath = `${groupFolderPath}/${fileName}.md`
await this.app.vault.create(
`note-splitter/group-${currentTime}/split-note-${Date.now() + i}.md`, line
filePath, line
);
}
new Notice("Split into " + split.length + " note" + (split.length > 1 ? "s" : ""));
new Notice("Split into " + splitLines.length + " note" + (splitLines.length > 1 ? "s" : ""));
}
},
});
}

onunload() {}
onunload() { }

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}
}
67 changes: 67 additions & 0 deletions src/setting-tab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import NoteSplitterPlugin from "./main";

export default class NoteSplitterSettingsTab extends PluginSettingTab {

plugin: NoteSplitterPlugin;

constructor(app: App, plugin: NoteSplitterPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display() {
let { containerEl } = this;

containerEl.empty();

new Setting(containerEl)
.setName("Folder path")
.setDesc("The path to the folder that split notes will be placed in")
.addText((text) =>
text
.setValue(this.plugin.settings.saveFolderPath)
.onChange(async (value) => {
this.plugin.settings.saveFolderPath = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Group name")
.setDesc("The name of the group folder that split notes will be placed in. A timestamp will be added to this name")
.addText((text) =>
text
.setValue(this.plugin.settings.groupFolderName)
.onChange(async (value) => {
this.plugin.settings.groupFolderName = value;
await this.plugin.saveSettings();
})
);


new Setting(containerEl)
.setName("Delimeter")
.setDesc("The delimeter to split by")
.addText((text) =>
text
.setValue(this.plugin.settings.delimiter)
.onChange(async (value) => {
this.plugin.settings.delimiter = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Use content as title")
.setDesc("If true, the sentence will be used as the title of the note, otherwise a timestamp will be used")
.addToggle((text) =>
text
.setValue(this.plugin.settings.useContentAsTitle)
.onChange(async (value) => {
this.plugin.settings.useContentAsTitle = value;
await this.plugin.saveSettings();
})
);
}
}

0 comments on commit 8b3500e

Please sign in to comment.