Skip to content

Commit 1f406cc

Browse files
feat: add options to explicitly control Database page categories
1 parent a46cc57 commit 1f406cc

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

src/DatabasePageRenderer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ export class DatabasePageRenderer {
1111
) {}
1212

1313
async renderDatabase(databaseId: string): Promise<Database> {
14-
const dbConfig = this.config[databaseId] || {
15-
parentCategory: "",
14+
const dbConfig: DatabaseConfig = this.config[databaseId] || {
15+
outSubDir: "",
16+
pageCategoryValuePrefix: "",
1617
properties: {
1718
category: "category",
1819
},

src/PageRenderer.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { DatabaseConfig } from "./SyncConfig";
1111
import { PropertiesParser } from "./PropertiesParser";
1212
import { logger } from "./logger";
1313

14+
const debug = require("debug")("page");
15+
1416
const fs = fsc.promises;
1517

1618
export class PageRenderer {
@@ -25,12 +27,19 @@ export class PageRenderer {
2527
const props = this.propertiesParser.filter(config, parsed);
2628

2729
const name = props.values["name"];
30+
if (!name) {
31+
this.throwMissingRequiredProperty("name", page);
32+
}
33+
34+
const category = props.values[props.keys.get(config.properties.category)!!];
35+
if (category) {
36+
this.throwMissingRequiredProperty(config.properties.category, page);
37+
}
38+
2839
const nameSlug = slugify(name);
29-
const categorySlug =
30-
config.parentCategory + slugify(props.values["category"]);
40+
const categorySlug = config.pageCategoryValuePrefix + slugify(category);
3141

32-
const dir = `docs/${categorySlug}`;
33-
const file = `${dir}/${nameSlug}.md`;
42+
const file = `${config.outDir}/${nameSlug}.md`;
3443

3544
// Design: all the rendering performance could be greatly enhanced writing directly to output streams instead
3645
// of concatenating all in memory. OTOH naively concatenatic strings is straightforward, easier to debug and rendering
@@ -42,7 +51,7 @@ export class PageRenderer {
4251
file,
4352
properties: props,
4453
render: async () => {
45-
const assetWriter = new AssetWriter(dir);
54+
const assetWriter = new AssetWriter(config.outDir);
4655

4756
const frontmatter = this.frontmatterRenderer.renderFrontmatter(
4857
props.values
@@ -53,11 +62,18 @@ export class PageRenderer {
5362
assetWriter
5463
);
5564

56-
await fs.mkdir(dir, { recursive: true });
65+
await fs.mkdir(config.outDir, { recursive: true });
5766
await fs.writeFile(file, frontmatter + body);
5867

5968
logger.info("wrote: " + file);
6069
},
6170
};
6271
}
72+
73+
private throwMissingRequiredProperty(propertyName: string, page: Page) {
74+
const msg = `Page ${page.url} is missing required property ${propertyName}`;
75+
debug(msg + "\n%O", page);
76+
77+
throw new Error(msg);
78+
}
6379
}

src/SyncConfig.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export interface SyncConfig {
88
cmsDatabaseId: string;
99

1010
/**
11-
* The output directory where the sync will place pages, e.g. "docs/"
11+
* The output directory where the sync will place pages.
12+
*
13+
* Example: "docs/"
1214
*/
1315
outDir: string;
1416

@@ -27,15 +29,41 @@ export interface SyncConfig {
2729

2830
export interface DatabaseConfig {
2931
/**
30-
* Name of the parent category to use
32+
* The output directory where the sync will place pages of this database.
33+
*
34+
* Example: "docs/mydb/"
3135
*/
32-
parentCategory: string;
36+
outDir: string;
37+
38+
/**
39+
* The prefix to apply to the category value of all pages.
40+
* This is useful to create a unique category name for all pages of this database.
41+
*
42+
* Example: "mydb/"
43+
*/
44+
pageCategoryValuePrefix: string;
45+
3346
/**
3447
* Notion API https://developers.notion.com/reference/post-database-query#post-database-query-sort
3548
*/
3649
sorts?: Sort[];
50+
51+
/**
52+
* Configuration options for Notion API page properties
53+
*/
3754
properties: {
55+
/**
56+
* The Notion API page property that provides the value to use for the markdown page category.
57+
* This will be prefixed by DatabaseConfig.pageCategoryValuePrefix
58+
*
59+
* Example: "Cluster"
60+
*/
3861
category: string;
62+
63+
/**
64+
* A whitelist of Notion API page property names to include in the markdown page properties.
65+
* Use this to select properties for export and control their ordering in rendered tables.
66+
*/
3967
include?: string[];
4068
};
4169
}

0 commit comments

Comments
 (0)