@@ -39,6 +39,47 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
39
39
}
40
40
41
41
export class CodeblockRantProcessor extends BaseRantProcessor {
42
+ rant ( input ?: string , seed ?: number ) {
43
+ this . resolveImports ( input ?? this . input ) . then ( ( program ) => {
44
+ this . processInput ( program , seed ?? randomSeed ( ) ) ;
45
+ this . renderResult ( ) ;
46
+ } ) ;
47
+ }
48
+
49
+ async resolveImports ( input : string ) : Promise < string > {
50
+ let program = "" ;
51
+ let lines = input . split ( "\n" ) ;
52
+ while ( lines . length > 0 && lines [ 0 ] . startsWith ( "import:" ) ) {
53
+ const blockLink = lines . shift ( ) . split ( "import:" ) [ 1 ] . trim ( ) ;
54
+
55
+ const { groups } = blockLink . match ( BLOCK_LINK_REGEX ) ;
56
+ const path = groups . link . replace ( / ( \[ | \] ) / g, "" ) ;
57
+ const block = groups . block . replace ( / ( \^ | # ) / g, "" ) . trim ( ) ;
58
+
59
+ const file = this . plugin . app . metadataCache . getFirstLinkpathDest (
60
+ path ,
61
+ this . sourcePath
62
+ ) ;
63
+
64
+ if ( ! file || ! ( file instanceof TFile ) ) {
65
+ throw new Error ( "Could not load file." ) ;
66
+ }
67
+
68
+ const cache = this . plugin . app . metadataCache . getFileCache ( file ) ;
69
+ const position = cache . blocks [ block ] . position ;
70
+
71
+ const content = await this . plugin . app . vault . cachedRead ( file ) ;
72
+ const rantProgram = content
73
+ . split ( "\n" )
74
+ . slice ( position . start . line + 1 , position . end . line )
75
+ . join ( "\n" ) ;
76
+
77
+ program += await this . resolveImports ( rantProgram ) ;
78
+ }
79
+
80
+ return program + "\n" + lines . join ( "\n" ) ;
81
+ }
82
+
42
83
renderResult ( ) {
43
84
this . container . empty ( ) ;
44
85
const content = this . container . createDiv ( { cls : this . getStyles ( ) } ) ;
0 commit comments