1
1
import { Document } from 'dgeni' ;
2
+ import { slugify } from 'markdown-toc' ;
2
3
3
- import { DaffDocExample } from '@daffodil/docs-utils' ;
4
+ import {
5
+ DaffDocExample ,
6
+ DaffDocTableOfContents ,
7
+ } from '@daffodil/docs-utils' ;
4
8
5
9
import { MARKDOWN_CODE_PROCESSOR_NAME } from '../../../processors/markdown' ;
6
10
import { FilterableProcessor } from '../../../utils/filterable-processor.type' ;
7
11
8
12
export const EXAMPLES_PROCESSOR_NAME = 'examples' ;
9
13
14
+ const genExamplesToc = ( examples : Array < DaffDocExample > ) : DaffDocTableOfContents =>
15
+ examples . map ( ( example ) => ( {
16
+ content : example . caption ,
17
+ lvl : 3 ,
18
+ slug : example . id ,
19
+ } ) ) ;
20
+
10
21
/**
11
22
* Adds subpackage entry point docs to the containing package doc.
12
23
*/
@@ -20,10 +31,20 @@ export class ExamplesProcessor implements FilterableProcessor {
20
31
$process ( docs : Array < Document > ) : Array < Document > {
21
32
return docs . map ( ( doc ) => {
22
33
if ( this . docTypes . includes ( doc . docType ) ) {
23
- doc . examples = doc . tags . tagsByName . get ( 'example' ) ?. map ( ( example ) : DaffDocExample => ( {
34
+ doc . examples = doc . tags . tagsByName . get ( 'example' ) ?. map ( ( example , i ) : DaffDocExample => ( {
35
+ id : slugify ( `${ doc . name } -example-${ i } ` ) ,
24
36
caption : example . caption ,
25
37
body : example . body ,
26
38
} ) ) || [ ] ;
39
+ doc . tableOfContents = doc . examples . length > 0 ? [
40
+ {
41
+ content : 'Examples' ,
42
+ lvl : 2 ,
43
+ // TODO: add doc-specific prefix
44
+ slug : 'examples' ,
45
+ } ,
46
+ ...genExamplesToc ( doc . examples ) ,
47
+ ] : [ ] ;
27
48
}
28
49
return doc ;
29
50
} ) ;
0 commit comments