Adds optional auto-link creation for highlights

This commit is contained in:
Alexis Rondeau 2020-12-04 14:08:23 +01:00
parent 63d43f844f
commit b7ee431a2a
5 changed files with 162 additions and 121 deletions

View file

@ -1 +1 @@
{"headlineText":"Summary for: $NOTE_TITLE","addFootnotes":true,"useBoldForHighlights":true}
{"headlineText":"Highlights for $NOTE_TITLE","addFootnotes":true,"useBoldForHighlights":false,"createLinks":true}

124
main.js

File diff suppressed because one or more lines are too long

View file

@ -2,10 +2,12 @@ export default class ExtractHighlightsPluginSettings {
public headlineText: string;
public addFootnotes: boolean;
public useBoldForHighlights: boolean;
public createLinks: boolean;
constructor() {
this.headlineText = "";
this.addFootnotes = false;
this.useBoldForHighlights = false;
this.createLinks = false;
}
}

View file

@ -53,6 +53,18 @@ export default class ExtractHighlightsPluginSettingsTab extends PluginSettingTab
this.plugin.saveData(this.plugin.settings);
}),
);
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);
}),
);
}
}

View file

@ -66,6 +66,7 @@ export default class ExtractHighlightsPlugin extends Plugin {
console.log("Found existing settings file");
this.settings.headlineText = loadedSettings.headlineText;
this.settings.addFootnotes = loadedSettings.addFootnotes;
this.settings.createLinks = loadedSettings.createLinks;
} else {
console.log("No settings file found, saving...");
this.saveData(this.settings);
@ -89,6 +90,80 @@ export default class ExtractHighlightsPlugin extends Plugin {
}
}
processHighlights(view: any): string {
var re;
if(this.settings.useBoldForHighlights) {
re = /(==|\<mark\>|\*\*)([\s\S]*?)(==|\<\/mark\>|\*\*)/g;
} else {
re = /(==|\<mark\>)([\s\S]*?)(==|\<\/mark\>)/g;
}
let data = view.data;
let basename = view.file.basename;
let matches = data.match(re);
this.counter += 1;
console.log(matches.length);
var result = "";
if (matches != null) {
if(this.settings.headlineText != "") {
let text = this.settings.headlineText.replace(/\$NOTE_TITLE/, `${basename}`)
result += `## ${text}\n`;
}
for (let entry of matches) {
var removeNewline = entry.replace(/\n/g, " ");
let removeHighlightStart = removeNewline.replace(/==/g, "")
let removeHighlightEnd = removeHighlightStart.replace(/\<mark\>/g, "")
let removeMarkClosing = removeHighlightEnd.replace(/\<\/mark\>/g, "")
let removeBold = removeMarkClosing.replace(/\*\*/g, "")
let removeDoubleSpaces = removeBold.replace(" ", " ");
removeDoubleSpaces = removeDoubleSpaces.replace(" ", " ");
removeDoubleSpaces = removeDoubleSpaces.trim();
result += "- "
if(this.settings.createLinks) {
result += "[[" + removeDoubleSpaces + "]]";
} else {
result += removeDoubleSpaces;
}
if(this.settings.addFootnotes) {
result += `[^${this.counter}]`;
}
result += "\n";
}
if(this.settings.addFootnotes) {
result += "\n"
result += `[^${this.counter}]: [[${basename}]]\n`
}
result += "\n";
}
return result;
}
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";
}
}
toggleHighlight() {
this.toggleLineHighlight();
}
@ -155,70 +230,4 @@ export default class ExtractHighlightsPlugin extends Plugin {
this.editor.replaceRange(highlightedLine, startPosition, endPosition);
this.editor.setCursor(cursorPosition);
}
processHighlights(view: any): string {
var re;
if(this.settings.useBoldForHighlights) {
re = /(==|\<mark\>|\*\*)([\s\S]*?)(==|\<\/mark\>|\*\*)/g;
} else {
re = /(==|\<mark\>)([\s\S]*?)(==|\<\/mark\>)/g;
}
let data = view.data;
let basename = view.file.basename;
let matches = data.match(re);
this.counter += 1;
console.log(matches.length);
var result = "";
if (matches != null) {
if(this.settings.headlineText != "") {
let text = this.settings.headlineText.replace(/\$NOTE_TITLE/, `${basename}`)
result += `## ${text}\n`;
}
for (let entry of matches) {
var removeNewline = entry.replace(/\n/g, " ");
let removeHighlightStart = removeNewline.replace(/==/g, "")
let removeHighlightEnd = removeHighlightStart.replace(/\<mark\>/g, "")
let removeMarkClosing = removeHighlightEnd.replace(/\<\/mark\>/g, "")
let removeBold = removeMarkClosing.replace(/\*\*/g, "")
let removeDoubleSpaces = removeBold.replace(" ", " ");
removeDoubleSpaces = removeDoubleSpaces.replace(" ", " ");
removeDoubleSpaces = removeDoubleSpaces.trim();
result += "- " + removeDoubleSpaces
if(this.settings.addFootnotes) {
result += `[^${this.counter}]`;
}
result += "\n";
}
if(this.settings.addFootnotes) {
result += "\n"
result += `[^${this.counter}]: [[${basename}]]\n`
}
result += "\n";
}
return result;
}
saveToClipboard(data: string): string {
if (data.length > 0) {
navigator.clipboard.writeText(data);
return "Highlights copied to clipboard!";
} else {
return "No highlights found";
}
}
}