Skip to content

Commit ea5da73

Browse files
committed
add readme helper
1 parent 050a74e commit ea5da73

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/render.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Handlebars, { RuntimeOptions } from 'handlebars';
22
import { Site, Page, DocItemWithContext, DOC_ITEM_CONTEXT } from './site';
33
import { Templates } from './templates';
44
import { itemType } from './utils/item-type';
5+
import fs from 'fs';
56

67
export interface RenderedPage {
78
id: string;
@@ -43,6 +44,21 @@ function itemPartial(item: DocItemWithContext, options?: RuntimeOptions) {
4344
return partial(item, options);
4445
}
4546

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+
4662
function buildRenderer(templates: Templates): (page: Page, options: TemplateOptions) => string {
4763
const pageTemplate = templates.partials?.page;
4864
if (pageTemplate === undefined) {
@@ -59,6 +75,8 @@ function buildRenderer(templates: Templates): (page: Page, options: TemplateOpti
5975
});
6076
}
6177

78+
H.registerHelper('readme', (path: string, opts: RuntimeOptions) => readmeHelper(H, path, opts));
79+
6280
for (const [name, fn] of Object.entries(templates.helpers ?? {})) {
6381
H.registerHelper(name, fn);
6482
}

0 commit comments

Comments
 (0)