Skip to content

Commit

Permalink
fix missing properties
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Sep 29, 2022
1 parent 455d751 commit da4bd59
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ export function buildSite(builds: Build[], siteConfig: SiteConfig, properties: P

const page = assignIfIncludedSource(assign, topLevelItem, file, siteConfig);

const withContext = Object.assign(topLevelItem, {
__item_context: { page, item: topLevelItem as DocItemWithContext, file, build },
});
const withContext = defineContext(topLevelItem, build, file, page);
defineProperties(withContext, properties);

if (isNewFile && page !== undefined) {
(pages[page] ??= []).push(withContext);
Expand All @@ -91,26 +90,32 @@ export function buildSite(builds: Build[], siteConfig: SiteConfig, properties: P
if (!isDocItem(item)) continue;
if (isNewFile && page !== undefined) items.push(item as DocItemWithContext);
const contract = topLevelItem.nodeType === 'ContractDefinition' ? topLevelItem : undefined;
const __item_context: DocItemContext = { page, item: item as DocItemWithContext, contract, file, build };
Object.assign(item, { __item_context });
const withContext = defineContext(item, build, file, page, contract);
defineProperties(withContext, properties);
}
}
}
}

for (const item of items) {
for (const [prop, fn] of Object.entries(properties)) {
const original: unknown = (item as any)[prop];
defineGetterMemoized(item as any, prop, () => fn(item.__item_context, original));
}
}

return {
items,
pages: Object.entries(pages).map(([id, pageItems]) => ({ id, items: pageItems })),
};
}

function defineContext(item: DocItem, build: BuildContext, file: SourceUnit, page?: string, contract?: ContractDefinition): DocItemWithContext {
return Object.assign(item, {
[DOC_ITEM_CONTEXT]: { build, file, contract, page, item: item as DocItemWithContext },
});
}

function defineProperties(item: DocItemWithContext, properties: Properties) {
for (const [prop, fn] of Object.entries(properties)) {
const original: unknown = (item as any)[prop];
defineGetterMemoized(item as any, prop, () => fn(item.__item_context, original));
}
}

function assignIfIncludedSource(
assign: PageAssigner,
item: DocItem,
Expand Down

0 comments on commit da4bd59

Please sign in to comment.