1
+ import * as mod from "https://deno.land/std/yaml/mod.ts" ;
2
+
1
3
type GeneratorFunction < T > = ( context : GeneratorContext ) => T ;
2
4
3
5
type Attr = {
@@ -34,12 +36,14 @@ type Span = WithAttr & {
34
36
content : Inline [ ] ;
35
37
} ;
36
38
37
- type Inline = Code | Emph | Str | Space | Span ;
39
+ type Inline = Code | Emph | Str | Space | Span | Shortcode ;
38
40
const isCode = ( inline : Inline ) : inline is Code => inline . type === "Code" ;
39
41
const isEmph = ( inline : Inline ) : inline is Emph => inline . type === "Emph" ;
40
42
const isStr = ( inline : Inline ) : inline is Str => inline . type === "Str" ;
41
43
const isSpace = ( inline : Inline ) : inline is Space => inline . type === "Space" ;
42
44
const isSpan = ( inline : Inline ) : inline is Span => inline . type === "Span" ;
45
+ const isShortcode = ( inline : Inline ) : inline is Shortcode =>
46
+ inline . type === "Shortcode" ;
43
47
44
48
type Para = {
45
49
type : "Para" ;
@@ -52,6 +56,13 @@ const isPara = (block: Block): block is Para => block.type === "Para";
52
56
type Document = {
53
57
type : "Document" ;
54
58
blocks : Block [ ] ;
59
+ meta : Record < string , unknown > ;
60
+ } ;
61
+
62
+ type Shortcode = {
63
+ type : "Shortcode" ;
64
+ content : string ;
65
+ escaped ?: boolean ;
55
66
} ;
56
67
57
68
class RenderContext {
@@ -86,6 +97,12 @@ class RenderContext {
86
97
}
87
98
}
88
99
100
+ renderShortcode ( shortcode : Shortcode ) {
101
+ const open = shortcode . escaped ? "{{{<" : "{{<" ;
102
+ const close = shortcode . escaped ? ">}}}" : ">}}" ;
103
+ this . content . push ( `${ open } ${ shortcode . content } ${ close } ` ) ;
104
+ }
105
+
89
106
renderInline ( inline : Inline ) {
90
107
if ( isCode ( inline ) ) {
91
108
this . content . push ( "`" + inline . text + "`" ) ;
@@ -111,6 +128,9 @@ class RenderContext {
111
128
if ( isSpan ( inline ) ) {
112
129
this . renderSpan ( inline ) ;
113
130
}
131
+ if ( isShortcode ( inline ) ) {
132
+ this . renderShortcode ( inline ) ;
133
+ }
114
134
}
115
135
116
136
renderPara ( para : Para ) {
@@ -131,6 +151,11 @@ class RenderContext {
131
151
}
132
152
133
153
renderDocument ( document : Document ) {
154
+ if ( Object . entries ( document . meta ) . length > 0 ) {
155
+ this . content . push ( "---\n" ) ;
156
+ this . content . push ( mod . stringify ( document . meta ) ) ;
157
+ this . content . push ( "---\n\n" ) ;
158
+ }
134
159
for ( const block of document . blocks ) {
135
160
this . renderBlock ( block ) ;
136
161
}
@@ -155,6 +180,7 @@ class GeneratorContext {
155
180
emph : number ;
156
181
code : number ;
157
182
span : number ;
183
+ shortcode : number ;
158
184
} ;
159
185
160
186
sizes : {
@@ -166,6 +192,8 @@ class GeneratorContext {
166
192
classes : string [ ] ;
167
193
ids : string [ ] ;
168
194
195
+ meta : Record < string , unknown > ;
196
+
169
197
////////////////////////////////////////////////////////////////////////////////
170
198
// helpers
171
199
@@ -180,13 +208,23 @@ class GeneratorContext {
180
208
return result ;
181
209
}
182
210
211
+ assign ( other : GeneratorContext ) {
212
+ this . probabilities = other . probabilities ;
213
+ this . sizes = other . sizes ;
214
+ this . classes = other . classes ;
215
+ this . ids = other . ids ;
216
+ this . meta = other . meta ;
217
+ }
218
+
183
219
smaller ( ) : GeneratorContext {
184
220
const newContext = new GeneratorContext ( ) ;
221
+ newContext . assign ( this ) ;
185
222
newContext . sizes = {
186
223
...this . sizes ,
187
224
inline : ~ ~ ( this . sizes . inline * 0.5 ) ,
188
225
block : ~ ~ ( this . sizes . block * 0.5 ) ,
189
226
} ;
227
+
190
228
return newContext ;
191
229
}
192
230
@@ -260,10 +298,23 @@ class GeneratorContext {
260
298
if ( Math . random ( ) < this . probabilities . emph ) {
261
299
return "Emph" ;
262
300
}
301
+ if ( Math . random ( ) < this . probabilities . shortcode ) {
302
+ return "InlineShortcode" ;
303
+ }
263
304
264
305
return "Null" ;
265
306
}
266
307
308
+ generateInlineShortcode ( ) : Shortcode {
309
+ const metaKey = this . freshId ( ) ;
310
+ const metaValue = this . freshId ( ) ;
311
+ this . meta [ metaKey ] = metaValue ;
312
+ return {
313
+ type : "Shortcode" ,
314
+ content : `meta ${ metaKey } ` ,
315
+ } ;
316
+ }
317
+
267
318
generateStr ( ) : Str {
268
319
return {
269
320
type : "Str" ,
@@ -322,6 +373,7 @@ class GeneratorContext {
322
373
Code : ( ) => this . generateCode ( ) ,
323
374
Emph : ( ) => this . generateEmph ( ) ,
324
375
Span : ( ) => this . generateSpan ( ) ,
376
+ InlineShortcode : ( ) => this . generateInlineShortcode ( ) ,
325
377
Null : ( ) => { } ,
326
378
} ;
327
379
return dispatch [ this . chooseInlineType ( ) ] ( ) ;
@@ -394,10 +446,12 @@ class GeneratorContext {
394
446
blocks . push ( small . generateBlock ( ) ) ;
395
447
}
396
448
397
- return {
449
+ const result : Document = {
398
450
type : "Document" ,
399
451
blocks,
452
+ meta : this . meta ,
400
453
} ;
454
+ return result ;
401
455
}
402
456
403
457
////////////////////////////////////////////////////////////////////////////////
@@ -413,12 +467,14 @@ class GeneratorContext {
413
467
code : 0.5 ,
414
468
span : 0.5 ,
415
469
emph : 0.5 ,
470
+ shortcode : 0.5 ,
416
471
} ;
417
472
this . sizes = {
418
473
inline : 10 ,
419
474
block : 10 ,
420
475
sentence : 10 ,
421
476
} ;
477
+ this . meta = { } ;
422
478
}
423
479
}
424
480
0 commit comments