Implement date highlighting

This commit is contained in:
lorenzovaccarini 2023-08-03 21:02:42 +02:00
parent ccec3e184c
commit ae26d897b1
5 changed files with 47 additions and 25 deletions

0
.nosync Normal file
View file

View file

@ -1,22 +1,23 @@
import wink from "wink-nlp";
import model from "wink-eng-lite-web-model";
import {readFileSync} from "fs";
import {iCloudServiceStatus} from "../iCloudJs";
import EventEmitter from "events";
export default class NPLController{
class NPLController{
private _customPatterns: {name, patterns}[];
private _pluginPath: string;
private _nlp;
private _ready: boolean;
constructor(pluginPath: string) {
constructor() {
this._ready = false;
this._nlp = wink( model );
this._ready = true;
}
init(pluginPath: string){
this._pluginPath = pluginPath;
this.loadPatterns();
this._nlp = wink( model );
this._nlp.learnCustomEntities(this._customPatterns);
this._ready = true;
}
loadPatterns(){
@ -32,14 +33,15 @@ export default class NPLController{
this._customPatterns.push({name: "duration", patterns: ["DURATION"]});
}
async process(text: string){
process(text: string): string | null {
if(!this._ready){
console.log("NPL not ready");
return;
}
text = text.replaceAll("\n", ".");
const its = this._nlp.its;
const doc = this._nlp.readDoc(text);
const match = doc.customEntities().out(its.value);
return match.length == 0 ? null : match[0];
//doc.sentences().each(sentence => sentence.customEntities().each(entity => console.log(entity.out(its.detail))));
//doc.customEntities().each(e => e.markup(`<span class='${e.out(its.type)} entity'>`, `</span>`));
//console.log(doc.out(its.markedUpText))
@ -61,3 +63,6 @@ export default class NPLController{
//console.log(doc.out(its.markedUpText));
}
}
const nplController = new NPLController();
export default nplController;

8
src/misc/misc.ts Normal file
View file

@ -0,0 +1,8 @@
export class Misc {
pluginPath: string;
setPath(pluginPath: string){
this.pluginPath = pluginPath;
}
}
const misc = new Misc();
export default misc;

View file

@ -3,6 +3,7 @@ import iCloudService, {iCloudServiceStatus} from "../iCloudJs";
import PluginController from "../controllers/pluginController";
import SafeController from "../controllers/safeController";
import {iCloudStatusModal} from "./modal";
import nplController from "../controllers/nplController";
import {NPLStateField} from "./nplExtension";
interface Settings {
@ -28,7 +29,8 @@ export default class iCalObsidianSync extends Plugin {
const basePath = (this.app.vault.adapter as any).basePath
const pluginPath =`${basePath}/.obsidian/plugins/obsidian-ical-sync`;
const nplStateField = new NPLStateField(pluginPath);
nplController.init(pluginPath);
const nplStateField = new NPLStateField();
this.registerEditorExtension(nplStateField.stateField);
safeController.injectPath(pluginPath);

View file

@ -1,41 +1,48 @@
import { Extension, RangeSetBuilder, StateField, Transaction, } from "@codemirror/state";
import { Decoration, DecorationSet, EditorView } from "@codemirror/view";
import NPLController from "../controllers/nplController";
import nplController from "../controllers/nplController";
import { syntaxTree } from "@codemirror/language";
//class testWidget
export class NPLStateField {
stateField: StateField<DecorationSet>;
private _npl: NPLController;
constructor(pluginPath: string) {
constructor() {
this.stateField = StateField.define<DecorationSet>({
create: this.create,
update: this.update,
provide: this.provide
});
this._npl = new NPLController(pluginPath);
this._npl.loadPatterns();
}
create(state): DecorationSet {
return Decoration.none;
}
builderAdd(builder, listCharFrom, listCharTo){
builder.add(
listCharFrom,
listCharTo,
Decoration.mark({
tagName: "mark"
})
);
}
update(oldState: DecorationSet, transaction: Transaction): DecorationSet {
const builder = new RangeSetBuilder<Decoration>();
const sentences = transaction.state.doc.toJSON();
//sentences.forEach()
console.log(sentences);
sentences.forEach((sentence, i) => {
const match = nplController.process(sentence);
if(match == null) return;
console.log("Match!");
let previousChars = 0;
for (let j=0; j < i; j++){
previousChars += sentences[j].length + 1;
}
const startsFrom = previousChars + sentence.indexOf(match)
const endsTo = startsFrom + match.length;
console.log(startsFrom, endsTo);
builder.add(
startsFrom,
endsTo,
Decoration.mark({
tagName: "mark"
})
);
});
return builder.finish();
}