@@ -2,6 +2,7 @@ import Handlebars, { RuntimeOptions } from 'handlebars';
2
2
import { Site , Page , DocItemWithContext , DOC_ITEM_CONTEXT } from './site' ;
3
3
import { Templates } from './templates' ;
4
4
import { itemType } from './utils/item-type' ;
5
+ import fs from 'fs' ;
5
6
6
7
export interface RenderedPage {
7
8
id : string ;
@@ -43,6 +44,21 @@ function itemPartial(item: DocItemWithContext, options?: RuntimeOptions) {
43
44
return partial ( item , options ) ;
44
45
}
45
46
47
+ function readmeHelper ( H : typeof Handlebars , path : string , opts : RuntimeOptions ) {
48
+ const items : DocItemWithContext [ ] = opts . data . root . items ;
49
+ const renderedItems = Object . fromEntries (
50
+ items . map ( item => [
51
+ item . name ,
52
+ new H . SafeString (
53
+ H . compile ( '{{>item}}' ) ( item , opts ) ,
54
+ ) ,
55
+ ] ) ,
56
+ ) ;
57
+ return new H . SafeString (
58
+ H . compile ( fs . readFileSync ( path , 'utf8' ) ) ( renderedItems , opts ) ,
59
+ ) ;
60
+ }
61
+
46
62
function buildRenderer ( templates : Templates ) : ( page : Page , options : TemplateOptions ) => string {
47
63
const pageTemplate = templates . partials ?. page ;
48
64
if ( pageTemplate === undefined ) {
@@ -59,6 +75,8 @@ function buildRenderer(templates: Templates): (page: Page, options: TemplateOpti
59
75
} ) ;
60
76
}
61
77
78
+ H . registerHelper ( 'readme' , ( path : string , opts : RuntimeOptions ) => readmeHelper ( H , path , opts ) ) ;
79
+
62
80
for ( const [ name , fn ] of Object . entries ( templates . helpers ?? { } ) ) {
63
81
H . registerHelper ( name , fn ) ;
64
82
}
0 commit comments