Skip to content

Commit 164bf68

Browse files
committed
feat: add setting to disable/enable rant block styling
1 parent 17c5d9b commit 164bf68

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

src/main.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,51 @@ import {
1515
BaseRantProcessor,
1616
} from "./processor";
1717
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+
};
1827

1928
export default class RantLangPlugin extends Plugin {
29+
settings: RantLangSettings;
2030
fileMap: Map<TFile, BaseRantProcessor[]> = new Map();
2131

2232
async onload() {
2333
// Load WebAssembly Rust plugin to have access to the Rust Rant crate.
2434
const buffer = Uint8Array.from(atob(rustPlugin), (c) => c.charCodeAt(0));
2535
await init(Promise.resolve(buffer));
2636

37+
// Settings initialization
38+
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
39+
this.addSettingTab(new SettingTab(this.app, this));
40+
2741
// Register Rant codeblocks.
2842
this.registerMarkdownCodeBlockProcessor(
2943
"rant",
30-
(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
44+
async (
45+
source: string,
46+
el: HTMLElement,
47+
ctx: MarkdownPostProcessorContext
48+
) => {
3149
const file = this.app.vault.getAbstractFileByPath(ctx.sourcePath);
3250
if (!file || !(file instanceof TFile)) return;
3351

52+
const enableStyling = this.settings.enableStyling;
3453
const processor = new CodeblockRantProcessor(source, el);
35-
processor.rant(randomSeed());
54+
processor.rant(randomSeed(), enableStyling);
3655

37-
this.registerRantProcessorForRerant(processor, file);
56+
await this.registerRantProcessorForRerant(processor, file);
3857
}
3958
);
4059

4160
// Register inline Rant blocks.
4261
this.registerMarkdownPostProcessor(
43-
(el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
62+
async (el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
4463
const file = this.app.vault.getAbstractFileByPath(ctx.sourcePath);
4564
if (!file || !(file instanceof TFile)) return;
4665

@@ -52,11 +71,12 @@ export default class RantLangPlugin extends Plugin {
5271
if (text.startsWith(inlineRantQueryPrefix)) {
5372
const code = text.substring(inlineRantQueryPrefix.length).trim();
5473

74+
const enableStyling = this.settings.enableStyling;
5575
const processor = new InlineRantProcessor(code, el, codeblock);
5676
ctx.addChild(processor);
57-
processor.rant(randomSeed());
77+
processor.rant(randomSeed(), enableStyling);
5878

59-
this.registerRantProcessorForRerant(processor, file);
79+
await this.registerRantProcessorForRerant(processor, file);
6080
}
6181
}
6282
}
@@ -74,10 +94,11 @@ export default class RantLangPlugin extends Plugin {
7494
this.fileMap.has(view.file)
7595
) {
7696
if (!checking) {
97+
const enableStyling = this.settings.enableStyling;
7798
const processors = this.fileMap.get(view.file);
7899

79100
processors.forEach((processor) => {
80-
processor.rant(randomSeed());
101+
processor.rant(randomSeed(), enableStyling);
81102
});
82103

83104
new Notice("Re-processed Rant blocks");
@@ -88,7 +109,15 @@ export default class RantLangPlugin extends Plugin {
88109
});
89110
}
90111

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+
) {
92121
// File-based tracking of registered processors inspired by javalent's excellent dice roller plugin: https://github.com/valentine195/obsidian-dice-roller
93122

94123
if (!this.fileMap.has(file)) {

src/processor.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { rant } from "../pkg/obsidian_rantlang_plugin.js";
44
export abstract class BaseRantProcessor extends MarkdownRenderChild {
55
result: string = "";
66
target: HTMLElement;
7+
enableStyling: boolean = false;
78

89
constructor(public input: string, public container: HTMLElement) {
910
super(container);
@@ -20,7 +21,8 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
2021
}
2122
}
2223

23-
rant(seed: number) {
24+
rant(seed: number, enableStyling: boolean) {
25+
this.enableStyling = enableStyling;
2426
this.processInput(seed);
2527
this.renderResult();
2628
}
@@ -29,10 +31,16 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
2931
export class CodeblockRantProcessor extends BaseRantProcessor {
3032
constructor(input: string, container: HTMLElement) {
3133
super(input, container);
32-
this.target = container.createEl("p", { cls: "rant" });
34+
const cls = this.enableStyling ? ["rant", "rant-block"] : "";
35+
this.target = container.createEl("p", { cls });
3336
}
3437

3538
renderResult() {
39+
const cls = this.enableStyling ? ["rant", "rant-block"] : "";
40+
const newChild = createEl("p", { cls });
41+
this.container.replaceChild(newChild, this.target);
42+
this.target = newChild;
43+
3644
const node = createFragment((frag) => {
3745
this.result.split("\n").forEach((text) => {
3846
frag.appendText(text);
@@ -52,7 +60,8 @@ export class InlineRantProcessor extends BaseRantProcessor {
5260
}
5361

5462
renderResult() {
55-
let temp = document.createElement("span");
63+
const cls = this.enableStyling ? ["rant", "rant-inline"] : "";
64+
let temp = createEl("span", { cls });
5665
temp.appendText(this.result);
5766
this.target.replaceWith(temp);
5867
this.target = temp;

src/settings.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { App, PluginSettingTab, Setting } from "obsidian";
2+
import type RantLangPlugin from "./main";
3+
4+
export default class SettingTab extends PluginSettingTab {
5+
additionalContainer: HTMLDivElement;
6+
7+
constructor(app: App, public plugin: RantLangPlugin) {
8+
super(app, plugin);
9+
this.plugin = plugin;
10+
}
11+
async display(): Promise<void> {
12+
this.containerEl.empty();
13+
this.containerEl.createEl("h2", { text: "Rang-Lang settings" });
14+
15+
new Setting(this.containerEl)
16+
.setName("Enable Rant block styling")
17+
.setDesc(
18+
"Turning this off will remove all styling from both inline and code Rant blocks."
19+
)
20+
.addToggle((toggle) =>
21+
toggle
22+
.setValue(this.plugin.settings.enableStyling)
23+
.onChange(
24+
async (value) =>
25+
await this.plugin.updateSettings({ enableStyling: value })
26+
)
27+
);
28+
}
29+
}

styles.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.rant {
2+
border: 1px solid var(--background-modifier-border);
3+
}
4+
5+
.rant-block {
6+
padding: 4px 8px;
7+
}
8+
9+
.rant-inline {
10+
padding: 0px 4px;
11+
}

0 commit comments

Comments
 (0)