update structure, add suggestor

This commit is contained in:
Maris 2024-06-24 16:51:20 +02:00
parent 11f8caacad
commit b74c49567d
12 changed files with 341 additions and 120 deletions

View File

@ -15,7 +15,7 @@ const context = await esbuild.context({
banner: {
js: banner,
},
entryPoints: ["main.ts"],
entryPoints: ["src/main.ts"],
bundle: true,
external: [
"obsidian",

View File

@ -4,7 +4,7 @@
"version": "1.0.0",
"minAppVersion": "0.15.0",
"description": "bliblablub",
"author": "Silas",
"author": "Silas Schimmel & Maris Beer",
"authorUrl": "https://obsidian.md",
"fundingUrl": "https://obsidian.md/pricing",
"isDesktopOnly": false

View File

@ -1,27 +0,0 @@
import axios from 'axios';
// Configure API key authorization: api_key
const apiKey = "6e6a95bf6f99b1430ce89239aad0fd41e6159583"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//api_key.apiKeyPrefix['Authorization'] = "Token"
var callback = function(error: string, data: string, response: string) {
if (error) {
console.log(apiKey);
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
export function getData(search: string) {
const url = 'https://de.openlegaldata.io/api/laws/search/?format=json'
return axios.get(`https://cors-anywhere.herokuapp.com/${url}&text=${encodeURIComponent(search)}`, {
headers: {
Authorization: apiKey
}
})
}

View File

@ -1,25 +0,0 @@
var OldpApi = require('oldp-api');
export const testlaw = OldpApi.LawSearch("1");
var defaultClient = OldpApi.ApiClient.instance;
// Configure API key authorization: api_key
var api_key = defaultClient.authentications['api_key'];
api_key.apiKey = "6e6a95bf6f99b1430ce89239aad0fd41e6159583"
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//api_key.apiKeyPrefix['Authorization'] = "Token"
var api = new OldpApi.AnnotationLabelsApi()
var data = new OldpApi.AnnotationLabel(); // {AnnotationLabel}
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
api.annotationLabelsCreate(data, callback);

182
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.7.2",
"cheerio": "^1.0.0-rc.12",
"oldp-api": "^0.1.0"
},
"devDependencies": {
@ -879,6 +880,11 @@
"dev": true,
"peer": true
},
"node_modules/boolbase": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
},
"node_modules/brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -959,6 +965,42 @@
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/cheerio": {
"version": "1.0.0-rc.12",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.12.tgz",
"integrity": "sha512-VqR8m68vM46BNnuZ5NtnGBKIE/DfN0cRIzg9n40EIq9NOv90ayxLBXA8fXC5gquFRGJSTRqBq25Jt2ECLR431Q==",
"dependencies": {
"cheerio-select": "^2.1.0",
"dom-serializer": "^2.0.0",
"domhandler": "^5.0.3",
"domutils": "^3.0.1",
"htmlparser2": "^8.0.1",
"parse5": "^7.0.0",
"parse5-htmlparser2-tree-adapter": "^7.0.0"
},
"engines": {
"node": ">= 6"
},
"funding": {
"url": "https://github.com/cheeriojs/cheerio?sponsor=1"
}
},
"node_modules/cheerio-select": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/cheerio-select/-/cheerio-select-2.1.0.tgz",
"integrity": "sha512-9v9kG0LvzrlcungtnJtpGNxY+fzECQKhK4EGJX2vByejiMX84MFNQw4UxPJl3bFbTMw+Dfs37XaIkCwTZfLh4g==",
"dependencies": {
"boolbase": "^1.0.0",
"css-select": "^5.1.0",
"css-what": "^6.1.0",
"domelementtype": "^2.3.0",
"domhandler": "^5.0.3",
"domutils": "^3.0.1"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@ -1030,6 +1072,32 @@
"node": ">= 8"
}
},
"node_modules/css-select": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-5.1.0.tgz",
"integrity": "sha512-nwoRF1rvRRnnCqqY7updORDsuqKzqYJ28+oSMaJMMgOauh3fvwHqMS7EZpIPqK8GL+g9mKxF1vP/ZjSeNjEVHg==",
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^6.1.0",
"domhandler": "^5.0.2",
"domutils": "^3.0.1",
"nth-check": "^2.0.1"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/css-what": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz",
"integrity": "sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==",
"engines": {
"node": ">= 6"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@ -1103,6 +1171,68 @@
"node": ">=6.0.0"
}
},
"node_modules/dom-serializer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
"integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==",
"dependencies": {
"domelementtype": "^2.3.0",
"domhandler": "^5.0.2",
"entities": "^4.2.0"
},
"funding": {
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
}
},
"node_modules/domelementtype": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz",
"integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fb55"
}
]
},
"node_modules/domhandler": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz",
"integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==",
"dependencies": {
"domelementtype": "^2.3.0"
},
"engines": {
"node": ">= 4"
},
"funding": {
"url": "https://github.com/fb55/domhandler?sponsor=1"
}
},
"node_modules/domutils": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-3.1.0.tgz",
"integrity": "sha512-H78uMmQtI2AhgDJjWeQmHwJJ2bLPD3GMmO7Zja/ZZh84wkm+4ut+IUnUdRa8uCGX88DiVx1j6FRe1XfxEgjEZA==",
"dependencies": {
"dom-serializer": "^2.0.0",
"domelementtype": "^2.3.0",
"domhandler": "^5.0.3"
},
"funding": {
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
"node_modules/entities": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
"engines": {
"node": ">=0.12"
},
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/es-define-property": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz",
@ -1737,6 +1867,24 @@
"node": ">= 0.4"
}
},
"node_modules/htmlparser2": {
"version": "8.0.2",
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-8.0.2.tgz",
"integrity": "sha512-GYdjWKDkbRLkZ5geuHs5NY1puJ+PXwP7+fHPRz06Eirsb9ugf6d8kkXav6ADhcODhFFPMIXyxkxSuMf3D6NCFA==",
"funding": [
"https://github.com/fb55/htmlparser2?sponsor=1",
{
"type": "github",
"url": "https://github.com/sponsors/fb55"
}
],
"dependencies": {
"domelementtype": "^2.3.0",
"domhandler": "^5.0.3",
"domutils": "^3.0.1",
"entities": "^4.4.0"
}
},
"node_modules/ignore": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.0.tgz",
@ -2028,6 +2176,17 @@
"dev": true,
"peer": true
},
"node_modules/nth-check": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.1.1.tgz",
"integrity": "sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==",
"dependencies": {
"boolbase": "^1.0.0"
},
"funding": {
"url": "https://github.com/fb55/nth-check?sponsor=1"
}
},
"node_modules/object-inspect": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.1.tgz",
@ -2131,6 +2290,29 @@
"node": ">=6"
}
},
"node_modules/parse5": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-7.1.2.tgz",
"integrity": "sha512-Czj1WaSVpaoj0wbhMzLmWD69anp2WH7FXMB9n1Sy8/ZFF9jolSQVMu1Ij5WIyGmcBmhk7EOndpO4mIpihVqAXw==",
"dependencies": {
"entities": "^4.4.0"
},
"funding": {
"url": "https://github.com/inikulin/parse5?sponsor=1"
}
},
"node_modules/parse5-htmlparser2-tree-adapter": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/parse5-htmlparser2-tree-adapter/-/parse5-htmlparser2-tree-adapter-7.0.0.tgz",
"integrity": "sha512-B77tOZrqqfUfnVcOrUvfdLbz4pu4RopLD/4vmu3HUPswwTA8OH0EMW9BlWR2B0RCoiZRAHEUu7IxeP1Pd1UU+g==",
"dependencies": {
"domhandler": "^5.0.2",
"parse5": "^7.0.0"
},
"funding": {
"url": "https://github.com/inikulin/parse5?sponsor=1"
}
},
"node_modules/path-exists": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",

View File

@ -2,7 +2,7 @@
"name": "obsidian-sample-plugin",
"version": "1.0.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"main": "src/main",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
@ -23,6 +23,7 @@
},
"dependencies": {
"axios": "^1.7.2",
"cheerio": "^1.0.0-rc.12",
"oldp-api": "^0.1.0"
}
}

53
src/api/opld.ts Normal file
View File

@ -0,0 +1,53 @@
import axios from "axios";
import * as cheerio from 'cheerio';
export interface OldpSearchResponseItem {
link: string;
title: string;
snippet: string;
}
export class OldpApi {
private readonly corsProxyUrl = 'https://cors-anywhere.herokuapp.com'
private readonly baseUrl = 'https://de.openlegaldata.io';
constructor() {}
private async getRawSearchResults(searchTerm: string): Promise<string | null> {
try {
const url = `${this.baseUrl}/search/?selected_facets=facet_model_name_exact%3ALaw&q=${encodeURIComponent(searchTerm)}`
const response = await axios.get<string>(`${this.corsProxyUrl}/${url}`);
const html = response.data;
return html;
} catch (error) {
console.error('Error fetching search results:', error);
return null;
}
}
private parseSearchResults(data: string): OldpSearchResponseItem[] {
const webDocument = cheerio.load(data);
const searchItems: OldpSearchResponseItem[] = [];
webDocument('.search-items li').each((index, element) => {
const link = `${this.baseUrl}${webDocument(element).find('a').attr('href')}`;
const title = (webDocument(element).find('h4').text()).replace('(Law)', '');
const snippet = webDocument(element).find('.search-snippet').text();
searchItems.push({
link,
title,
snippet
});
});
return searchItems;
}
public async search(searchTerm: string): Promise<OldpSearchResponseItem[]> {
const rawSearchResult = await this.getRawSearchResults(searchTerm);
if (rawSearchResult === null) return [];
return this.parseSearchResults(rawSearchResult);
}
}

68
src/lawSuggester.ts Normal file
View File

@ -0,0 +1,68 @@
import LawRefPlugin from "./main";
import { Editor, EditorPosition, EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, TFile } from "obsidian";
import { OldpApi, OldpSearchResponseItem } from "./api/opld";
export default class LawSuggester extends EditorSuggest<OldpSearchResponseItem> {
private readonly oldpApi = new OldpApi();
plugin: LawRefPlugin;
// todo: probably refactor this?
queryRegex = new RegExp(/(?:(?<=^|\n)|(?<=\s))§[-\w]+(?=\s|$)/gi);
constructor(plugin: LawRefPlugin) {
super(plugin.app);
this.plugin = plugin;
}
onTrigger(cursor: EditorPosition, editor: Editor, _: TFile): EditorSuggestTriggerInfo | null {
// todo: probably refactor selection
const sub = editor.getLine(cursor.line).slice(0, cursor.ch);
const matches = sub.match(this.queryRegex);
if (matches === null || matches.groups === null || matches.groups?.sc === null) return null;
if (matches != null) console.log('matches', matches);
return {
end: cursor,
start: {
ch: sub.lastIndexOf(matches.groups?.sc ?? ''),
line: cursor.line,
},
// always get latest
query: matches[matches.length - 1],
}
}
async getSuggestions(context: EditorSuggestContext): Promise<OldpSearchResponseItem[]> {
// TODO: fix bug where when you change something in the string at e.g. char 2 it only triggers rerender for substring
console.log('query', context.query?.replace('§', '').replace('-', ' ').replace('_', ' '))
const query = context.query?.replace('§', '').replace('-', ' ').replace('_', ' ');
// don't execute if input length is under 2
if(query?.length < 2) return [];
return await this.oldpApi.search(query);
}
renderSuggestion(suggestion: OldpSearchResponseItem, el: HTMLElement): void {
// TODO refactor appearence and hover
const outer = el.createDiv({ cls: "lawRef-suggestion-container" });
let shortcodeDiv = createDiv({ title: `${suggestion.snippet}` })
shortcodeDiv.setText(suggestion.title);
outer.appendChild(shortcodeDiv)
}
selectSuggestion(suggestion: OldpSearchResponseItem, evt: MouseEvent | KeyboardEvent): void {
// TODO: implement me
/* if(!this.context) return;
const { start, end } = this.context;
const shortcode = suggestion.names.includes(suggestion.matchedName) ? suggestion.matchedName : suggestion.names[0]
const outEm = suggestion.names.some(n => windowsSupportedFirstChar.includes(n)) && suggestion.emoji.split("").length > 1
? suggestion.emoji.split("")[0]
: suggestion.emoji;
const repl = this.plugin.settings.immediateReplace ? outEm : `:${shortcode}: `;
this.context.editor.replaceRange(repl, start, end);
this.plugin.updateHistory(suggestion.matchedName); */
}
}

View File

@ -1,6 +1,7 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace, WorkspaceLeaf, parseFrontMatterEntry } from 'obsidian';
import { VIEW_TYPE_EXAMPLE, ExampleView } from 'law-view';
import { getData } from 'openlegaldataparser';
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault, Workspace, WorkspaceLeaf, MarkdownPostProcessorContext } from 'obsidian';
import { VIEW_TYPE_EXAMPLE } from './law-view';
import { OldpApi } from './api/opld';
import LawSuggester from './lawSuggester';
// Remember to rename these classes and interfaces!
@ -12,68 +13,13 @@ const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}
export default class MyPlugin extends Plugin {
export default class LawRefPlugin extends Plugin {
settings: MyPluginSettings;
private readonly OldpApi = new OldpApi();
async onload() {
await this.loadSettings();
//register law review view
this.registerView(
VIEW_TYPE_EXAMPLE,
(leaf) => new ExampleView(leaf)
);
// This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('This is a not notice!');
this.activateView();
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new SampleModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'open-sample-modal-complex',
name: 'Open sample modal (complex)',
checkCallback: (checking: boolean) => {
// Conditions to check
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation.
if (!checking) {
new SampleModal(this.app).open();
}
// This command will only show up in Command Palette when the check function returns true
return true;
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));
@ -85,6 +31,10 @@ export default class MyPlugin extends Plugin {
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
// register suggestor on § key
this.registerEditorSuggest(new LawSuggester(this))
}
onunload() {
@ -120,8 +70,9 @@ export default class MyPlugin extends Plugin {
if (!metadata) return console.log("no metadata");
let returner = metadata.frontmatter;
if (returner) {
const oldpApi = new OldpApi();
console.log(returner['§']);
console.log(await getData(returner['§'][0]));
console.log(await oldpApi.search(returner['§']));
}
}
@ -152,9 +103,9 @@ class SampleModal extends Modal {
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
plugin: LawRefPlugin;
constructor(app: App, plugin: MyPlugin) {
constructor(app: App, plugin: LawRefPlugin) {
super(app, plugin);
this.plugin = plugin;
}

View File

@ -6,3 +6,19 @@ available in the app when your plugin is enabled.
If your plugin does not need CSS, delete this file.
*/
.lawRef-suggestion-container {
display: flex;
place-content: space-between;
}
.lawRef-suggestion-entry {
border-top: solid var(--background-secondary) 1px;
padding-left: 10px;
}
.ES-suggestion-item {
border-top: solid var(--background-secondary) 1px;
padding-left: 10px;
}

View File

@ -1,12 +1,13 @@
{
"compilerOptions": {
"baseUrl": ".",
"baseUrl": "./",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "ES6",
"allowJs": true,
"noImplicitAny": true,
"outDir": "./dist",
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
@ -19,5 +20,6 @@
]
},
"include": [
"**/*.ts" ]
"**/*.ts"
]
}