Skip to content

Commit 381cfe5

Browse files
committed
Adds automatically opening new file
1 parent 0eae448 commit 381cfe5

File tree

5 files changed

+115
-31
lines changed

5 files changed

+115
-31
lines changed

data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"headlineText":"Highlights for: \"$NOTE_TITLE\"","addFootnotes":true,"useBoldForHighlights":false,"createLinks":true,"autoCapitalize":true}
1+
{"headlineText":"Highlights for: \"$NOTE_TITLE\"","addFootnotes":false,"useBoldForHighlights":false,"createLinks":false,"autoCapitalize":true,"createNewFile":true}

main.js

Lines changed: 72 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ExtractHighlightsPluginSettings.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ export default class ExtractHighlightsPluginSettings {
44
public useBoldForHighlights: boolean;
55
public createLinks: boolean;
66
public autoCapitalize: boolean;
7+
public createNewFile: boolean;
78

89
constructor() {
910
this.headlineText = "";
1011
this.addFootnotes = false;
1112
this.useBoldForHighlights = false;
1213
this.createLinks = false;
1314
this.autoCapitalize = false;
15+
this.createNewFile = false;
1416
}
1517
}

src/ExtractHighlightsPluginSettingsTab.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,18 @@ export default class ExtractHighlightsPluginSettingsTab extends PluginSettingTab
7676
this.plugin.settings.autoCapitalize = value;
7777
this.plugin.saveData(this.plugin.settings);
7878
}),
79-
); }
79+
);
80+
81+
new Setting(containerEl)
82+
.setName('Open new file with highlights')
83+
.setDesc(
84+
'If enabled, opens a new file with the highlights copied into.',
85+
)
86+
.addToggle((toggle) =>
87+
toggle.setValue(this.plugin.settings.createNewFile).onChange((value) => {
88+
this.plugin.settings.createNewFile = value;
89+
this.plugin.saveData(this.plugin.settings);
90+
}),
91+
);
92+
}
8093
}

src/main.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,37 @@ export default class ExtractHighlightsPlugin extends Plugin {
5858
this.settings.addFootnotes = loadedSettings.addFootnotes;
5959
this.settings.createLinks = loadedSettings.createLinks;
6060
this.settings.autoCapitalize = loadedSettings.autoCapitalize;
61+
this.settings.createNewFile = loadedSettings.createNewFile;
6162
} else {
6263
console.log("No settings file found, saving...");
6364
this.saveData(this.settings);
6465
}
6566
})();
6667
}
6768

68-
extractHighlights(): void {
69+
async extractHighlights() {
6970
let activeLeaf: any = this.app.workspace.activeLeaf ?? null
7071

71-
console.log(this.app.workspace.activeLeaf?.view);
72+
console.log(activeLeaf?.view.file);
73+
74+
let name = activeLeaf?.view.file.basename;
7275

7376
try {
7477
if (activeLeaf?.view?.data) {
7578
let highlightsText = this.processHighlights(activeLeaf.view);
7679
let saveStatus = this.saveToClipboard(highlightsText);
7780
new Notice(saveStatus);
81+
82+
if (this.settings.createNewFile) {
83+
const newBasename = "Highlights for " + name + ".md";
84+
console.log(newBasename);
85+
// Add link back to Original
86+
highlightsText += `\n\n## Source\n- [[${name}]]`;
87+
88+
await this.saveToFile(newBasename, highlightsText);
89+
await this.app.workspace.openLinkText(newBasename, newBasename, true);
90+
}
91+
7892
} else {
7993
new Notice("No highlights to extract.");
8094
}
@@ -83,6 +97,16 @@ export default class ExtractHighlightsPlugin extends Plugin {
8397
}
8498
}
8599

100+
async saveToFile(filePath: string, mdString: string) {
101+
//If files exists then append content to existing file
102+
const fileExists = await this.app.vault.adapter.exists(filePath);
103+
if (fileExists) {
104+
105+
} else {
106+
await this.app.vault.create(filePath, mdString);
107+
}
108+
}
109+
86110
processHighlights(view: any): string {
87111

88112
var re;

0 commit comments

Comments
 (0)