@@ -8,12 +8,18 @@ const REMARK_STRINGIFY_OPTIONS: Options = {
8
8
emphasis : '*' ,
9
9
fences : true ,
10
10
listItemIndent : 'one' ,
11
- tightDefinitions : true
11
+ tightDefinitions : true ,
12
+ handlers : {
13
+ placeholder : ( node : PlaceholderNode ) => {
14
+ console . log ( 'placeholder node' , node )
15
+ return node . value
16
+ }
17
+ } as any
12
18
}
13
19
14
-
15
20
function createRemarkProcessor ( ) {
16
- return remark ( ) . use ( remarkStringify , REMARK_STRINGIFY_OPTIONS )
21
+ return remark ( )
22
+ . use ( remarkStringify , REMARK_STRINGIFY_OPTIONS )
17
23
}
18
24
19
25
/**
@@ -22,50 +28,14 @@ function createRemarkProcessor() {
22
28
* @returns Plain string representation
23
29
*/
24
30
export function customAstToString ( ast : Root ) : string {
25
- let result = ''
26
- for ( const node of ast . children ) {
27
- result += nodeToString ( node ) + '\n'
28
- }
29
- return result . trim ( )
30
- }
31
-
32
- /**
33
- * Convert a single node to string
34
- * @param node AST node
35
- * @returns String representation
36
- */
37
- function nodeToString ( node : any ) : string {
38
- switch ( node . type ) {
39
- case 'paragraph' :
40
- return paragraphToString ( node )
41
- case 'text' :
42
- return node . value
43
- default :
44
- return createRemarkProcessor ( ) . stringify ( { type : 'root' , children : [ node ] } ) . trim ( )
45
- }
31
+ const processor = createRemarkProcessor ( )
32
+ return processor . stringify ( ast ) . trim ( )
46
33
}
47
34
48
- /**
49
- * Convert paragraph node to string
50
- * @param node Paragraph node
51
- * @returns String representation
52
- */
53
- function paragraphToString ( node : any ) : string {
54
- return childrenToString ( node )
55
- }
56
35
57
36
/**
58
- * Process children of a node and join them
59
- * @param node Parent node
60
- * @returns Combined string of all children
37
+ * Process code blocks with labels and convert them to placeholders
61
38
*/
62
- function childrenToString ( node : any ) : string {
63
- if ( ! node . children || node . children . length === 0 ) {
64
- return ''
65
- }
66
- return node . children . map ( ( child : any ) => nodeToString ( child ) ) . join ( '' )
67
- }
68
-
69
39
export function processCodeBlocksWithLabel ( ast : Root ) : RootContent [ ] {
70
40
const newChildren : RootContent [ ] = [ ]
71
41
for ( let i = 0 ; i < ast . children . length ; i ++ ) {
@@ -96,18 +66,19 @@ export function processCodeBlocksWithLabel(ast: Root): RootContent[] {
96
66
nextNode . position . start . line - node . position . end . line === 1
97
67
98
68
let finalCommandText = ''
69
+ let placeholderNode : RootContent | null = null
99
70
let shouldProcessNode = true
100
71
101
- // processing differet type of context
102
72
switch ( metas [ 'label' ] ) {
103
73
case 'changes' :
104
- finalCommandText = '[[contextCommand:"changes"]]'
74
+ finalCommandText = 'contextCommand:changes'
75
+ placeholderNode = createPlaceholderNode ( `[[contextCommand:${ finalCommandText } ]]` ) as unknown as RootContent
105
76
break
106
77
case 'file' :
107
78
if ( metas [ 'object' ] ) {
108
79
try {
109
- finalCommandText = formatPlaceholder ( ' file' , metas [ 'object' ] )
110
- if ( ! finalCommandText ) {
80
+ placeholderNode = createPlaceholderNode ( `[[ file: ${ metas [ 'object' ] } ]]` ) as unknown as RootContent
81
+ if ( ! placeholderNode ) {
111
82
shouldProcessNode = false
112
83
newChildren . push ( node )
113
84
}
@@ -120,8 +91,8 @@ export function processCodeBlocksWithLabel(ast: Root): RootContent[] {
120
91
case 'symbol' :
121
92
if ( metas [ 'object' ] ) {
122
93
try {
123
- finalCommandText = formatPlaceholder ( ' symbol' , metas [ 'object' ] )
124
- if ( ! finalCommandText ) {
94
+ placeholderNode = createPlaceholderNode ( `[[ symbol: ${ metas [ 'object' ] } ]]` ) as unknown as RootContent
95
+ if ( ! placeholderNode ) {
125
96
shouldProcessNode = false
126
97
newChildren . push ( node )
127
98
}
@@ -154,7 +125,7 @@ export function processCodeBlocksWithLabel(ast: Root): RootContent[] {
154
125
type : 'paragraph' ,
155
126
children : [
156
127
...( prevNode . children || [ ] ) ,
157
- { type : 'text' , value : ` ${ finalCommandText } ` } ,
128
+ placeholderNode || { type : 'text' , value : ` ${ finalCommandText } ` } ,
158
129
...( nextNode . children || [ ] )
159
130
]
160
131
} as RootContent )
@@ -167,7 +138,7 @@ export function processCodeBlocksWithLabel(ast: Root): RootContent[] {
167
138
newChildren . push ( {
168
139
type : 'paragraph' ,
169
140
children : [
170
- { type : 'text' , value : `${ finalCommandText } ` } ,
141
+ placeholderNode || { type : 'text' , value : `${ finalCommandText } ` } ,
171
142
...( nextNode . children || [ ] )
172
143
]
173
144
} as RootContent )
@@ -176,14 +147,15 @@ export function processCodeBlocksWithLabel(ast: Root): RootContent[] {
176
147
prevNode . type === 'paragraph' &&
177
148
isPrevNodeSameLine
178
149
) {
179
- ; ( prevNode . children || [ ] ) . push ( {
180
- type : 'text' ,
181
- value : ` ${ finalCommandText } `
182
- } )
150
+ ; ( prevNode . children || [ ] ) . push (
151
+ placeholderNode || { type : 'text' , value : ` ${ finalCommandText } ` }
152
+ )
183
153
} else {
184
154
newChildren . push ( {
185
155
type : 'paragraph' ,
186
- children : [ { type : 'text' , value : finalCommandText } ]
156
+ children : [
157
+ placeholderNode || { type : 'text' , value : finalCommandText }
158
+ ]
187
159
} as RootContent )
188
160
}
189
161
} else {
@@ -217,7 +189,6 @@ export function formatObjectToMarkdownBlock(
217
189
content : string
218
190
) : string {
219
191
try {
220
- // Convert the object to a JSON string
221
192
const objJSON = JSON . stringify ( obj )
222
193
223
194
const codeNode : Root = {
@@ -235,29 +206,24 @@ export function formatObjectToMarkdownBlock(
235
206
const processor = createRemarkProcessor ( )
236
207
237
208
const res = '\n' + processor . stringify ( codeNode ) . trim ( ) + '\n'
238
- console . log ( 'res' , res )
239
209
return res ;
240
210
} catch ( error ) {
241
- console . error ( `Error formatting ${ label } to markdown block:` , error )
242
211
return `\n*Error formatting ${ label } *\n`
243
212
}
244
213
}
245
214
246
215
247
216
248
- /**
249
- * Format a placeholder with proper backslash escaping
250
- * @param type The type of placeholder (e.g., 'file', 'symbol')
251
- * @param objStr The string representation of the object
252
- * @returns The formatted placeholder text
253
- */
254
- export function formatPlaceholder ( type : string , objStr : string ) : string {
255
- if ( ! objStr ) return ''
256
-
257
- try {
258
- return `[[${ type } :${ objStr } ]]`
259
- } catch ( error ) {
260
- console . error ( `Error formatting ${ type } placeholder:` , error )
261
- return ''
262
- }
217
+
218
+
219
+ export interface PlaceholderNode extends Node {
220
+ type : 'placeholder'
221
+ value : string
222
+ }
223
+
224
+ export function createPlaceholderNode ( value : string ) : PlaceholderNode {
225
+ return {
226
+ type : 'placeholder' ,
227
+ value : value ,
228
+ } as PlaceholderNode
263
229
}
0 commit comments