Skip to content

Commit

Permalink
Render updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Javadyakuza committed Aug 11, 2023
1 parent 1524ac6 commit d4a3b86
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export async function main(
config.templates
);
const site = buildSite(builds, config, templates.properties ?? {});
const renderedSite = render(site, templates, config.collapseNewlines);
const renderedSite = render(
site,
templates,
config.sourcesDir!,
config.collapseNewlines
);
replaceAdocReferences(renderedSite, config.sourcesDir);
for (const { id, contents } of renderedSite) {
const outputFile = path.resolve(config.root, config.outputDir, id);
Expand Down
59 changes: 38 additions & 21 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Handlebars, { RuntimeOptions } from 'handlebars';
import { Site, Page, DocItemWithContext, DOC_ITEM_CONTEXT } from './site';
import { Templates } from './templates';
import { itemType } from './utils/item-type';
import fs from 'fs';
import Handlebars, { RuntimeOptions } from "handlebars";
import { Site, Page, DocItemWithContext, DOC_ITEM_CONTEXT } from "./site";
import { Templates } from "./templates";
import { itemType } from "./utils/item-type";
import fs from "fs";
import { join } from "path";

export interface RenderedPage {
id: string;
Expand All @@ -15,13 +16,18 @@ interface TemplateOptions {
};
}

export function render(site: Site, templates: Templates, collapseNewlines?: boolean): RenderedPage[] {
const renderPage = buildRenderer(templates);
export function render(
site: Site,
templates: Templates,
srcDir: string,
collapseNewlines?: boolean
): RenderedPage[] {
const renderPage = buildRenderer(templates, srcDir);
const renderedPages: RenderedPage[] = [];
for (const page of site.pages) {
let contents = renderPage(page, { data: { site } });
if (collapseNewlines) {
contents = contents.replace(/\n{3,}/g, '\n\n');
contents = contents.replace(/\n{3,}/g, "\n\n");
}
renderedPages.push({
id: page.id,
Expand All @@ -31,11 +37,14 @@ export function render(site: Site, templates: Templates, collapseNewlines?: bool
return renderedPages;
}

export const itemPartialName = (item: DocItemWithContext) => itemType(item).replace(/ /g, '-').toLowerCase();
export const itemPartialName = (item: DocItemWithContext) =>
itemType(item).replace(/ /g, "-").toLowerCase();

function itemPartial(item: DocItemWithContext, options?: RuntimeOptions) {
if (!item.__item_context) {
throw new Error(`Partial 'item' used in unsupported context (not a doc item)`);
throw new Error(
`Partial 'item' used in unsupported context (not a doc item)`
);
}
const partial = options?.partials?.[itemPartialName(item)];
if (!partial) {
Expand All @@ -44,22 +53,28 @@ function itemPartial(item: DocItemWithContext, options?: RuntimeOptions) {
return partial(item, options);
}

function readmeHelper(H: typeof Handlebars, path: string, opts: RuntimeOptions) {
function readmeHelper(
H: typeof Handlebars,
path: string,
opts: RuntimeOptions,
srcDir: string
) {
const items: DocItemWithContext[] = opts.data.root.items;
const renderedItems = Object.fromEntries(
items.map(item => [
items.map((item) => [
item.name,
new H.SafeString(
H.compile('{{>item}}')(item, opts),
),
]),
new H.SafeString(H.compile("{{>item}}")(item, opts)),
])
);
return new H.SafeString(
H.compile(fs.readFileSync(path, 'utf8'))(renderedItems, opts),
H.compile(fs.readFileSync(join(srcDir, path), "utf8"))(renderedItems, opts)
);
}

function buildRenderer(templates: Templates): (page: Page, options: TemplateOptions) => string {
function buildRenderer(
templates: Templates,
srcDir: string
): (page: Page, options: TemplateOptions) => string {
const pageTemplate = templates.partials?.page;
if (pageTemplate === undefined) {
throw new Error(`Missing 'page' template`);
Expand All @@ -75,13 +90,15 @@ function buildRenderer(templates: Templates): (page: Page, options: TemplateOpti
});
}

H.registerHelper('readme', (path: string, opts: RuntimeOptions) => readmeHelper(H, path, opts));
H.registerHelper("readme", (path: string, opts: RuntimeOptions) =>
readmeHelper(H, path, opts, srcDir)
);

for (const [name, fn] of Object.entries(templates.helpers ?? {})) {
H.registerHelper(name, fn);
}

H.registerPartial('item', itemPartial);
H.registerPartial("item", itemPartial);

return H.compile('{{>page}}');
return H.compile("{{>page}}");
}

0 comments on commit d4a3b86

Please sign in to comment.