generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
annotationParse.ts
61 lines (59 loc) · 1.98 KB
/
annotationParse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { CardSchedule } from "schedule"
import yaml from "yaml"
export class AnnotationWrapper {
static defaultRegAnnotation = String.raw`%%[^\%\^]+?%%\n\^blockID`
static defaultRegWrapper = /%%\n\`\`\`(YAML|AOSRDATA)\n([\s\S]+?)\`\`\`\n%%\n\^.+/gm
static findAnnotationWrapper(fileText:string, blockID:string) {
let regAnnotation = this.defaultRegAnnotation.replace("blockID", blockID)
let matchReg = new RegExp(regAnnotation, "gm")
let annotation = ""
fileText.match(matchReg)?.forEach((value) => {
annotation = value
})
return annotation
}
static deWrapper(annotation:string):string {
let results = annotation.matchAll(AnnotationWrapper.defaultRegWrapper)
for (let result of results) {
return result[2]
}
return ""
}
static enWrapper(ID: string, annotation: string, format: string): string {
if (format === "YAML") {
return "%%\n\`\`\`YAML\n" + annotation + "\`\`\`\n%%\n^" + ID
} else if (format === "AOSRDATA") {
return "%%\n\`\`\`AOSRDATA\n" + annotation + "\`\`\`\n%%\n^" + ID
} else {
throw new Error("Invalid format: " + format)
}
}
}
export class AnnotationObject {
copy(obj: AnnotationObject) {
this.cardSchedule.copy(obj.cardSchedule)
}
cardSchedule:CardSchedule
constructor() {
this.cardSchedule = new CardSchedule()
}
static Parse(annotation:string):AnnotationObject {
if (!annotation) {
return new AnnotationObject()
}
let ret = new AnnotationObject()
try {
let obj:AnnotationObject = yaml.parse(annotation)
ret.copy(obj)
} catch (error) {
console.log(error)
}
return ret
}
static Stringify(fmt:AnnotationObject):string {
let s = yaml.stringify(fmt, (key, value)=> {
if (value !== null) return value
})
return s
}
}