mirror of
https://github.com/alberti42/obsidian-plugins-annotations.git
synced 2026-07-22 10:10:24 +00:00
Added parser
This commit is contained in:
parent
4573427914
commit
f49d18e0ff
7 changed files with 1510 additions and 13 deletions
88
package-lock.json
generated
88
package-lock.json
generated
|
|
@ -19,6 +19,7 @@
|
|||
"esbuild": "0.17.3",
|
||||
"esbuild-plugin-copy": "^2.1.1",
|
||||
"obsidian": "latest",
|
||||
"peggy": "^4.0.3",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
}
|
||||
|
|
@ -525,6 +526,35 @@
|
|||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/@peggyjs/from-mem": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/@peggyjs/from-mem/-/from-mem-1.3.0.tgz",
|
||||
"integrity": "sha512-kzGoIRJjkg3KuGI4bopz9UvF3KguzfxalHRDEIdqEZUe45xezsQ6cx30e0RKuxPUexojQRBfu89Okn7f4/QXsw==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"semver": "7.6.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/@peggyjs/from-mem/node_modules/semver": {
|
||||
"version": "7.6.0",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz",
|
||||
"integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"lru-cache": "^6.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"semver": "bin/semver.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/codemirror": {
|
||||
"version": "5.60.8",
|
||||
"resolved": "https://registry.npmjs.org/@types/codemirror/-/codemirror-5.60.8.tgz",
|
||||
|
|
@ -980,6 +1010,16 @@
|
|||
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/commander": {
|
||||
"version": "12.1.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz",
|
||||
"integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
|
|
@ -1795,6 +1835,19 @@
|
|||
"dev": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/lru-cache": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
|
||||
"integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"yallist": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/merge2": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
|
||||
|
|
@ -1992,6 +2045,24 @@
|
|||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/peggy": {
|
||||
"version": "4.0.3",
|
||||
"resolved": "https://registry.npmjs.org/peggy/-/peggy-4.0.3.tgz",
|
||||
"integrity": "sha512-v7/Pt6kGYsfXsCrfb52q7/yg5jaAwiVaUMAPLPvy4DJJU6Wwr72t6nDIqIDkGfzd1B4zeVuTnQT0RGeOhe/uSA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@peggyjs/from-mem": "1.3.0",
|
||||
"commander": "^12.1.0",
|
||||
"source-map-generator": "0.8.0"
|
||||
},
|
||||
"bin": {
|
||||
"peggy": "bin/peggy.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/picomatch": {
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
|
||||
|
|
@ -2172,6 +2243,16 @@
|
|||
"node": ">=8"
|
||||
}
|
||||
},
|
||||
"node_modules/source-map-generator": {
|
||||
"version": "0.8.0",
|
||||
"resolved": "https://registry.npmjs.org/source-map-generator/-/source-map-generator-0.8.0.tgz",
|
||||
"integrity": "sha512-psgxdGMwl5MZM9S3FWee4EgsEaIjahYV5AzGnwUvPhWeITz/j6rKpysQHlQ4USdxvINlb8lKfWGIXwfkrgtqkA==",
|
||||
"dev": true,
|
||||
"license": "BSD-3-Clause",
|
||||
"engines": {
|
||||
"node": ">= 10"
|
||||
}
|
||||
},
|
||||
"node_modules/strip-ansi": {
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
|
||||
|
|
@ -2361,6 +2442,13 @@
|
|||
"dev": true,
|
||||
"peer": true
|
||||
},
|
||||
"node_modules/yallist": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
|
||||
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
|
||||
"dev": true,
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/yocto-queue": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json",
|
||||
"grammar": "peggy --format es src/grammar.pegjs -o src/peggy.mjs"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
|
@ -19,6 +20,7 @@
|
|||
"esbuild": "0.17.3",
|
||||
"esbuild-plugin-copy": "^2.1.1",
|
||||
"obsidian": "latest",
|
||||
"peggy": "^4.0.3",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
|
|
|
|||
51
src/grammar.pegjs
Normal file
51
src/grammar.pegjs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
main
|
||||
= blocks:block* { return blocks; }
|
||||
|
||||
block
|
||||
= annotation_block1 / annotation_block / loose_line
|
||||
|
||||
annotation_block1
|
||||
= name:plugin_name id:id_field type:type_field? begin_cmd anno:annotation_text end_cmd { return {
|
||||
id: id,
|
||||
name: name,
|
||||
anno: anno,
|
||||
type: type ? type : "markdown",
|
||||
} }
|
||||
|
||||
annotation_block
|
||||
= plugin_name newline+ id_field b:begin_cmd annotation_text end_cmd { console.log(b); return b; }
|
||||
|
||||
plugin_name
|
||||
= "#" _ @name:$not_newline newline+
|
||||
|
||||
id_field
|
||||
= "<!--" _* "id:" _ @$(!"-->" !_ .)+ _* "-->" newline+
|
||||
|
||||
type_field
|
||||
= "<!--" _* "type:" _* @valid_types _* "-->" newline+
|
||||
|
||||
valid_types
|
||||
= $("markdown"i / "html"i / "text"i) { return text().toLowerCase(); }
|
||||
|
||||
begin_cmd
|
||||
= $("<!--" _* "BEGIN ANNOTATION" _* "-->" newline+)
|
||||
|
||||
end_cmd
|
||||
= $("<!--" _* "END ANNOTATION" _* "-->" newline+)
|
||||
|
||||
annotation_text
|
||||
= $(!end_cmd .)*
|
||||
|
||||
loose_line
|
||||
= $(([^\n\r]* newline) / [^\n\r]+)
|
||||
|
||||
not_newline
|
||||
= [^\n\r]+
|
||||
|
||||
newline
|
||||
= [\n\r]
|
||||
|
||||
empty_line
|
||||
= $(_ [\n\r])
|
||||
|
||||
_ = [ \f\t\v\u0020\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
|
||||
22
src/main.ts
22
src/main.ts
|
|
@ -9,7 +9,8 @@ import {
|
|||
Plugins,
|
||||
PluginManifest,
|
||||
FileSystemAdapter,
|
||||
// PluginSettingTab,
|
||||
TAbstractFile,
|
||||
// PluginSettingTab,
|
||||
// App,
|
||||
} from 'obsidian';
|
||||
import { around } from 'monkey-around';
|
||||
|
|
@ -17,6 +18,7 @@ import { PluginAnnotationDict, PluginsAnnotationsSettingsWithoutNames, isPluginA
|
|||
import { DEFAULT_SETTINGS, DEFAULT_SETTINGS_WITHOUT_NAMES } from './defaults';
|
||||
import { PluginsAnnotationsSettingTab } from 'settings_tab'
|
||||
import * as path from 'path';
|
||||
import { readAnnotationsFromFile, writeAnnotationsToFile } from 'manageAnnotations';
|
||||
|
||||
export default class PluginsAnnotations extends Plugin {
|
||||
settings: PluginsAnnotationsSettings = {...DEFAULT_SETTINGS};
|
||||
|
|
@ -45,6 +47,12 @@ export default class PluginsAnnotations extends Plugin {
|
|||
this.observeTab(activeTab);
|
||||
}
|
||||
});
|
||||
|
||||
this.app.vault.on('modify', (modifiedFile: TAbstractFile) => {
|
||||
if (modifiedFile.path === this.settings.markdown_file_path) {
|
||||
readAnnotationsFromFile(this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async loadSettings(): Promise<void> {
|
||||
|
|
@ -87,6 +95,16 @@ export default class PluginsAnnotations extends Plugin {
|
|||
|
||||
// Merge loaded settings with default settings
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, getSettingsFromData(await this.loadData()));
|
||||
|
||||
if(this.settings.markdown_file_path!=='') {
|
||||
const file = this.app.vault.getAbstractFileByPath(this.settings.markdown_file_path);
|
||||
if(file) {
|
||||
readAnnotationsFromFile(this);
|
||||
} else {
|
||||
// writeAnnotationsToFile(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Store the path to the vault
|
||||
|
|
@ -567,7 +585,7 @@ export default class PluginsAnnotations extends Plugin {
|
|||
// there could be changes in the settings due to synchronization among devices
|
||||
// which only happens after the plugin is loaded
|
||||
await this.loadSettings();
|
||||
|
||||
|
||||
const pluginsContainer = tab.containerEl.querySelector('.installed-plugins-container');
|
||||
if (!pluginsContainer) return;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
// manageAnnotations.ts
|
||||
|
||||
import PluginsAnnotations from "main";
|
||||
import { Platform, TFile } from "obsidian";
|
||||
import { normalizePath, Platform, TFile } from "obsidian";
|
||||
import { joinPaths, makePosixPathOScompatible, parseFilePath, showConfirmationDialog } from "utils";
|
||||
import { parse, SyntaxError } from "./peggy.mjs";
|
||||
import { PluginAnnotation, PluginAnnotationDict } from "types";
|
||||
|
||||
export async function handleMarkdownFilePathChange(plugin: PluginsAnnotations, filepath: string): Promise<void> {
|
||||
const parsed_filepath = parseFilePath(filepath);
|
||||
|
|
@ -64,22 +66,55 @@ export async function handleMarkdownFilePathChange(plugin: PluginsAnnotations, f
|
|||
|
||||
plugin.settings.markdown_file_path = filepath;
|
||||
plugin.saveSettings(plugin.settings);
|
||||
writeAnnotationsToFile(plugin, filepath);
|
||||
writeAnnotationsToFile(plugin);
|
||||
}
|
||||
|
||||
export async function writeAnnotationsToFile(plugin: PluginsAnnotations, filePath: string) {
|
||||
export async function readAnnotationsFromFile(plugin: PluginsAnnotations): Promise<Record<string, { name: string; anno: string }>> {
|
||||
const filePath = plugin.settings.markdown_file_path;
|
||||
try {
|
||||
const file = plugin.app.vault.getAbstractFileByPath(filePath);
|
||||
if (!file) return {};
|
||||
|
||||
const content = await plugin.app.vault.read(file as TFile);
|
||||
|
||||
const content_parsed = parse(content);
|
||||
|
||||
const annotations: PluginAnnotationDict = {};
|
||||
|
||||
const dictionary = content_parsed.reduce((acc: PluginAnnotationDict, current: unknown) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const {_, ...theRest} = current;
|
||||
acc[current.id] = theRest;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
console.log(dictionary);
|
||||
|
||||
// content_parsed.map((item:PluginAnnotation) => {console.log(item)});
|
||||
|
||||
|
||||
return annotations;
|
||||
} catch (error) {
|
||||
console.error('Failed to read annotations from file:', error);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeAnnotationsToFile(plugin: PluginsAnnotations) {
|
||||
if(!plugin.pluginNameToIdMap) return;
|
||||
|
||||
const filePath = plugin.settings.markdown_file_path;
|
||||
|
||||
const annotations = plugin.settings.annotations;
|
||||
|
||||
try {
|
||||
let content = '';
|
||||
for (const pluginId in annotations) {
|
||||
|
||||
console.log(pluginId);
|
||||
console.log(annotations[pluginId].anno);
|
||||
console.log(annotations[pluginId].name);
|
||||
console.log('---');
|
||||
// console.log(pluginId);
|
||||
// console.log(annotations[pluginId].anno);
|
||||
// console.log(annotations[pluginId].name);
|
||||
// console.log('---');
|
||||
content += `# ${annotations[pluginId].name}\n\n<!-- id: ${pluginId} -->\n<!-- BEGIN ANNOTATION -->\n${annotations[pluginId].anno}\n<!-- END ANNOTATION -->\n`;
|
||||
}
|
||||
|
||||
|
|
|
|||
1302
src/peggy.mjs
Normal file
1302
src/peggy.mjs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -5,6 +5,7 @@ import { DEFAULT_SETTINGS, DEFAULT_SETTINGS_WITHOUT_NAMES } from './defaults';
|
|||
export interface PluginAnnotation {
|
||||
name: string; // extended name of the plugin
|
||||
anno: string; // personal annontation
|
||||
type: AnnotationType; // type of annotation
|
||||
}
|
||||
|
||||
export interface PluginAnnotationDict {
|
||||
|
|
@ -34,9 +35,9 @@ export function isPluginsAnnotationsSettings(s:unknown): s is PluginsAnnotations
|
|||
}
|
||||
|
||||
export enum AnnotationType {
|
||||
text,
|
||||
html,
|
||||
markdown,
|
||||
text = 'text',
|
||||
html = 'html',
|
||||
markdown = 'markdown',
|
||||
}
|
||||
|
||||
// For backward compatibility only with version 'FAA70013-38E9-4FDF-B06A-F899F6487C19'
|
||||
|
|
|
|||
Loading…
Reference in a new issue