@@ -8,16 +8,8 @@ import { DatabaseConfig, DatabaseConfigRenderPages } from './SyncConfig';
8
8
9
9
const debug = require ( "debug" ) ( "properties" ) ;
10
10
11
- export interface ParsedProperties {
12
- title : string | null ;
13
- category : string | null ;
14
- order : number | undefined ;
15
- properties : Record < string , any > ;
16
- keys : Map < string , string > ;
17
- }
18
-
19
11
export class PropertiesParser {
20
- constructor ( private readonly richText : RichTextRenderer ) { }
12
+ constructor ( private readonly richText : RichTextRenderer ) { }
21
13
22
14
public async parsePageProperties (
23
15
page : Page ,
@@ -60,14 +52,30 @@ export class PropertiesParser {
60
52
61
53
public async parseProperties ( page : Page , config : DatabaseConfig ) {
62
54
/**
63
- * Design: we always lookup the properties opn the page object itself.
55
+ * Design: we always lookup the properties on the page object itself.
64
56
* This way we only parse properties once and avoid any problems coming from
65
57
* e.g. category properties being filtered via include filters.
66
58
*/
59
+
60
+ /**
61
+ * Terminology:
62
+ *
63
+ * property: Notion API property name
64
+ * key: slugified Notion API property name, used to later build frontmatter
65
+ * value: Notion API property value
66
+ */
67
+
68
+ /**
69
+ * A record of key->value
70
+ */
67
71
const properties : Record < string , any > = { } ;
72
+ /**
73
+ * A map of proprety -> key
74
+ */
68
75
const keys = new Map < string , string > ( ) ;
69
76
70
77
let title : string | null = null ;
78
+ let titleProperty : string | null = null ;
71
79
let category : string | null = null ;
72
80
let order : number | undefined = undefined ;
73
81
@@ -89,6 +97,7 @@ export class PropertiesParser {
89
97
90
98
if ( value . type === "title" ) {
91
99
title = parsedValue ;
100
+ titleProperty = name ;
92
101
}
93
102
94
103
if ( categoryProperty && name === categoryProperty ) {
@@ -99,14 +108,24 @@ export class PropertiesParser {
99
108
order = parsedValue ;
100
109
}
101
110
}
111
+
112
+ if ( ! titleProperty ) {
113
+ throw this . errorMissingRequiredProperty ( "of type 'title'" , page ) ;
114
+ }
115
+
116
+ // no explicit ordering specified, so we make sure to put the title property first
117
+ const includes = config . properties ?. include
118
+ || [ titleProperty , ...Array . from ( keys . keys ( ) ) . filter ( x => x != titleProperty ) ] ;
119
+
120
+
102
121
return {
103
122
title,
104
123
category,
105
124
order,
106
125
properties,
107
126
keys : PropertiesParser . filterIncludedKeys (
108
- config . properties ?. include ,
109
- keys
127
+ keys ,
128
+ includes
110
129
) ,
111
130
} ;
112
131
}
@@ -155,8 +174,8 @@ export class PropertiesParser {
155
174
}
156
175
157
176
public static filterIncludedKeys (
177
+ keys : Map < string , string > ,
158
178
includes : string [ ] | undefined ,
159
- keys : Map < string , string >
160
179
) : Map < string , string > {
161
180
if ( ! includes ) {
162
181
return keys ;
0 commit comments