@@ -15,32 +15,51 @@ import {
15
15
BaseRantProcessor ,
16
16
} from "./processor" ;
17
17
import { randomSeed } from "./utils" ;
18
+ import SettingTab from "./settings" ;
19
+
20
+ interface RantLangSettings {
21
+ enableStyling : boolean ;
22
+ }
23
+
24
+ const DEFAULT_SETTINGS : RantLangSettings = {
25
+ enableStyling : true ,
26
+ } ;
18
27
19
28
export default class RantLangPlugin extends Plugin {
29
+ settings : RantLangSettings ;
20
30
fileMap : Map < TFile , BaseRantProcessor [ ] > = new Map ( ) ;
21
31
22
32
async onload ( ) {
23
33
// Load WebAssembly Rust plugin to have access to the Rust Rant crate.
24
34
const buffer = Uint8Array . from ( atob ( rustPlugin ) , ( c ) => c . charCodeAt ( 0 ) ) ;
25
35
await init ( Promise . resolve ( buffer ) ) ;
26
36
37
+ // Settings initialization
38
+ this . settings = Object . assign ( DEFAULT_SETTINGS , await this . loadData ( ) ) ;
39
+ this . addSettingTab ( new SettingTab ( this . app , this ) ) ;
40
+
27
41
// Register Rant codeblocks.
28
42
this . registerMarkdownCodeBlockProcessor (
29
43
"rant" ,
30
- ( source : string , el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
44
+ async (
45
+ source : string ,
46
+ el : HTMLElement ,
47
+ ctx : MarkdownPostProcessorContext
48
+ ) => {
31
49
const file = this . app . vault . getAbstractFileByPath ( ctx . sourcePath ) ;
32
50
if ( ! file || ! ( file instanceof TFile ) ) return ;
33
51
52
+ const enableStyling = this . settings . enableStyling ;
34
53
const processor = new CodeblockRantProcessor ( source , el ) ;
35
- processor . rant ( randomSeed ( ) ) ;
54
+ processor . rant ( randomSeed ( ) , enableStyling ) ;
36
55
37
- this . registerRantProcessorForRerant ( processor , file ) ;
56
+ await this . registerRantProcessorForRerant ( processor , file ) ;
38
57
}
39
58
) ;
40
59
41
60
// Register inline Rant blocks.
42
61
this . registerMarkdownPostProcessor (
43
- ( el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
62
+ async ( el : HTMLElement , ctx : MarkdownPostProcessorContext ) => {
44
63
const file = this . app . vault . getAbstractFileByPath ( ctx . sourcePath ) ;
45
64
if ( ! file || ! ( file instanceof TFile ) ) return ;
46
65
@@ -52,11 +71,12 @@ export default class RantLangPlugin extends Plugin {
52
71
if ( text . startsWith ( inlineRantQueryPrefix ) ) {
53
72
const code = text . substring ( inlineRantQueryPrefix . length ) . trim ( ) ;
54
73
74
+ const enableStyling = this . settings . enableStyling ;
55
75
const processor = new InlineRantProcessor ( code , el , codeblock ) ;
56
76
ctx . addChild ( processor ) ;
57
- processor . rant ( randomSeed ( ) ) ;
77
+ processor . rant ( randomSeed ( ) , enableStyling ) ;
58
78
59
- this . registerRantProcessorForRerant ( processor , file ) ;
79
+ await this . registerRantProcessorForRerant ( processor , file ) ;
60
80
}
61
81
}
62
82
}
@@ -74,10 +94,11 @@ export default class RantLangPlugin extends Plugin {
74
94
this . fileMap . has ( view . file )
75
95
) {
76
96
if ( ! checking ) {
97
+ const enableStyling = this . settings . enableStyling ;
77
98
const processors = this . fileMap . get ( view . file ) ;
78
99
79
100
processors . forEach ( ( processor ) => {
80
- processor . rant ( randomSeed ( ) ) ;
101
+ processor . rant ( randomSeed ( ) , enableStyling ) ;
81
102
} ) ;
82
103
83
104
new Notice ( "Re-processed Rant blocks" ) ;
@@ -88,7 +109,15 @@ export default class RantLangPlugin extends Plugin {
88
109
} ) ;
89
110
}
90
111
91
- registerRantProcessorForRerant ( processor : BaseRantProcessor , file : TFile ) {
112
+ async updateSettings ( settings : Partial < RantLangSettings > ) {
113
+ Object . assign ( this . settings , settings ) ;
114
+ await this . saveData ( this . settings ) ;
115
+ }
116
+
117
+ async registerRantProcessorForRerant (
118
+ processor : BaseRantProcessor ,
119
+ file : TFile
120
+ ) {
92
121
// File-based tracking of registered processors inspired by javalent's excellent dice roller plugin: https://github.com/valentine195/obsidian-dice-roller
93
122
94
123
if ( ! this . fileMap . has ( file ) ) {
0 commit comments