mirror of
https://github.com/akaalias/extract-highlights-plugin.git
synced 2026-07-22 05:10:29 +00:00
Adds toggling for sentences
This commit is contained in:
parent
641ec898dd
commit
d38bc97fd5
5 changed files with 132 additions and 148 deletions
99
main.js
99
main.js
File diff suppressed because one or more lines are too long
|
|
@ -1,37 +1,39 @@
|
|||
export default class ToggleHighlight {
|
||||
|
||||
toggleHighlight(s: string, ch?: number) {
|
||||
console.log(s);
|
||||
console.log(ch);
|
||||
|
||||
if(s == "") return "";
|
||||
if(s.indexOf(".") < 0) { return "==" + s + "=="}
|
||||
|
||||
// insert cursor-marker
|
||||
let left = s.substring(0, ch);
|
||||
let right = s.substring(ch, s.length);
|
||||
let markedLine = left + "$CURSOR$" + right;
|
||||
let right = s.substring(ch);
|
||||
let marked = left + "$CURSOR$" + right;
|
||||
|
||||
if(ch != null) {
|
||||
// https://regex101.com/r/BSpvV6/7
|
||||
let p = marked.match(/(==(.*?)==)|[^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$)/gm);
|
||||
|
||||
if(ch == s.length) { return s; }
|
||||
let np = new Array();
|
||||
|
||||
const parts = markedLine.split(".");
|
||||
if(p.length > 0) {
|
||||
p.forEach(function (part) {
|
||||
if(typeof part !== 'undefined' ) {
|
||||
if(part.trim() == "") { return; }
|
||||
|
||||
if(ch <= 0) { return "==" + parts[0].replace("$CURSOR$", "") + ".== " + parts[1].trimLeft() + "."; }
|
||||
if(part.includes("$CURSOR$")) {
|
||||
|
||||
let result = ""
|
||||
|
||||
for (let entry of parts) {
|
||||
if(entry.indexOf("$CURSOR$") >= 0) {
|
||||
entry = entry.replace("$CURSOR$", "");
|
||||
entry = ". ==" + entry.trimLeft() + ".==";
|
||||
if(part.startsWith("==") && part.endsWith("==")) {
|
||||
part = part.replace(/==/g, "");
|
||||
} else {
|
||||
part = "==" + part + "==";
|
||||
}
|
||||
part = part.replace("$CURSOR$", "");
|
||||
part = part.trim();
|
||||
}
|
||||
part = part.trim();
|
||||
np.push(part);
|
||||
}
|
||||
result += entry;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return `==${s}==`;
|
||||
return np.join(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
70
src/main.ts
70
src/main.ts
|
|
@ -2,6 +2,7 @@ import {Plugin, Notice, addIcon, View, MarkdownView} from "obsidian"
|
|||
import ExtractHighlightsPluginSettings from "./ExtractHighlightsPluginSettings"
|
||||
import ExtractHighlightsPluginSettingsTab from "./ExtractHighlightsPluginSettingsTab"
|
||||
import {Position} from "codemirror";
|
||||
import ToggleHighlight from "./ToggleHighlight";
|
||||
|
||||
addIcon('target', '<path d="M50 88C29.0132 88 12 70.9868 12 50C12 29.0132 29.0132 12 50 12C70.9868 12 88 29.0132 88 50C87.9761 70.9769 70.9769 87.9761 50 88ZM50 22.8571C35.0094 22.8571 22.8571 35.0094 22.8571 50C22.8571 64.9906 35.0094 77.1429 50 77.1429C64.9906 77.1429 77.1429 64.9906 77.1429 50C77.1429 35.0094 64.9906 22.8571 50 22.8571ZM50 66.2857C41.0056 66.2857 33.7143 58.9943 33.7143 50C33.7143 41.0056 41.0056 33.7143 50 33.7143C58.9943 33.7143 66.2857 41.0056 66.2857 50C66.2857 54.3192 64.5699 58.4616 61.5157 61.5157C58.4616 64.5699 54.3192 66.2857 50 66.2857Z" fill="#646464"/>')
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ export default class ExtractHighlightsPlugin extends Plugin {
|
|||
this.addCommand({
|
||||
id: "shortcut-highlight-sentence",
|
||||
name: "Shortcut for highlighting sentence cursor is in",
|
||||
callback: () => this.toggleLineHighlight(),
|
||||
callback: () => this.createHighlight(),
|
||||
hotkeys: [
|
||||
{
|
||||
modifiers: ["Alt", "Shift"],
|
||||
|
|
@ -171,73 +172,18 @@ export default class ExtractHighlightsPlugin extends Plugin {
|
|||
}
|
||||
}
|
||||
|
||||
toggleHighlight() {
|
||||
this.toggleLineHighlight();
|
||||
}
|
||||
|
||||
toggleLineHighlight() {
|
||||
const cursorPosition = this.editor.getCursor();
|
||||
const ch = cursorPosition.ch;
|
||||
const line = cursorPosition.line;
|
||||
const lineText = this.editor.getLine(cursorPosition.line);
|
||||
|
||||
const allTextLeftOfCursor = lineText.substr(0, cursorPosition.ch);
|
||||
const allTextRightOfCursor = lineText.substr(cursorPosition.ch);
|
||||
|
||||
let periodIndexLeftOfCursor = allTextLeftOfCursor.lastIndexOf(".");
|
||||
|
||||
if(periodIndexLeftOfCursor == -1) { periodIndexLeftOfCursor = 0; }
|
||||
|
||||
let sentenceUntilLeftOfCursor = allTextLeftOfCursor.substr(periodIndexLeftOfCursor, ch - periodIndexLeftOfCursor);
|
||||
|
||||
if(sentenceUntilLeftOfCursor.startsWith(". ")) {
|
||||
sentenceUntilLeftOfCursor = sentenceUntilLeftOfCursor.replace(". ", "")
|
||||
}
|
||||
|
||||
let periodIndexRightOfCursor = allTextRightOfCursor.indexOf(".");
|
||||
|
||||
if(periodIndexRightOfCursor == -1) {
|
||||
periodIndexRightOfCursor = allTextRightOfCursor.length
|
||||
}
|
||||
|
||||
let sentenceUntilRightOfCursor = allTextRightOfCursor.substr(0, periodIndexRightOfCursor + 1);
|
||||
let currentSentence = sentenceUntilLeftOfCursor + sentenceUntilRightOfCursor;
|
||||
let absolutePeriodIndexRightOfCursor = periodIndexRightOfCursor + allTextLeftOfCursor.length;
|
||||
|
||||
currentSentence = "==" + currentSentence + "==";
|
||||
|
||||
let replaceOffset = periodIndexLeftOfCursor;
|
||||
|
||||
if(allTextLeftOfCursor.contains(".")) {
|
||||
replaceOffset = periodIndexLeftOfCursor + 2
|
||||
}
|
||||
|
||||
this.editor.replaceRange(currentSentence, {line: line, ch: replaceOffset}, {line: line, ch: absolutePeriodIndexRightOfCursor + 1});
|
||||
this.editor.setCursor(cursorPosition);
|
||||
}
|
||||
|
||||
toggleFullLine() {
|
||||
createHighlight() {
|
||||
const cursorPosition = this.editor.getCursor();
|
||||
let lineText = this.editor.getLine(cursorPosition.line);
|
||||
|
||||
let highlightedLine = "";
|
||||
let startPosition: Position;
|
||||
let endPosition: Position;
|
||||
let th = new ToggleHighlight();
|
||||
let result = th.toggleHighlight(lineText, cursorPosition.ch);
|
||||
|
||||
if(lineText.startsWith("==") && lineText.endsWith("==")) {
|
||||
highlightedLine = lineText.replace(/==/g, "");
|
||||
startPosition = {line: cursorPosition.line, ch: 0};
|
||||
endPosition = {line: cursorPosition.line, ch: highlightedLine.length + 4};
|
||||
} else {
|
||||
highlightedLine = "==" + lineText + "==";
|
||||
startPosition = {line: cursorPosition.line, ch: 0};
|
||||
endPosition = {line: cursorPosition.line, ch: highlightedLine.length};
|
||||
}
|
||||
|
||||
this.editor.replaceRange(highlightedLine, startPosition, endPosition);
|
||||
this.editor.setCursor(cursorPosition);
|
||||
this.editor.replaceRange(result, {line: cursorPosition.line, ch: 0}, {line: cursorPosition.line, ch: lineText.length})
|
||||
this.editor.setCursor({line: cursorPosition.line, ch: cursorPosition.ch + 2});
|
||||
}
|
||||
|
||||
|
||||
capitalizeFirstLetter(s: string) {
|
||||
console.log("capitalizing...");
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import 'mocha';
|
||||
import { assert } from 'chai';
|
||||
import {assert} from 'chai';
|
||||
import ToggleHighlight from "../src/ToggleHighlight";
|
||||
|
||||
let subject: ToggleHighlight = null;
|
||||
|
|
@ -9,38 +9,70 @@ describe("Toggle Highlights", () => {
|
|||
subject = new ToggleHighlight();
|
||||
});
|
||||
|
||||
describe("Empty input", () => {
|
||||
xdescribe("Empty input", () => {
|
||||
it("Returns an empty string", () => {
|
||||
const result = subject.toggleHighlight("");
|
||||
assert.equal(result, "");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Turning Highlights on", () => {
|
||||
it("Returns a highlighted string", () => {
|
||||
const result = subject.toggleHighlight("Foo");
|
||||
describe("Turning Highlights ON", () => {
|
||||
it("Returns everything when there is no period", () => {
|
||||
const result = subject.toggleHighlight("Foo", 0);
|
||||
assert.equal(result, "==Foo==");
|
||||
});
|
||||
|
||||
it("Returns first highlighted sentence", () => {
|
||||
const result = subject.toggleHighlight("Foo.", 0);
|
||||
assert.equal(result, "==Foo.==");
|
||||
});
|
||||
|
||||
it("Returns first highlighted sentence with cursor position 0", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar.", 0);
|
||||
assert.equal(result, "==Foo.== Bar.");
|
||||
const result = subject.toggleHighlight("Foo. Bar. Baz.", 0);
|
||||
assert.equal(result, "==Foo.== Bar. Baz.");
|
||||
});
|
||||
|
||||
it("Returns second highlighted sentence with cursor position 4", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar.", 4);
|
||||
assert.equal(result, "Foo. ==Bar.==");
|
||||
it("Returns first highlighted sentence with cursor position 1", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar. Baz.", 1);
|
||||
assert.equal(result, "==Foo.== Bar. Baz.");
|
||||
});
|
||||
|
||||
it("Returns second highlighted sentence with cursor position 6", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar.", 6);
|
||||
const result = subject.toggleHighlight("Foo. Bar. Baz.", 6);
|
||||
assert.equal(result, "Foo. ==Bar.== Baz.");
|
||||
});
|
||||
|
||||
it("Returns second highlighted sentence with cursor position 8", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar. Baz.", 8);
|
||||
assert.equal(result, "Foo. ==Bar.== Baz.");
|
||||
});
|
||||
|
||||
it("Returns second highlighted sentence with cursor position 10", () => {
|
||||
const result = subject.toggleHighlight("==Foo.== Bar. Baz.", 10);
|
||||
assert.equal(result, "==Foo.== ==Bar.== Baz.");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Turning Highlights OFF", () => {
|
||||
it("Returns sentence", () => {
|
||||
const result = subject.toggleHighlight("==Foo.==", 2);
|
||||
assert.equal(result, "Foo.");
|
||||
});
|
||||
|
||||
it("Returns first un-highlighted and second sentence", () => {
|
||||
const result = subject.toggleHighlight("==Foo.== Bar.", 2);
|
||||
assert.equal(result, "Foo. Bar.");
|
||||
});
|
||||
|
||||
it("Returns first sentence un-highlighted", () => {
|
||||
const result = subject.toggleHighlight("==Foo.== ==Bar.==", 2);
|
||||
assert.equal(result, "Foo. ==Bar.==");
|
||||
});
|
||||
|
||||
it("Returns no highlighted sentence with cursor position 9", () => {
|
||||
const result = subject.toggleHighlight("Foo. Bar.", 9);
|
||||
assert.equal(result, "Foo. Bar.");
|
||||
it("Returns second sentence un-highlighted", () => {
|
||||
const result = subject.toggleHighlight("==Foo.== ==Bar.==", 11);
|
||||
assert.equal(result, "==Foo.== Bar.");
|
||||
});
|
||||
});
|
||||
});
|
||||
7
tsconfig.testing.json
Normal file
7
tsconfig.testing.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6"
|
||||
},
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
Loading…
Reference in a new issue