11import { Docs } from '@codama/nodes' ;
2- import { BaseFragment , mapFragmentContent , Path } from '@codama/renderers-core' ;
2+ import { BaseFragment , createFragmentTemplate , mapFragmentContent , Path } from '@codama/renderers-core' ;
33
4- import { addToImportMap , getImportMapLinks , ImportMap , importMap , mergeImportMaps , PathOverrides } from './importMap' ;
4+ import {
5+ addToImportMap ,
6+ createImportMap ,
7+ getImportMapLinks ,
8+ ImportMap ,
9+ mergeImportMaps ,
10+ PathOverrides ,
11+ } from './importMap' ;
512
613export type Fragment = BaseFragment & Readonly < { imports : ImportMap } > ;
714
8- export function fragment ( content : string ) : Fragment {
9- return Object . freeze ( { content, imports : importMap ( ) } ) ;
15+ function createFragment ( content : string ) : Fragment {
16+ return Object . freeze ( { content, imports : createImportMap ( ) } ) ;
1017}
1118
12- export function mergeFragments ( fragments : Fragment [ ] , mergeContent : ( contents : string [ ] ) => string ) {
19+ function isFragment ( value : unknown ) : value is Fragment {
20+ return typeof value === 'object' && value !== null && 'content' in value ;
21+ }
22+
23+ export function fragment ( template : TemplateStringsArray , ...items : unknown [ ] ) : Fragment {
24+ return createFragmentTemplate ( template , items , isFragment , mergeFragments ) ;
25+ }
26+
27+ export function mergeFragments (
28+ fragments : ( Fragment | undefined ) [ ] ,
29+ mergeContent : ( contents : string [ ] ) => string ,
30+ ) : Fragment {
31+ const filteredFragments = fragments . filter ( ( f ) : f is Fragment => f !== undefined ) ;
1332 return Object . freeze ( {
14- content : mergeContent ( fragments . map ( fragment => fragment . content ) ) ,
15- imports : mergeImportMaps ( fragments . map ( fragment => fragment . imports ) ) ,
33+ content : mergeContent ( filteredFragments . map ( fragment => fragment . content ) ) ,
34+ imports : mergeImportMaps ( filteredFragments . map ( fragment => fragment . imports ) ) ,
1635 } ) ;
1736}
1837
@@ -21,12 +40,12 @@ export function addFragmentImports(fragment: Fragment, path: Path, names: string
2140}
2241
2342export function getFrontmatterFragment ( title : string , description : string ) : Fragment {
24- return fragment ( `---\ntitle: ${ title } \ndescription: ${ description } \n---` ) ;
43+ return fragment `---\ntitle: ${ title } \ndescription: ${ description } \n---` ;
2544}
2645
2746export function getTitleAndDescriptionFragment ( title : string , docs ?: Docs ) : Fragment {
2847 return mergeFragments (
29- [ fragment ( `# ${ title } ` ) , ... ( docs && docs . length > 0 ? [ fragment ( docs . join ( '\n' ) ) ] : [ ] ) ] ,
48+ [ fragment `# ${ title } ` , docs && docs . length > 0 ? fragment ` ${ docs . join ( '\n' ) } ` : undefined ] ,
3049 cs => cs . join ( '\n\n' ) ,
3150 ) ;
3251}
@@ -36,7 +55,7 @@ export function getCodeBlockFragment(code: Fragment, language: string = ''): Fra
3655}
3756
3857export function getTableFragment ( headers : ( Fragment | string ) [ ] , rows : ( Fragment | string ) [ ] [ ] ) : Fragment {
39- const toFragment = ( cell : Fragment | string ) => ( typeof cell === 'string' ? fragment ( cell ) : cell ) ;
58+ const toFragment = ( cell : Fragment | string ) => ( typeof cell === 'string' ? createFragment ( cell ) : cell ) ;
4059 const headerFragments = headers . map ( toFragment ) ;
4160 const rowFragments = rows . map ( row => row . map ( toFragment ) ) ;
4261 const colWidths = headerFragments . map ( ( header , colIndex ) =>
@@ -46,7 +65,7 @@ export function getTableFragment(headers: (Fragment | string)[], rows: (Fragment
4665 const mergeCells = ( fs : Fragment [ ] ) => mergeFragments ( fs , cs => cs . join ( ' | ' ) ) ;
4766 const lines = [
4867 mergeCells ( headerFragments . map ( padCells ) ) ,
49- mergeCells ( headerFragments . map ( ( _ , colIndex ) => fragment ( '-' . repeat ( colWidths [ colIndex ] ) ) ) ) ,
68+ mergeCells ( headerFragments . map ( ( _ , colIndex ) => createFragment ( '-' . repeat ( colWidths [ colIndex ] ) ) ) ) ,
5069 ...rowFragments . map ( row => mergeCells ( row . map ( padCells ) ) ) ,
5170 ] ;
5271 return mergeFragments (
@@ -56,12 +75,12 @@ export function getTableFragment(headers: (Fragment | string)[], rows: (Fragment
5675}
5776
5877export function getCommentFragment ( lines : string [ ] ) : Fragment {
59- return fragment ( `<!--\n${ lines . join ( '\n' ) } \n-->` ) ;
78+ return fragment `<!--\n${ lines . join ( '\n' ) } \n-->` ;
6079}
6180
6281export function getPageFragment ( fragments : Fragment [ ] , pathOverrides : PathOverrides = { } ) : Fragment {
6382 const page = mergeFragments ( fragments , cs => cs . join ( '\n\n' ) ) ;
6483 const links = getImportMapLinks ( page . imports , pathOverrides ) ;
6584 if ( links . length === 0 ) return page ;
66- return mapFragmentContent ( page , c => `${ c } \n\n## See also\n\n${ links . join ( '\n' ) } ` ) ;
85+ return fragment `${ page } \n\n## See also\n\n${ links . join ( '\n' ) } ` ;
6786}
0 commit comments