Skip to content

Commit 78575ae

Browse files
feat: publish frontmatter in the sync() result
this makes it easier for consumers to write index files containing the actual frontmatter data which was built from the frontmatterBuilder functions also align the frontmatter building for table entries so that it works the same as for pages
1 parent a37e840 commit 78575ae

12 files changed

+34
-31
lines changed

src/DatabaseEntryRenderer.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { Page } from "@notionhq/client/build/src/api-types";
2+
import { DatabaseConfigRenderTable } from ".";
23

34
import { PropertiesParser } from "./PropertiesParser";
45
import { RenderDatabaseEntryTask } from "./RenderDatabaseEntryTask";
56

67
export class DatabaseEntryRenderer {
78
constructor(private readonly propertiesParser: PropertiesParser) {}
89

9-
async renderEntry(page: Page): Promise<RenderDatabaseEntryTask> {
10+
async renderEntry(
11+
page: Page,
12+
config: DatabaseConfigRenderTable
13+
): Promise<RenderDatabaseEntryTask> {
1014
const props = await this.propertiesParser.parsePageProperties(page);
15+
const frontmatterProperties = config.entries?.frontmatterBuilder(props);
1116

1217
return {
1318
properties: props,
19+
frontmatter: frontmatterProperties,
1420
};
1521
}
1622
}

src/DatabasePageRenderer.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ export class DatabasePageRenderer {
3434
// Design: all the rendering performance could be greatly enhanced writing directly to output streams instead
3535
// of concatenating all in memory. OTOH naively concatenatic strings is straightforward, easier to debug and rendering
3636
// performance probably not the bottleneck compared to the IO cost of notion API invocations.
37+
const frontmatterProperties = config.pages.frontmatterBuilder(props);
3738

3839
return {
3940
file,
41+
frontmatter: frontmatterProperties,
4042
properties: props,
4143
render: async () => {
4244
const context = new RenderingLoggingContext(page.url, file);
@@ -51,10 +53,7 @@ export class DatabasePageRenderer {
5153
try {
5254
const assetWriter = new AssetWriter(destDir);
5355

54-
const frontmatter = this.frontmatterRenderer.renderFrontmatter(
55-
props,
56-
config
57-
);
56+
const frontmatter = this.frontmatterRenderer.renderFrontmatter(frontmatterProperties);
5857
const body = await this.bodyRenderer.renderBody(
5958
page,
6059
assetWriter,

src/DatabaseViewRenderer.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe("DatabaseViewRenderer", () => {
2828
];
2929

3030
const result = sut.renderViews(entries, {
31-
entries: { emitToIndex: false },
3231
renderAs: "table",
3332
views: [
3433
{
@@ -72,7 +71,6 @@ describe("DatabaseViewRenderer", () => {
7271
];
7372

7473
const result = sut.renderViews(entries, {
75-
entries: { emitToIndex: false },
7674
renderAs: "table",
7775
views: [
7876
{

src/DeferredRenderer.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ export class DeferredRenderer {
6262
page: Page,
6363
config: DatabaseConfigRenderTable
6464
): Promise<RenderDatabaseEntryTask> {
65-
const task = await this.entryRenderer.renderEntry(page);
65+
const task = await this.entryRenderer.renderEntry(page, config);
6666

6767
// entries are complete the moment they are retrieved, there's no more deferred processing necessary on them
6868
// also there should be no duplicate entries, so we do not cache/lookup any of them
69-
if (config.entries.emitToIndex) {
69+
if (task.frontmatter) {
7070
this.renderedEntries.push(task);
7171
}
7272

@@ -103,15 +103,15 @@ export class DeferredRenderer {
103103
).map((x) => ({
104104
file: x.file,
105105
meta: x.properties.meta,
106-
properties: x.properties.properties,
106+
frontmatter: x.frontmatter,
107107
}));
108108

109109
const entries: RenderedDatabaseEntry[] = this.renderedEntries.map((x) => ({
110110
meta: {
111111
id: x.properties.meta.id,
112112
url: x.properties.meta.url,
113113
},
114-
properties: x.properties.properties,
114+
frontmatter: x.frontmatter,
115115
}));
116116

117117
return pages.concat(entries);

src/FrontmatterRenderer.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import * as yaml from 'js-yaml';
2-
import { DatabaseConfigRenderPages } from '.';
3-
4-
import { DatabasePageProperties } from './DatabasePageProperties';
52

63
export class FrontmatterRenderer {
74
constructor() {}
85

9-
public renderFrontmatter(props: DatabasePageProperties, config: DatabaseConfigRenderPages) {
10-
const obj = config.pages.frontmatterBuilder(props);
11-
6+
public renderFrontmatter(obj: Record<string, any>) {
127
const frontmatter = `---\n${yaml.dump(obj)}---\n\n`;
138

149
return frontmatter;

src/RenderDatabaseEntryTask.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { DatabasePageProperties } from "./DatabasePageProperties";
22

33
export interface RenderDatabaseEntryTask {
44
properties: DatabasePageProperties
5+
frontmatter?: Record<string, any>
56
// note: there's nothing to do to render an individual database entry, they are always rendered as part of tables
67
}

src/RenderDatabasePageTask.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { RenderDatabaseEntryTask } from "./RenderDatabaseEntryTask";
22

33
export interface RenderDatabasePageTask extends RenderDatabaseEntryTask {
44
file: string;
5+
frontmatter: Record<string, any>
56
render: () => Promise<any>;
67
}

src/RenderedDatabaseEntry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { DatabaseEntryMeta } from './DatabaseEntryMeta';
22

33
export interface RenderedDatabaseEntry {
44
meta: DatabaseEntryMeta,
5-
properties: Map<string, any>;
5+
frontmatter?: Record<string, any>;
66
}

src/RenderedDatabasePage.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import { RenderedDatabaseEntry } from './RenderedDatabaseEntry';
44
export interface RenderedDatabasePage extends RenderedDatabaseEntry {
55
meta: DatabasePageMeta;
66
file: string;
7+
frontmatter: Record<string, any>;
78
}

src/SyncConfig.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ export interface DatabaseConfigRenderTable extends DatabaseConfigBase {
5252
*/
5353
views?: DatabaseView[];
5454

55-
entries: {
55+
entries?: {
5656
/**
57-
* Controls whether to emit database entries to the index of rendered pages/entries
57+
* Optional: Build frontmatter onject for index entries.
58+
* If omitted, no index entries will be rendered for this table
5859
*/
59-
emitToIndex: boolean;
60+
frontmatterBuilder: (props: DatabasePageProperties) => Record<string, any>;
6061
};
6162
}
6263

src/config.ts

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ export function lookupDatabaseConfig(
1616
};
1717
const defaultDbConfig: DatabaseConfigRenderTable = {
1818
renderAs: "table",
19-
entries: {
20-
emitToIndex: false,
21-
},
2219
};
2320

2421
const fallbackDbConfig: DatabaseConfig =

src/index.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
export { RenderedDatabaseEntry } from './RenderedDatabaseEntry';
2-
export { RenderedDatabasePage } from './RenderedDatabasePage';
3-
export { sync } from './sync';
4-
export { slugify } from './slugify';
1+
export { RenderedDatabaseEntry } from "./RenderedDatabaseEntry";
2+
export { RenderedDatabasePage } from "./RenderedDatabasePage";
3+
export { sync } from "./sync";
4+
export { slugify } from "./slugify";
55
export {
6-
DatabaseConfig, DatabaseConfigRenderPages, DatabaseConfigRenderTable, SyncConfig
7-
} from './SyncConfig';
8-
6+
DatabaseConfig,
7+
DatabaseConfigRenderPages,
8+
DatabaseConfigRenderTable,
9+
SyncConfig,
10+
} from "./SyncConfig";
11+
export { DatabasePageProperties } from "./DatabasePageProperties";
12+
export { DatabasePageMeta } from "./DatabasePageMeta";

0 commit comments

Comments
 (0)