Adds explode mode

This commit is contained in:
Alexis Rondeau 2021-01-14 15:08:32 +01:00
parent 77017cedeb
commit 4a486c9d2e
6 changed files with 235 additions and 77 deletions

View file

@ -1,7 +1,7 @@
## Extract Highlights Plugin
![](https://github.com/akaalias/extract-highlights-plugin/workflows/Node.js%20CI/badge.svg)
Extracts all highlights from a current note in Obsidian into your clipboard. Based on [icebear's plugin request](https://forum.obsidian.md/t/extract-highlights-from-note/7867).
Create and extract highlights from a current markdown note in Obsidian into your clipboard. Based on [icebear's plugin request](https://forum.obsidian.md/t/extract-highlights-from-note/7867).
### Watch the Quick-Start Tutorial!
@ -19,15 +19,11 @@ Optionally you can customize...
### Demo Creating and Extracting Highlights
1. First you see how I use the CREATE highlights hot-key to highlight sentences
2. Then you see how I use the EXTRACT highlights hot-key to create a new file with the highlights
![basic functionality](https://github.com/akaalias/extract-highlights-plugin/blob/master/highlight.gif?raw=true)
#### Using the Hotkey to EXTRACT highlights
The default hotkey is:
<kbd>SHIFT</kbd> + <kbd>ALT</kbd> + <kbd>=</kbd>
#### Using the Hotkey to HIGHLIGHT (and UN-HIGHLIGHT) the sentence under cursor
The default hotkey for this is:
@ -40,6 +36,12 @@ Super useful for when you're reading and just don't want to switch to your mouse
Will remove highlighting if the sentence under your cursor is currently delimited by "==".
#### Using the Hotkey to EXTRACT highlights
The default hotkey is:
<kbd>SHIFT</kbd> + <kbd>ALT</kbd> + <kbd>=</kbd>
#### Using the Ribbon Button
There is also a button (a circle-shape) that's added to your left-side ribbon.
@ -71,7 +73,28 @@ Are you using Extract Highlights? I'd love to hear from you!
### Backlog
#### UI
#### TODO
- Record video on using the "explosion mode" for research and creating atomic notes
- Pre-requisites
- Highlights Plugin
- Create links
- Create page
- Enable explode mode
- Open notes on creation
- Sliding Panes Plugin
- Start with a good article (Economist)
- Go through and highlight sentences
- Create MOC and explode into notes
- BOOOMMMMM!!!
- You've got an MOC
- You've got the core for single-idea, atomic notes
- You've got a backlink to the original file
#### DOING
...
#### DONE
- [x] "Explode" highlights into individual notes (assumes I'm creating the list of links as well)
- [x] command (<kbd>SHIFT</kbd> + <kbd>ALT</kbd> + <kbd>=</kbd>) which then copies all of the highlighted text either into:
- [x] click a button which then copies all of the highlighted text either into:
- [x] allow for `<mark></mark>` to be used as highlights
@ -82,10 +105,5 @@ Are you using Extract Highlights? I'd love to hear from you!
- [x] allow to add footnotes for each highlight and include link to source-note
- [x] allow to optionally enable bold for highlights
- [x] allow for Command Palette to trigger copying (Works sort of, bug in Electron)
#### Outputs
- [x] my clipboard
- [ ] the top/botton of the note and selected for a next step,
- [ ] a designated area in the note such as {{highlights}}, or
- [ ] a new note
- [x] a new note

View file

@ -1 +1 @@
{"headlineText":"Highlights for: \"$NOTE_TITLE\"","addFootnotes":false,"useBoldForHighlights":false,"createLinks":false,"autoCapitalize":true,"createNewFile":true}
{"headlineText":"","addFootnotes":false,"useBoldForHighlights":false,"createLinks":true,"autoCapitalize":true,"createNewFile":true,"explodeIntoNotes":true,"openExplodedNotes":true}

129
main.js

File diff suppressed because one or more lines are too long

View file

@ -5,6 +5,8 @@ export default class ExtractHighlightsPluginSettings {
public createLinks: boolean;
public autoCapitalize: boolean;
public createNewFile: boolean;
public explodeIntoNotes: boolean;
public openExplodedNotes: boolean;
constructor() {
this.headlineText = "";
@ -13,5 +15,7 @@ export default class ExtractHighlightsPluginSettings {
this.createLinks = false;
this.autoCapitalize = false;
this.createNewFile = false;
this.explodeIntoNotes = false;
this.openExplodedNotes = false;
}
}

View file

@ -54,18 +54,6 @@ export default class ExtractHighlightsPluginSettingsTab extends PluginSettingTab
}),
);
new Setting(containerEl)
.setName('Create links')
.setDesc(
'If enabled, will turn each highlight into a [[ link ]] to create a highlight MOC',
)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.createLinks).onChange((value) => {
this.plugin.settings.createLinks = value;
this.plugin.saveData(this.plugin.settings);
}),
);
new Setting(containerEl)
.setName('Auto-capitalize first letter')
.setDesc(
@ -78,6 +66,26 @@ export default class ExtractHighlightsPluginSettingsTab extends PluginSettingTab
}),
);
new Setting(containerEl)
.setName('Create links')
.setDesc(
'If enabled, will turn each highlight into a [[ link ]] to create a highlight MOC',
)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.createLinks).onChange((value) => {
this.plugin.settings.createLinks = value;
// disable explode notes mode
if(this.plugin.settings.explodeIntoNotes && value == false) {
this.plugin.settings.explodeIntoNotes = false;
this.plugin.settings.openExplodedNotes = false;
}
this.plugin.saveData(this.plugin.settings);
}),
);
new Setting(containerEl)
.setName('Open new file with highlights')
.setDesc(
@ -86,8 +94,46 @@ export default class ExtractHighlightsPluginSettingsTab extends PluginSettingTab
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.createNewFile).onChange((value) => {
this.plugin.settings.createNewFile = value;
// disable explode notes mode
if(this.plugin.settings.explodeIntoNotes && value == false) {
this.plugin.settings.explodeIntoNotes = false;
this.plugin.settings.openExplodedNotes = false;
}
this.plugin.saveData(this.plugin.settings);
}),
);
containerEl.createEl("h2", {text: "💥 Explode Notes Mode 💥"});
containerEl.createEl("p", {text: "A secret mode that will take your highlighting to the next level. Only available if you have 'Create Links' and 'Create new File' enabled. After enabling both, close this window and open again to see options."});
if(this.plugin.settings.createLinks && this.plugin.settings.createNewFile) {
new Setting(containerEl)
.setName('Explode links into notes')
.setDesc(
'If enabled, will turn each highlight into a note with the highlighted text as quote and a backlink to the MOC and source-file. Very powerful but use with caution!',
)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.explodeIntoNotes).onChange((value) => {
this.plugin.settings.explodeIntoNotes = value;
this.plugin.saveData(this.plugin.settings);
}),
);
new Setting(containerEl)
.setName('Open exploded notes on creation')
.setDesc(
'If enabled, will open each of your exploded notes when you create them. Fun and useful to continue working in your highlight-notes right away!',
)
.addToggle((toggle) =>
toggle.setValue(this.plugin.settings.openExplodedNotes).onChange((value) => {
this.plugin.settings.openExplodedNotes = value;
this.plugin.saveData(this.plugin.settings);
}),
);
}
}
}

View file

@ -59,6 +59,8 @@ export default class ExtractHighlightsPlugin extends Plugin {
this.settings.createLinks = loadedSettings.createLinks;
this.settings.autoCapitalize = loadedSettings.autoCapitalize;
this.settings.createNewFile = loadedSettings.createNewFile;
this.settings.explodeIntoNotes = loadedSettings.explodeIntoNotes;
this.settings.openExplodedNotes = loadedSettings.openExplodedNotes;
} else {
console.log("No settings file found, saving...");
this.saveData(this.settings);
@ -69,24 +71,44 @@ export default class ExtractHighlightsPlugin extends Plugin {
async extractHighlights() {
let activeLeaf: any = this.app.workspace.activeLeaf ?? null
console.log(activeLeaf?.view.file);
let name = activeLeaf?.view.file.basename;
try {
if (activeLeaf?.view?.data) {
let highlightsText = this.processHighlights(activeLeaf.view);
let highlightsText = this.processHighlights(activeLeaf.view).markdown;
let items = this.processHighlights(activeLeaf.view).items;
let saveStatus = this.saveToClipboard(highlightsText);
new Notice(saveStatus);
const newBasenameMOC = "Highlights for " + name + ".md";
if (this.settings.createNewFile) {
const newBasename = "Highlights for " + name + ".md";
console.log(newBasename);
// Add link back to Original
highlightsText += `\n\n## Source\n- [[${name}]]`;
highlightsText += `## Source\n- [[${name}]]`;
await this.saveToFile(newBasename, highlightsText);
await this.app.workspace.openLinkText(newBasename, newBasename, true);
await this.saveToFile(newBasenameMOC, highlightsText);
await this.app.workspace.openLinkText(newBasenameMOC, newBasenameMOC, true);
}
if(this.settings.createNewFile && this.settings.createLinks && this.settings.explodeIntoNotes) {
for(var i = 0; i < items.length; i++) {
console.log("Creating file for " + items[i]);
var content = "";
// add highlight as quote
content += "## Source\n"
content += `> ${items[i]}[^1]`;
content += "\n\n";
content += `[^1]: [[${name}]]`;
content += "\n";
console.log(content);
const newBasename = items[i] + ".md";
await this.saveToFile(newBasename, content);
if(this.settings.openExplodedNotes) {
await this.app.workspace.openLinkText(newBasename, newBasename, true);
}
}
}
} else {
@ -101,13 +123,13 @@ export default class ExtractHighlightsPlugin extends Plugin {
//If files exists then append content to existing file
const fileExists = await this.app.vault.adapter.exists(filePath);
if (fileExists) {
console.log("File exists already...");
} else {
await this.app.vault.create(filePath, mdString);
}
}
processHighlights(view: any): string {
processHighlights(view) {
var re;
@ -122,9 +144,8 @@ export default class ExtractHighlightsPlugin extends Plugin {
let matches = data.match(re);
this.counter += 1;
console.log(matches.length);
var result = "";
var items = [];
if (matches != null) {
if(this.settings.headlineText != "") {
@ -152,9 +173,15 @@ export default class ExtractHighlightsPlugin extends Plugin {
result += "- "
if(this.settings.createLinks) {
result += "[[" + removeDoubleSpaces + "]]";
// First, sanitize highlight to be used as a file-link
// * " \ / | < > : ?
let sanitized = removeDoubleSpaces.replace(/\*|\"|\\|\/|\<|\>|\:|\?|\|/gm, "");
sanitized = sanitized.trim();
result += "[[" + sanitized + "]]";
items.push(sanitized);
} else {
result += removeDoubleSpaces;
items.push(removeDoubleSpaces);
}
if(this.settings.addFootnotes) {
@ -172,15 +199,12 @@ export default class ExtractHighlightsPlugin extends Plugin {
result += "\n";
}
return result;
return {markdown: result, items: items};
}
saveToClipboard(data: string): string {
if (data.length > 0) {
console.log(data);
navigator.clipboard.writeText(data);
return "Highlights copied to clipboard!";
} else {
return "No highlights found";
@ -209,7 +233,6 @@ export default class ExtractHighlightsPlugin extends Plugin {
capitalizeFirstLetter(s: string) {
console.log("capitalizing...");
return s.charAt(0).toUpperCase() + s.slice(1);
}
}