mdbook_lad_preprocessor 0.20.0

Language Agnostic Declaration (LAD) file format for the bevy_mod_scripting crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
use std::{borrow::Cow, collections::HashSet, path::PathBuf};

use ladfile::{
    LadArgument, LadFieldOrVariableKind, LadFile, LadFunction, LadInstance, LadTypeDefinition,
    LadTypeId, LadTypeLayout, LadVisitable, ReflectionPrimitiveKind,
};
use mdbook_preprocessor::book::{BookItem, Chapter, SectionNumber};

use crate::{
    argument_visitor::MarkdownArgumentVisitor,
    markdown::{IntoMarkdown, Markdown, MarkdownBuilder, TableBuilder, markdown_substring},
    markdown_vec,
};

fn print_type(
    ladfile: &LadFile,
    type_: &dyn LadVisitable,
    raw_type_id_replacement: Option<&'static str>,
    linkifier_base_path_and_escape_opt: Option<(PathBuf, bool)>,
) -> String {
    let mut visitor =
        if let Some((linkifier_base_path, _)) = linkifier_base_path_and_escape_opt.clone() {
            MarkdownArgumentVisitor::new_with_linkifier(ladfile, move |str| {
                let printed_type = linkify_filename(str);
                linkifier_base_path.join(printed_type).with_extension("md")
            })
        } else {
            MarkdownArgumentVisitor::new(ladfile)
        };
    if let Some(replacement) = raw_type_id_replacement {
        visitor = visitor.with_raw_type_id_replacement(replacement);
    }

    type_.accept(&mut visitor);
    let mut printed = visitor.build();
    if let Some((_, escape_opt)) = linkifier_base_path_and_escape_opt
        && escape_opt
    {
        printed = escape_markdown(printed)
    }
    printed
}

fn escape_markdown(markdown: String) -> String {
    markdown
        .replace("<", "\\<")
        .replace(">", "\\>")
        .replace("|", "\\|")
}

#[derive(Debug)]
pub(crate) enum SectionData<'a> {
    Summary {
        title: Option<String>,
    },
    /// A link directory to all the types within the ladfile
    TypeSummary,
    /// A link directory to all global functions within the ladfile
    FunctionSummary,
    /// A link directory to all global instances within the ladfile
    InstancesSummary,
    TypeDetail {
        lad_type_id: &'a LadTypeId,
        lad_type: &'a LadTypeDefinition,
    },
    FunctionDetail {
        types_directory: PathBuf,
        function: &'a LadFunction,
    },
}

/// Sections which convert to single markdown files
#[derive(Debug)]
pub(crate) struct Section<'a> {
    /// The path to the parent we can use for absolute links
    pub parent_path: PathBuf,
    pub ladfile: &'a LadFile,
    pub data: SectionData<'a>,
}

/// Makes a filename safe to put in links
pub fn linkify_filename(name: impl Into<String>) -> String {
    name.into()
        .to_lowercase()
        .replace(" ", "_")
        .replace("<", "")
        .replace(">", "")
}

impl<'a> Section<'a> {
    pub(crate) fn new(parent_path: PathBuf, ladfile: &'a LadFile, data: SectionData<'a>) -> Self {
        Self {
            ladfile,
            data,
            parent_path,
        }
    }

    /// convert into a chapter, including children
    pub(crate) fn into_chapter(self, parent: Option<&Chapter>, index: usize) -> Chapter {
        let mut builder = MarkdownBuilder::new();
        self.to_markdown(&mut builder);

        let default_chapter = Chapter::default();
        let parent = match parent {
            Some(parent) => parent,
            None => &default_chapter,
        };

        let parent_path = self.parent_path.clone();

        let parent_source_path = parent
            .source_path
            .clone()
            .unwrap_or_default()
            .with_extension("");

        let current_number = if let Some(mut parent_number) = parent.number.clone() {
            parent_number.push(index as u32);
            parent_number
        } else {
            SectionNumber::new(vec![index as u32])
        };

        let mut chapter = Chapter {
            name: self.title(),
            content: builder.build(),
            parent_names: vec![parent.name.clone()],
            path: Some(parent_path.join(self.file_name())),
            source_path: Some(parent_source_path.join(self.file_name())),
            number: Some(current_number),
            sub_items: vec![],
        };

        chapter.sub_items = self
            .children()
            .into_iter()
            .enumerate()
            .map(|(i, c)| c.into_chapter(Some(&chapter), i))
            .map(BookItem::Chapter)
            .collect();

        chapter
    }

    pub(crate) fn title(&self) -> String {
        match &self.data {
            SectionData::Summary { title, .. } => {
                title.as_deref().unwrap_or("Bindings Summary").to_owned()
            }
            SectionData::TypeSummary { .. } => "Types".to_owned(),
            SectionData::FunctionSummary { .. } => "Functions".to_owned(),
            SectionData::InstancesSummary { .. } => "Globals".to_owned(),
            SectionData::TypeDetail { lad_type_id, .. } => {
                print_type(self.ladfile, *lad_type_id, None, None)
            }
            SectionData::FunctionDetail { function, .. } => {
                function.identifier_with_overload().to_string()
            }
        }
    }

    pub(crate) fn is_code_heading(&self) -> bool {
        matches!(
            self.data,
            SectionData::TypeDetail { .. } | SectionData::FunctionDetail { .. }
        )
    }

    pub(crate) fn file_name(&self) -> String {
        linkify_filename(self.title()) + ".md"
    }

    pub(crate) fn children(&self) -> Vec<Section<'a>> {
        let child_parent_path = self
            .parent_path
            .join(linkify_filename(self.title()))
            .with_extension("");

        match self.data {
            SectionData::Summary { .. } => {
                vec![
                    Section::new(
                        child_parent_path.clone(),
                        self.ladfile,
                        SectionData::InstancesSummary,
                    ),
                    Section::new(
                        child_parent_path.clone(),
                        self.ladfile,
                        SectionData::FunctionSummary,
                    ),
                    Section::new(
                        child_parent_path.clone(),
                        self.ladfile,
                        SectionData::TypeSummary,
                    ),
                ]
            }
            SectionData::TypeSummary => self
                .ladfile
                .types
                .iter()
                .map(|(lad_type_id, lad_type)| {
                    Section::new(
                        child_parent_path.clone(),
                        self.ladfile,
                        SectionData::TypeDetail {
                            lad_type,
                            lad_type_id,
                        },
                    )
                })
                .collect(),

            SectionData::FunctionSummary => {
                let associated_functions = self
                    .ladfile
                    .types
                    .iter()
                    .flat_map(|t| &t.1.associated_functions)
                    .collect::<HashSet<_>>();

                let non_associated_functions = self
                    .ladfile
                    .functions
                    .iter()
                    .filter_map(|f| (!associated_functions.contains(f.0)).then_some(f.1));

                non_associated_functions
                    .map(|function| {
                        Section::new(
                            child_parent_path.clone(),
                            self.ladfile,
                            SectionData::FunctionDetail {
                                function,
                                types_directory: PathBuf::from("../types"),
                            },
                        )
                    })
                    .collect()
            }
            SectionData::InstancesSummary { .. } => {
                vec![]
            }
            SectionData::TypeDetail { lad_type, .. } => lad_type
                .associated_functions
                .iter()
                .filter_map(|f| {
                    let function = self.ladfile.functions.get(f)?;
                    Some(Section::new(
                        child_parent_path.clone(),
                        self.ladfile,
                        SectionData::FunctionDetail {
                            function,
                            types_directory: PathBuf::from("../../types"),
                        },
                    ))
                })
                .collect(),
            SectionData::FunctionDetail { .. } => vec![],
        }
    }

    pub(crate) fn section_items(&self) -> Vec<SectionItem<'_>> {
        match self.data {
            SectionData::Summary { .. } => {
                let title = self.title().clone();

                vec![SectionItem::Markdown {
                    markdown: Box::new(move |builder| {
                        builder.heading(2, "Contents");
                        builder.text("This is an automatically generated file, you'll find links to the contents below");
                        builder.table(|builder| {
                            builder.headers(vec!["Section", "Contents"]);
                            builder.row(markdown_vec![
                                Markdown::new_paragraph("Types").code(),
                                Markdown::Link {
                                    text: Box::new("Describes all available binding types"),
                                    url: format!("./{}/types.md", linkify_filename(title.clone())),
                                    anchor: false
                                }
                            ]);
                            builder.row(markdown_vec![
                                Markdown::new_paragraph("Global Functions").code(),
                                Markdown::Link {
                                    text: Box::new(
                                        "Documents all the global functions present in the bindings"
                                    ),
                                    url: format!(
                                        "./{}/functions.md",
                                        linkify_filename(title.clone())
                                    ),
                                    anchor: false
                                }
                            ]);
                            builder.row(markdown_vec![
                                Markdown::new_paragraph("Globals").code(),
                                Markdown::Link {
                                    text: Box::new(
                                        "Documents all global variables present in the bindings"
                                    ),
                                    url: format!(
                                        "./{}/globals.md",
                                        linkify_filename(title.clone())
                                    ),
                                    anchor: false
                                }
                            ]);
                        });
                    }),
                }]
            }
            SectionData::InstancesSummary => {
                let instances = self.ladfile.globals.iter().collect::<Vec<_>>();
                let types_directory = PathBuf::from("./types");
                vec![SectionItem::InstancesSummary {
                    instances,
                    ladfile: self.ladfile,
                    types_directory,
                }]
            }
            SectionData::TypeSummary => {
                let types = self.ladfile.types.keys().collect::<Vec<_>>();
                vec![SectionItem::TypesSummary {
                    types,
                    types_directory: PathBuf::from("./types").to_string_lossy().to_string(),
                    ladfile: self.ladfile,
                }]
            }
            SectionData::FunctionSummary => {
                let associated_functions = self
                    .ladfile
                    .types
                    .iter()
                    .flat_map(|t| &t.1.associated_functions)
                    .collect::<HashSet<_>>();

                let non_associated_functions = self
                    .ladfile
                    .functions
                    .iter()
                    .filter_map(|f| (!associated_functions.contains(f.0)).then_some(f.1))
                    .collect();
                vec![
                    SectionItem::Markdown {
                        markdown: Box::new(|builder| {
                            builder.heading(2, "Non-Associated Functions");
                            builder.text("Global functions that are not associated with any type and callable from anywhere in the script.");
                        }),
                    },
                    SectionItem::FunctionsSummary {
                        functions: non_associated_functions,
                        functions_directory: "functions".to_owned(),
                    },
                ]
            }
            SectionData::TypeDetail { lad_type, .. } => {
                let functions = lad_type
                    .associated_functions
                    .iter()
                    .filter_map(|i| self.ladfile.functions.get(i))
                    .collect::<Vec<_>>();
                vec![
                    SectionItem::Layout {
                        layout: &lad_type.layout,
                        ladfile: self.ladfile,
                        types_directory: PathBuf::from("./"),
                    },
                    SectionItem::Description { lad_type },
                    SectionItem::Markdown {
                        markdown: Box::new(|builder| {
                            builder.heading(2, "Associated Functions");
                        }),
                    },
                    SectionItem::FunctionsSummary {
                        functions,
                        functions_directory: linkify_filename(self.title()),
                    },
                ]
            }
            SectionData::FunctionDetail {
                function,
                ref types_directory,
            } => {
                vec![SectionItem::FunctionDetails {
                    function,
                    ladfile: self.ladfile,
                    types_directory: types_directory.clone(),
                }]
            }
        }
    }
}

impl IntoMarkdown for Section<'_> {
    fn to_markdown(&self, builder: &mut MarkdownBuilder) {
        if self.is_code_heading() {
            builder.heading(1, Markdown::new_paragraph(self.title()).code());
        } else {
            builder.heading(1, self.title());
        }

        for item in self.section_items() {
            item.to_markdown(builder);
        }
    }
}

const NO_DOCS_STRING: &str = "No Documentation 🚧";

/// Items which combine markdown elements to build a section
pub enum SectionItem<'a> {
    Markdown {
        markdown: Box<dyn Fn(&mut MarkdownBuilder) + 'static>,
    },
    Layout {
        layout: &'a LadTypeLayout,
        ladfile: &'a LadFile,
        types_directory: PathBuf,
    },
    Description {
        lad_type: &'a LadTypeDefinition,
    },
    FunctionsSummary {
        functions: Vec<&'a LadFunction>,
        functions_directory: String,
    },
    FunctionDetails {
        function: &'a LadFunction,
        ladfile: &'a ladfile::LadFile,
        types_directory: PathBuf,
    },
    TypesSummary {
        types: Vec<&'a LadTypeId>,
        types_directory: String,
        ladfile: &'a ladfile::LadFile,
    },
    InstancesSummary {
        ladfile: &'a ladfile::LadFile,
        instances: Vec<(&'a Cow<'static, str>, &'a LadInstance)>,
        types_directory: PathBuf,
    },
}

impl std::fmt::Debug for SectionItem<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            SectionItem::Markdown { .. } => "Markdown",
            SectionItem::Layout { .. } => "Layout",
            SectionItem::Description { .. } => "Description",
            SectionItem::FunctionsSummary { .. } => "FunctionsSummary",
            SectionItem::FunctionDetails { .. } => "FunctionDetails",
            SectionItem::TypesSummary { .. } => "TypesSummary",
            SectionItem::InstancesSummary { .. } => "InstancesSummary",
        })
    }
}

impl IntoMarkdown for SectionItem<'_> {
    fn to_markdown(&self, builder: &mut MarkdownBuilder) {
        match self {
            SectionItem::Markdown { markdown } => (markdown)(builder),
            SectionItem::Layout {
                layout,
                ladfile,
                types_directory,
            } => {
                // process the variants here
                let opaque = layout.for_each_variant(
                    |v, _i| match v {
                        ladfile::LadVariant::TupleStruct { name, fields } => {
                            builder.heading(3, name.to_string()).list(
                                true,
                                fields
                                    .iter()
                                    .map(|f| {
                                        Markdown::Raw(print_type(
                                            ladfile,
                                            &f.type_,
                                            None,
                                            Some((types_directory.clone(), true)),
                                        ))
                                    })
                                    .collect(),
                            );
                        }
                        ladfile::LadVariant::Struct { name, fields } => {
                            builder.heading(3, name.to_string()).list(
                                false,
                                fields
                                    .iter()
                                    .map(|f| {
                                        markdown_vec![
                                            Markdown::new_paragraph(f.name.clone()).bold(),
                                            Markdown::new_paragraph(":"),
                                            Markdown::Raw(print_type(
                                                ladfile,
                                                &f.type_,
                                                None,
                                                Some((types_directory.clone(), true))
                                            ))
                                        ]
                                    })
                                    .collect(),
                            );
                        }
                        ladfile::LadVariant::Unit { name } => {
                            builder.heading(3, name.to_string());
                        }
                    },
                    "Opaque Type. 🔒",
                );

                if let Some(opaque) = opaque {
                    builder.text(opaque);
                }
            }
            SectionItem::Description {
                lad_type: description,
            } => {
                builder.heading(2, "Description").quote(Markdown::Raw(
                    description
                        .documentation
                        .as_deref()
                        .unwrap_or(NO_DOCS_STRING)
                        .to_owned(),
                ));
            }
            SectionItem::FunctionsSummary {
                functions,
                functions_directory: functions_path,
            } => {
                builder.text("For function details and documentation, click on the function link.");

                // make a table of functions as a quick reference, make them link to function details sub-sections
                builder.table(|builder| {
                    builder.headers(vec!["Function", "Summary"]);
                    for function in functions.iter() {
                        let first_col = function.identifier_with_overload().to_string();

                        // first line with content from documentation trimmed to 100 chars
                        let second_col = function
                            .documentation
                            .as_deref()
                            .map(|doc| markdown_substring(doc, 100))
                            .unwrap_or_else(|| NO_DOCS_STRING.to_string());

                        builder.row(markdown_vec![
                            Markdown::Link {
                                text: Box::new(first_col),
                                url: format!(
                                    "./{}/{}.md",
                                    functions_path,
                                    function.identifier_with_overload()
                                ),
                                anchor: false
                            },
                            Markdown::new_paragraph(second_col.to_string().replace("\n", " ")),
                        ]);
                    }
                });
            }
            SectionItem::TypesSummary {
                types,
                types_directory,
                ladfile,
            } => {
                builder.heading(2, "Available Types");
                builder.text("All registered reflect-able types which can be constructed and directly manipulated by scripts.");

                // make a table of types as a quick reference, make them link to type details sub-sections
                builder.table(|builder| {
                    builder.headers(vec!["Type", "Summary"]);
                    for type_ in types.iter() {
                        let printed_type_for_url = print_type(ladfile, *type_, None, None);
                        let printed_type_pretty =
                            print_type(ladfile, *type_, Some("Unknown"), None);

                        let documentation = ladfile.get_type_documentation(type_);

                        // first line with content from documentation trimmed to 100 chars
                        let second_col = documentation
                            .map(|doc| markdown_substring(doc, 100))
                            .unwrap_or_else(|| NO_DOCS_STRING.to_string());

                        let mut link_builder = MarkdownBuilder::new();
                        link_builder.tight_inline();
                        link_builder.link(
                            Markdown::new_paragraph(printed_type_pretty).code(),
                            format!(
                                "./{types_directory}/{}.md",
                                linkify_filename(printed_type_for_url)
                            ),
                        );

                        builder.row(markdown_vec![
                            link_builder,
                            Markdown::new_paragraph(second_col.replace("\n", " ")),
                        ]);
                    }
                });
            }
            SectionItem::InstancesSummary {
                instances,
                ladfile,
                types_directory,
            } => {
                builder.heading(2, "Global Values");
                builder.text("Global values that are accessible anywhere inside scripts. You should avoid naming conflicts with these and trying to overwrite or edit them.");
                // make a table of instances as a quick reference, make them link to instance details sub-sections

                // first build a non-static instance table
                let instances = instances
                    .iter()
                    .map(|(k, v)| {
                        let name = k.to_string();
                        let types_directory = types_directory.clone();
                        let escaped =
                            print_type(ladfile, &v.type_kind, None, Some((types_directory, true)));
                        (v.is_static, name, escaped)
                    })
                    .collect::<Vec<_>>();

                builder.heading(3, "Instances");
                builder.text("Instances containing actual accessible values.");
                builder.table(|builder| {
                    builder.headers(vec!["Instance", "Type"]);
                    for (_, name, instance) in instances.iter().filter(|(a, _, _)| !*a) {
                        builder.row(markdown_vec![
                            Markdown::new_paragraph(name).code(),
                            Markdown::Raw(instance.clone())
                        ]);
                    }
                });

                builder.heading(3, "Static Instances");
                builder.text("Static type references, existing for the purpose of typed static function calls.");
                builder.table(|builder| {
                    builder.headers(vec!["Instance", "Type"]);
                    for (_, name, instance) in instances.iter().filter(|(a, _, _)| *a) {
                        builder.row(markdown_vec![
                            Markdown::new_paragraph(name).code(),
                            Markdown::Raw(instance.clone())
                        ]);
                    }
                });
            }
            SectionItem::FunctionDetails {
                function,
                ladfile,
                types_directory,
            } => {
                // if the function takes in a FunctionCallContext argument, we notify that it's an impure function
                // which potentially tries to access anything in the world
                if function.arguments.iter().any(|a| {
                    matches!(
                        a.kind,
                        LadFieldOrVariableKind::Primitive(
                            ReflectionPrimitiveKind::FunctionCallContext
                        )
                    )
                }) {
                    builder.raw(
                    r#"
                        <div class="warning">
                            This function is impure, it might potentially try to access anything in the world.
                            If you are using it in the context of a script system, it might cause access errors.
                        </div>
                    "#.trim(),
                    );
                    builder.append("\n\n");
                }

                // we don't escape this, this is already markdown
                builder.quote(Markdown::Raw(
                    function
                        .documentation
                        .as_deref()
                        .unwrap_or(NO_DOCS_STRING)
                        .to_owned(),
                ));

                builder.heading(4, "Arguments");
                let headers = vec!["Name", "Type", "Documentation"];
                builder.table(|builder| {
                    builder.headers(headers.clone());
                    for (idx, arg) in function.arguments.iter().enumerate() {
                        build_lad_function_argument_row(
                            idx,
                            arg,
                            ladfile,
                            types_directory.clone(),
                            builder,
                        );
                    }
                });

                builder.heading(4, "Returns");
                builder.table(|builder| {
                    builder.headers(headers.clone());
                    build_lad_function_argument_row(
                        0,
                        &function.return_type,
                        ladfile,
                        types_directory.clone(),
                        builder,
                    )
                });
            }
        }
    }
}

fn build_lad_function_argument_row(
    idx: usize,
    arg: &LadArgument,
    ladfile: &LadFile,
    types_directory: PathBuf,
    builder: &mut TableBuilder,
) {
    // we exclude function call context as it's not something scripts pass down
    if matches!(
        arg.kind,
        LadFieldOrVariableKind::Primitive(ReflectionPrimitiveKind::FunctionCallContext)
    ) {
        return;
    }

    let types_directory = types_directory.to_owned();
    let escaped = print_type(ladfile, &arg.kind, None, Some((types_directory, false)));

    let arg_name = arg
        .name
        .as_ref()
        .cloned()
        .unwrap_or_else(|| Cow::Owned(format!("arg{idx}")));

    builder.row(markdown_vec![
        Markdown::new_paragraph(arg_name).bold(),
        Markdown::Raw(escaped),
        Markdown::Raw(
            arg.documentation
                .as_deref()
                .unwrap_or(NO_DOCS_STRING)
                .to_owned()
        )
    ]);
}