1
- import { MarkdownRenderChild , MarkdownRenderer } from "obsidian" ;
1
+ import { MarkdownRenderChild , MarkdownRenderer , TFile } from "obsidian" ;
2
2
import { rant } from "../pkg/obsidian_rantlang_plugin.js" ;
3
- import { RantLangSettings } from "./settings.js" ;
4
- import { randomSeed } from "./utils.js" ;
3
+ import type RantLangPlugin from "./main" ;
4
+ import { RantLangSettings } from "./settings" ;
5
+ import { randomSeed } from "./utils" ;
6
+
7
+ export const BLOCK_LINK_REGEX =
8
+ / \[ \[ (?< link > [ \s \S ] + ?) # ? \^ (?< block > [ \s \S ] + ?) \] \] / ;
5
9
6
10
export abstract class BaseRantProcessor extends MarkdownRenderChild {
7
11
result : string = "" ;
8
12
9
13
constructor (
14
+ public plugin : RantLangPlugin ,
10
15
public input : string ,
11
16
public container : HTMLElement ,
12
17
public settings : RantLangSettings ,
13
- public sourcePath : string ,
14
- public customizations : Customization [ ] = [ ]
18
+ public sourcePath : string
15
19
) {
16
20
super ( container ) ;
17
21
this . rant ( ) ;
18
22
}
19
23
20
24
abstract renderResult ( ) : void ;
21
25
22
- processInput ( seed : number ) {
26
+ processInput ( input : string , seed : number ) {
23
27
try {
24
- this . result = rant ( this . input , seed ) ;
28
+ this . result = rant ( input , seed ) ;
25
29
} catch ( error ) {
26
30
this . result = "ERROR processing Rant block (see console for details)" ;
27
31
console . error ( error ) ;
28
32
}
29
33
}
30
34
31
- rant ( seed ?: number ) {
32
- this . processInput ( seed ?? randomSeed ( ) ) ;
35
+ rant ( input ?: string , seed ?: number ) {
36
+ this . processInput ( input ?? this . input , seed ?? randomSeed ( ) ) ;
33
37
this . renderResult ( ) ;
34
38
}
35
39
}
36
40
37
- export type Customization = "bold" | "italic" ;
38
-
39
41
export class CodeblockRantProcessor extends BaseRantProcessor {
40
42
renderResult ( ) {
41
43
this . container . empty ( ) ;
@@ -53,9 +55,6 @@ export class CodeblockRantProcessor extends BaseRantProcessor {
53
55
if ( this . settings . highlight ) {
54
56
cls . push ( "rant-highlight" ) ;
55
57
}
56
- this . customizations . forEach ( ( style ) => {
57
- cls . push ( `rant-${ style } ` ) ;
58
- } ) ;
59
58
return cls ;
60
59
}
61
60
}
@@ -83,3 +82,76 @@ export class InlineRantProcessor extends BaseRantProcessor {
83
82
return cls ;
84
83
}
85
84
}
85
+
86
+ export class BlockLinkRantProcessor extends BaseRantProcessor {
87
+ loaded : boolean = false ;
88
+ path : string ;
89
+ block : string ;
90
+
91
+ constructor (
92
+ plugin : RantLangPlugin ,
93
+ input : string ,
94
+ container : HTMLElement ,
95
+ settings : RantLangSettings ,
96
+ sourcePath : string
97
+ ) {
98
+ super ( plugin , input , container , settings , sourcePath ) ;
99
+ this . parseBlockLink ( input ) ;
100
+ this . rant ( ) ;
101
+ }
102
+
103
+ parseBlockLink ( blockLink : string ) {
104
+ const { groups } = blockLink . match ( BLOCK_LINK_REGEX ) ;
105
+ this . path = groups . link . replace ( / ( \[ | \] ) / g, "" ) ;
106
+ this . block = groups . block . replace ( / ( \^ | # ) / g, "" ) . trim ( ) ;
107
+ this . loaded = true ;
108
+ }
109
+
110
+ rant ( ) {
111
+ if ( ! this . loaded ) {
112
+ return ;
113
+ }
114
+
115
+ const file = this . plugin . app . metadataCache . getFirstLinkpathDest (
116
+ this . path ,
117
+ this . sourcePath
118
+ ) ;
119
+
120
+ if ( ! file || ! ( file instanceof TFile ) ) {
121
+ throw new Error ( "Could not load file." ) ;
122
+ }
123
+
124
+ const cache = this . plugin . app . metadataCache . getFileCache ( file ) ;
125
+ const position = cache . blocks [ this . block ] . position ;
126
+
127
+ this . plugin . app . vault . cachedRead ( file ) . then ( ( content ) => {
128
+ const rantProgram = content
129
+ . split ( "\n" )
130
+ . slice ( position . start . line + 1 , position . end . line )
131
+ . join ( "\n" ) ;
132
+ super . rant ( rantProgram ) ;
133
+ } ) ;
134
+ }
135
+
136
+ renderResult ( ) {
137
+ let temp = createEl ( "div" ) ;
138
+ MarkdownRenderer . renderMarkdown ( this . result , temp , this . sourcePath , this ) ;
139
+
140
+ this . container . empty ( ) ;
141
+ this . container . className = this . getStyles ( ) . join ( " " ) ;
142
+
143
+ temp . childNodes . forEach ( ( paragraph ) => {
144
+ paragraph . childNodes . forEach ( ( node ) => {
145
+ this . container . appendChild ( node . cloneNode ( true ) ) ;
146
+ } ) ;
147
+ } ) ;
148
+ }
149
+
150
+ getStyles ( ) {
151
+ let cls = [ "rant-inline" ] ;
152
+ if ( this . settings . highlight ) {
153
+ cls . push ( "rant-highlight" ) ;
154
+ }
155
+ return cls ;
156
+ }
157
+ }
0 commit comments