Skip to content

Commit c9c872e

Browse files
committed
links, shortcodes in targets, punctuation
1 parent 936a458 commit c9c872e

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

tools/document-generator/main.ts

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ type Code = WithAttr & {
1717
text: string;
1818
};
1919

20+
type Link = WithAttr & {
21+
type: "Link";
22+
content: Inline[];
23+
target: string;
24+
};
25+
2026
type Emph = {
2127
type: "Emph";
2228
content: Inline[];
@@ -36,14 +42,15 @@ type Span = WithAttr & {
3642
content: Inline[];
3743
};
3844

39-
type Inline = Code | Emph | Str | Space | Span | Shortcode;
45+
type Inline = Code | Emph | Str | Space | Span | Shortcode | Link;
4046
const isCode = (inline: Inline): inline is Code => inline.type === "Code";
4147
const isEmph = (inline: Inline): inline is Emph => inline.type === "Emph";
4248
const isStr = (inline: Inline): inline is Str => inline.type === "Str";
4349
const isSpace = (inline: Inline): inline is Space => inline.type === "Space";
4450
const isSpan = (inline: Inline): inline is Span => inline.type === "Span";
4551
const isShortcode = (inline: Inline): inline is Shortcode =>
4652
inline.type === "Shortcode";
53+
const isLink = (inline: Inline): inline is Link => inline.type === "Link";
4754

4855
type Para = {
4956
type: "Para";
@@ -69,6 +76,16 @@ class RenderContext {
6976
indent: number;
7077
content: string[];
7178

79+
renderLink(link: Link) {
80+
this.content.push("[");
81+
for (const inline of link.content) {
82+
this.renderInline(inline);
83+
}
84+
this.content.push("]");
85+
this.content.push("(" + link.target + ")");
86+
this.renderAttr(link.attr);
87+
}
88+
7289
renderAttr(attr?: Attr) {
7390
if (attr === undefined) {
7491
return;
@@ -131,6 +148,9 @@ class RenderContext {
131148
if (isShortcode(inline)) {
132149
this.renderShortcode(inline);
133150
}
151+
if (isLink(inline)) {
152+
this.renderLink(inline);
153+
}
134154
}
135155

136156
renderPara(para: Para) {
@@ -180,7 +200,9 @@ class GeneratorContext {
180200
emph: number;
181201
code: number;
182202
span: number;
203+
link: number;
183204
shortcode: number;
205+
targetShortcode: number;
184206
};
185207

186208
sizes: {
@@ -228,6 +250,11 @@ class GeneratorContext {
228250
return newContext;
229251
}
230252

253+
generatePunctuation() {
254+
const punctuations = [".", "!", "?", ",", ";", ":"];
255+
return punctuations[~~(Math.random() * punctuations.length)];
256+
}
257+
231258
////////////////////////////////////////////////////////////////////////////////
232259
// Attr-related functions
233260

@@ -298,6 +325,9 @@ class GeneratorContext {
298325
if (Math.random() < this.probabilities.emph) {
299326
return "Emph";
300327
}
328+
if (Math.random() < this.probabilities.link) {
329+
return "Link";
330+
}
301331
if (Math.random() < this.probabilities.shortcode) {
302332
return "InlineShortcode";
303333
}
@@ -367,12 +397,42 @@ class GeneratorContext {
367397
};
368398
}
369399

400+
generateTarget(): string {
401+
let target = this.freshId();
402+
if (Math.random() < this.probabilities.targetShortcode) {
403+
const shortcode = this.generateInlineShortcode();
404+
target = `${target}-{{< ${shortcode.content} >}}`;
405+
}
406+
return target;
407+
}
408+
409+
generateLink(): Link {
410+
const small = this.smaller();
411+
const contentSize = ~~(Math.random() * small.sizes.inline) + 1;
412+
const content: Inline[] = [];
413+
414+
for (let i = 0; i < contentSize; i++) {
415+
const inline = small.generateInline();
416+
if (inline) {
417+
content.push(inline);
418+
}
419+
}
420+
421+
return {
422+
attr: this.randomAttr(),
423+
type: "Link",
424+
content,
425+
target: this.generateTarget(),
426+
};
427+
}
428+
370429
generateInline() {
371430
const dispatch = {
372431
Str: () => this.generateStr(),
373432
Code: () => this.generateCode(),
374433
Emph: () => this.generateEmph(),
375434
Span: () => this.generateSpan(),
435+
Link: () => this.generateLink(),
376436
InlineShortcode: () => this.generateInlineShortcode(),
377437
Null: () => {},
378438
};
@@ -401,16 +461,14 @@ class GeneratorContext {
401461
} else {
402462
content.push({
403463
type: "Str",
404-
text: ".",
464+
text: small.generatePunctuation(),
405465
});
406466
}
407467
} else {
408-
if (i === sentenceSize - 1) {
409-
content.push({
410-
type: "Str",
411-
text: ".",
412-
});
413-
}
468+
content.push({
469+
type: "Str",
470+
text: small.generatePunctuation(),
471+
});
414472
}
415473
}
416474
};
@@ -467,7 +525,9 @@ class GeneratorContext {
467525
code: 0.5,
468526
span: 0.5,
469527
emph: 0.5,
528+
link: 0.5,
470529
shortcode: 0.5,
530+
targetShortcode: 0.25,
471531
};
472532
this.sizes = {
473533
inline: 10,

0 commit comments

Comments
 (0)