Skip to content

Commit f1182c1

Browse files
committed
feat: enable markdown processing within Rant codeblocks
This allows for e.g. links, md styling, or markdown post processing from other plugins (e.g., dice-roller) to be displayed correctly.
1 parent d8c2771 commit f1182c1

File tree

5 files changed

+44
-60
lines changed

5 files changed

+44
-60
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Obsidian Rant-Lang
22
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/lanice/obsidian-rant?style=for-the-badge&sort=semver)](https://github.com/lanice/obsidian-rant/releases/latest)
3-
![GitHub All Releases](https://img.shields.io/github/downloads/lanice/obsidian-rant/total?style=for-the-badge)
43

54
Thin wrapper around the [Rant language](https://rant-lang.org/) Rust crate to be used in Obsidian.
65

src/main.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class RantLangPlugin extends Plugin {
2727
const buffer = Uint8Array.from(atob(rustPlugin), (c) => c.charCodeAt(0));
2828
await init(Promise.resolve(buffer));
2929

30-
// Settings initialization
30+
// Initialize settings.
3131
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
3232
this.addSettingTab(new SettingTab(this.app, this));
3333

@@ -53,9 +53,11 @@ export default class RantLangPlugin extends Plugin {
5353
input,
5454
el,
5555
this.settings,
56+
ctx.sourcePath,
5657
customizations
5758
);
5859
processor.rant(randomSeed());
60+
ctx.addChild(processor);
5961

6062
await this.registerRantProcessorForRerant(processor, file);
6163
}
@@ -77,9 +79,9 @@ export default class RantLangPlugin extends Plugin {
7779

7880
const processor = new InlineRantProcessor(
7981
code,
80-
el,
8182
codeblock,
82-
this.settings
83+
this.settings,
84+
ctx.sourcePath
8385
);
8486
ctx.addChild(processor);
8587
processor.rant(randomSeed());

src/processor.ts

+27-45
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { MarkdownRenderChild } from "obsidian";
1+
import { MarkdownRenderChild, MarkdownRenderer } from "obsidian";
22
import { rant } from "../pkg/obsidian_rantlang_plugin.js";
33
import { RantLangSettings } from "./settings.js";
44

55
export abstract class BaseRantProcessor extends MarkdownRenderChild {
66
result: string = "";
7-
target: HTMLElement;
87

98
constructor(
109
public input: string,
1110
public container: HTMLElement,
12-
public settings: RantLangSettings
11+
public settings: RantLangSettings,
12+
public sourcePath: string,
13+
public customizations: Customization[] = []
1314
) {
1415
super(container);
1516
}
@@ -34,61 +35,42 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
3435
export type Customization = "bold" | "italic";
3536

3637
export class CodeblockRantProcessor extends BaseRantProcessor {
37-
customizations: Customization[];
38-
39-
constructor(
40-
input: string,
41-
container: HTMLElement,
42-
settings: RantLangSettings,
43-
customizations: Customization[] = []
44-
) {
45-
super(input, container, settings);
46-
this.customizations = customizations;
47-
this.target = container.createEl("p");
48-
}
49-
5038
renderResult() {
51-
const cls = this.getStyles();
52-
const newChild = createEl("p", { cls });
53-
this.container.replaceChild(newChild, this.target);
54-
this.target = newChild;
55-
56-
const node = createFragment((frag) => {
57-
this.result.split("\n").forEach((text) => {
58-
frag.appendText(text);
59-
frag.createEl("br");
60-
});
61-
});
62-
63-
this.target.replaceChildren(node);
39+
this.container.empty();
40+
const content = this.container.createDiv({ cls: this.getStyles() });
41+
MarkdownRenderer.renderMarkdown(
42+
this.result,
43+
content,
44+
this.sourcePath,
45+
this
46+
);
6447
}
6548

6649
getStyles() {
67-
let cls = this.settings.enableStyling ? ["rant", "rant-block"] : [];
50+
let cls = ["rant-block"];
51+
if (this.settings.highlight) {
52+
cls.push("rant-highlight");
53+
}
6854
this.customizations.forEach((style) => {
6955
cls.push(`rant-${style}`);
7056
});
7157
return cls;
7258
}
7359
}
7460

75-
/** Processes inline Rant blocks. */
7661
export class InlineRantProcessor extends BaseRantProcessor {
77-
constructor(
78-
input: string,
79-
container: HTMLElement,
80-
target: HTMLElement,
81-
settings: RantLangSettings
82-
) {
83-
super(input, container, settings);
84-
this.target = target;
85-
}
86-
8762
renderResult() {
88-
const cls = this.settings.enableStyling ? ["rant", "rant-inline"] : "";
89-
let temp = createEl("span", { cls });
63+
let temp = createEl("span", { cls: this.getStyles() });
9064
temp.appendText(this.result);
91-
this.target.replaceWith(temp);
92-
this.target = temp;
65+
this.container.replaceWith(temp);
66+
this.container = temp;
67+
}
68+
69+
getStyles() {
70+
let cls = ["rant-inline"];
71+
if (this.settings.highlight) {
72+
cls.push("rant-highlight");
73+
}
74+
return cls;
9375
}
9476
}

src/settings.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { App, PluginSettingTab, Setting } from "obsidian";
22
import type RantLangPlugin from "./main";
33

44
export interface RantLangSettings {
5-
enableStyling: boolean;
5+
highlight: boolean;
66
}
77

88
export const DEFAULT_SETTINGS: RantLangSettings = {
9-
enableStyling: true,
9+
highlight: false,
1010
};
1111

1212
export default class SettingTab extends PluginSettingTab {
@@ -18,19 +18,20 @@ export default class SettingTab extends PluginSettingTab {
1818
}
1919
async display(): Promise<void> {
2020
this.containerEl.empty();
21+
this.containerEl.addClass("rant-settings");
2122
this.containerEl.createEl("h2", { text: "Rang-Lang settings" });
2223

2324
new Setting(this.containerEl)
24-
.setName("Enable Rant block styling")
25+
.setName("Highlight Rant blocks")
2526
.setDesc(
26-
"Turning this off will remove all styling from both inline and code Rant blocks."
27+
"Highlight Rant blocks (both inline and codeblocks) by adding a box around them."
2728
)
2829
.addToggle((toggle) =>
2930
toggle
30-
.setValue(this.plugin.settings.enableStyling)
31+
.setValue(this.plugin.settings.highlight)
3132
.onChange(
3233
async (value) =>
33-
await this.plugin.updateSettings({ enableStyling: value })
34+
await this.plugin.updateSettings({ highlight: value })
3435
)
3536
);
3637
}

styles.css

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
.rant {
1+
.rant-highlight {
22
border: 1px solid var(--background-modifier-border);
33
}
44

5-
.rant-block {
6-
padding: 4px 8px;
5+
.rant-highlight.rant-inline {
6+
padding: 0px 4px;
77
}
88

9-
.rant-inline {
10-
padding: 0px 4px;
9+
.rant-highlight.rant-block {
10+
padding: 0px 8px;
1111
}
1212

1313
.rant-bold {

0 commit comments

Comments
 (0)