1
- import { MarkdownRenderChild } from "obsidian" ;
1
+ import { MarkdownRenderChild , MarkdownRenderer } from "obsidian" ;
2
2
import { rant } from "../pkg/obsidian_rantlang_plugin.js" ;
3
3
import { RantLangSettings } from "./settings.js" ;
4
4
5
5
export abstract class BaseRantProcessor extends MarkdownRenderChild {
6
6
result : string = "" ;
7
- target : HTMLElement ;
8
7
9
8
constructor (
10
9
public input : string ,
11
10
public container : HTMLElement ,
12
- public settings : RantLangSettings
11
+ public settings : RantLangSettings ,
12
+ public sourcePath : string ,
13
+ public customizations : Customization [ ] = [ ]
13
14
) {
14
15
super ( container ) ;
15
16
}
@@ -34,61 +35,42 @@ export abstract class BaseRantProcessor extends MarkdownRenderChild {
34
35
export type Customization = "bold" | "italic" ;
35
36
36
37
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
-
50
38
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
+ ) ;
64
47
}
65
48
66
49
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
+ }
68
54
this . customizations . forEach ( ( style ) => {
69
55
cls . push ( `rant-${ style } ` ) ;
70
56
} ) ;
71
57
return cls ;
72
58
}
73
59
}
74
60
75
- /** Processes inline Rant blocks. */
76
61
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
-
87
62
renderResult ( ) {
88
- const cls = this . settings . enableStyling ? [ "rant" , "rant-inline" ] : "" ;
89
- let temp = createEl ( "span" , { cls } ) ;
63
+ let temp = createEl ( "span" , { cls : this . getStyles ( ) } ) ;
90
64
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 ;
93
75
}
94
76
}
0 commit comments