Skip to content

Commit 87e7d55

Browse files
committed
feat: add bold/italic styling options to rant codeblocks
1 parent 44a4169 commit 87e7d55

File tree

5 files changed

+79
-29
lines changed

5 files changed

+79
-29
lines changed

README.md

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

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

src/main.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@ import {
1313
CodeblockRantProcessor,
1414
InlineRantProcessor,
1515
BaseRantProcessor,
16+
Customization,
1617
} from "./processor";
1718
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-
};
19+
import SettingTab, { DEFAULT_SETTINGS, RantLangSettings } from "./settings";
2720

2821
export default class RantLangPlugin extends Plugin {
2922
settings: RantLangSettings;
@@ -49,9 +42,20 @@ export default class RantLangPlugin extends Plugin {
4942
const file = this.app.vault.getAbstractFileByPath(ctx.sourcePath);
5043
if (!file || !(file instanceof TFile)) return;
5144

52-
const enableStyling = this.settings.enableStyling;
53-
const processor = new CodeblockRantProcessor(source, el);
54-
processor.rant(randomSeed(), enableStyling);
45+
let customizations: Customization[] = [];
46+
let lines = source.split("\n");
47+
while (lines.length > 0 && ["bold", "italic"].contains(lines[0])) {
48+
customizations.push(lines.shift() as Customization);
49+
}
50+
const input = lines.join("\n");
51+
52+
const processor = new CodeblockRantProcessor(
53+
input,
54+
el,
55+
this.settings,
56+
customizations
57+
);
58+
processor.rant(randomSeed());
5559

5660
await this.registerRantProcessorForRerant(processor, file);
5761
}
@@ -71,10 +75,14 @@ export default class RantLangPlugin extends Plugin {
7175
if (text.startsWith(inlineRantQueryPrefix)) {
7276
const code = text.substring(inlineRantQueryPrefix.length).trim();
7377

74-
const enableStyling = this.settings.enableStyling;
75-
const processor = new InlineRantProcessor(code, el, codeblock);
78+
const processor = new InlineRantProcessor(
79+
code,
80+
el,
81+
codeblock,
82+
this.settings
83+
);
7684
ctx.addChild(processor);
77-
processor.rant(randomSeed(), enableStyling);
85+
processor.rant(randomSeed());
7886

7987
await this.registerRantProcessorForRerant(processor, file);
8088
}
@@ -94,11 +102,10 @@ export default class RantLangPlugin extends Plugin {
94102
this.fileMap.has(view.file)
95103
) {
96104
if (!checking) {
97-
const enableStyling = this.settings.enableStyling;
98105
const processors = this.fileMap.get(view.file);
99106

100107
processors.forEach((processor) => {
101-
processor.rant(randomSeed(), enableStyling);
108+
processor.rant(randomSeed());
102109
});
103110

104111
new Notice("Re-processed Rant blocks");

src/processor.ts

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { MarkdownRenderChild } from "obsidian";
22
import { rant } from "../pkg/obsidian_rantlang_plugin.js";
3+
import { RantLangSettings } from "./settings.js";
34

45
export abstract class BaseRantProcessor extends MarkdownRenderChild {
56
result: string = "";
67
target: HTMLElement;
7-
enableStyling: boolean = false;
88

9-
constructor(public input: string, public container: HTMLElement) {
9+
constructor(
10+
public input: string,
11+
public container: HTMLElement,
12+
public settings: RantLangSettings
13+
) {
1014
super(container);
1115
}
1216

@@ -21,22 +25,30 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
2125
}
2226
}
2327

24-
rant(seed: number, enableStyling: boolean) {
25-
this.enableStyling = enableStyling;
28+
rant(seed: number) {
2629
this.processInput(seed);
2730
this.renderResult();
2831
}
2932
}
3033

34+
export type Customization = "bold" | "italic";
35+
3136
export class CodeblockRantProcessor extends BaseRantProcessor {
32-
constructor(input: string, container: HTMLElement) {
33-
super(input, container);
34-
const cls = this.enableStyling ? ["rant", "rant-block"] : "";
35-
this.target = container.createEl("p", { cls });
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");
3648
}
3749

3850
renderResult() {
39-
const cls = this.enableStyling ? ["rant", "rant-block"] : "";
51+
const cls = this.getStyles();
4052
const newChild = createEl("p", { cls });
4153
this.container.replaceChild(newChild, this.target);
4254
this.target = newChild;
@@ -50,17 +62,30 @@ export class CodeblockRantProcessor extends BaseRantProcessor {
5062

5163
this.target.replaceChildren(node);
5264
}
65+
66+
getStyles() {
67+
let cls = this.settings.enableStyling ? ["rant", "rant-block"] : [];
68+
this.customizations.forEach((style) => {
69+
cls.push(`rant-${style}`);
70+
});
71+
return cls;
72+
}
5373
}
5474

5575
/** Processes inline Rant blocks. */
5676
export class InlineRantProcessor extends BaseRantProcessor {
57-
constructor(input: string, container: HTMLElement, target: HTMLElement) {
58-
super(input, container);
77+
constructor(
78+
input: string,
79+
container: HTMLElement,
80+
target: HTMLElement,
81+
settings: RantLangSettings
82+
) {
83+
super(input, container, settings);
5984
this.target = target;
6085
}
6186

6287
renderResult() {
63-
const cls = this.enableStyling ? ["rant", "rant-inline"] : "";
88+
const cls = this.settings.enableStyling ? ["rant", "rant-inline"] : "";
6489
let temp = createEl("span", { cls });
6590
temp.appendText(this.result);
6691
this.target.replaceWith(temp);

src/settings.ts

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

4+
export interface RantLangSettings {
5+
enableStyling: boolean;
6+
}
7+
8+
export const DEFAULT_SETTINGS: RantLangSettings = {
9+
enableStyling: true,
10+
};
11+
412
export default class SettingTab extends PluginSettingTab {
513
additionalContainer: HTMLDivElement;
614

styles.css

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@
99
.rant-inline {
1010
padding: 0px 4px;
1111
}
12+
13+
.rant-bold {
14+
font-weight: bold;
15+
}
16+
17+
.rant-italic {
18+
font-style: italic;
19+
}

0 commit comments

Comments
 (0)