generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
deck.tsx
114 lines (104 loc) · 3.21 KB
/
deck.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Pattern } from "Pattern";
import { findOutline } from "card";
import { Engine, RuleProperties } from "json-rules-engine";
import { FrontMatterCache, MarkdownPostProcessorContext, MarkdownRenderChild, getAllTags, parseFrontMatterTags } from "obsidian";
import React from "react";
import { Root, createRoot } from "react-dom/client";
import { App } from "view";
export interface DeckConfig {
rule: RuleProperties
}
interface FactPattern {
card: {
path: string
tags: string[]
text: string
outline: string // outline heading string
}
file: {
tags: string[] // from frontmatter
}
}
function convertToFact(p: Pattern): FactPattern {
let tags = parseFrontMatterTags(p.card.fileCache?.frontmatter) || []
let outline = findOutline(p.card.fileCache, p.card.indexBuff)
return {
card: {
path: p.card.note.path,
tags: p.card.tags,
text: p.card.cardText,
outline: outline
},
file: {
tags: tags
}
}
}
export async function ParseRule(rule: RuleProperties, allPattern: Pattern[]) {
let results: Pattern[] = []
let engine = new Engine()
try {
engine.addRule(rule)
engine.addOperator('regexMatch', (factValue, regex) => {
if (typeof regex !== "string") return false;
if (typeof factValue === "string") {
const regexPattern = new RegExp(regex);
return regexPattern.test(factValue);
}
if (Array.isArray(factValue)) {
const regexPattern = new RegExp(regex);
return factValue.some(item => {
return typeof item === "string" && regexPattern.test(item);
});
}
return false;
})
await Promise.all(allPattern.map(async (pattern, index) => {
let fact = convertToFact(pattern)
try {
return engine.run(fact).then((events) => {
if (events.events.length > 0) {
results.push(pattern);
}
})
} catch (error) {
console.log(error)
}
}))
} catch (error) {
console.log(error)
}
return results
}
export function handlerDeckCode(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) {
let config: DeckConfig
try {
config = JSON.parse(source) as DeckConfig
let testEngine = new Engine()
testEngine.addRule(config.rule)
ctx.addChild(new DeckView(el, config))
} catch (error) {
console.log(error)
el.createSpan(
{ "text": error }
)
}
}
export class DeckView extends MarkdownRenderChild {
root: Root
rule: RuleProperties
constructor(containerEl: HTMLElement, rule: DeckConfig) {
super(containerEl)
this.rule = rule.rule
}
onload(): void {
let rootDiv = this.containerEl.createDiv()
this.root = createRoot(rootDiv);
this.root.render(
<App view={this} rule={this.rule}></App>
)
}
onunload(): void {
this.root.unmount()
}
}