Skip to content

Commit 3f625b7

Browse files
committed
feat: Allow importing rant codeblocks via BlockLink into other rant codeblocks using 'import: [[File#^block-id]]' statements at the top of the program
1 parent 082bf36 commit 3f625b7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/processor.ts

+41
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,47 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
3939
}
4040

4141
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+
4283
renderResult() {
4384
this.container.empty();
4485
const content = this.container.createDiv({ cls: this.getStyles() });

0 commit comments

Comments
 (0)