@@ -5,10 +5,19 @@ import { getSetting } from "~/utils/extensionSettings";
55
66export const DISCOURSE_GRAPH_PROP_NAME = "discourse-graph" ;
77
8+ const SANE_ROLE_NAME_RE = new RegExp ( / ^ [ \w \- ] * $ / ) ;
9+
810const strictQueryForReifiedBlocks = async (
911 parameterUids : Record < string , string > ,
1012) : Promise < string | null > => {
1113 const paramsAsSeq = Object . entries ( parameterUids ) ;
14+ // validate parameter names
15+ if (
16+ Object . keys ( parameterUids ) . filter ( ( k ) => ! k . match ( SANE_ROLE_NAME_RE ) ) . length
17+ )
18+ throw new Error (
19+ `invalid parameter names in ${ Object . keys ( parameterUids ) . join ( ", " ) } ` ,
20+ ) ;
1221 const query = `[:find ?u ?d
1322 :in $ ${ paramsAsSeq . map ( ( [ k ] ) => "?" + k ) . join ( " " ) }
1423 :where [?s :block/uid ?u] [?s :block/props ?p] [(get ?p :${ DISCOURSE_GRAPH_PROP_NAME } ) ?d]
@@ -72,7 +81,7 @@ const createReifiedBlock = async ({
7281const RELATION_PAGE_TITLE = "roam/js/discourse-graph/relations" ;
7382let relationPageUid : string | undefined = undefined ;
7483
75- const getRelationPageUid = async ( ) : Promise < string > => {
84+ const getOrCreateRelationPageUid = async ( ) : Promise < string > => {
7685 if ( relationPageUid === undefined ) {
7786 relationPageUid = getPageUidByPageTitle ( RELATION_PAGE_TITLE ) ;
7887 if ( relationPageUid === "" ) {
@@ -82,8 +91,16 @@ const getRelationPageUid = async (): Promise<string> => {
8291 return relationPageUid ;
8392} ;
8493
94+ export const getExistingRelationPageUid = ( ) : string | undefined => {
95+ if ( relationPageUid === undefined ) {
96+ const uid = getPageUidByPageTitle ( RELATION_PAGE_TITLE ) ;
97+ if ( uid !== "" ) relationPageUid = uid ;
98+ }
99+ return relationPageUid ;
100+ } ;
101+
85102export const countReifiedRelations = async ( ) : Promise < number > => {
86- const pageUid = await getRelationPageUid ( ) ;
103+ const pageUid = getExistingRelationPageUid ( ) ;
87104 if ( pageUid === undefined ) return 0 ;
88105 const r = await window . roamAlphaAPI . data . async . q (
89106 `[:find (count ?c) :where [?p :block/children ?c] [?p :block/uid "${ pageUid } "]]` ,
@@ -103,7 +120,7 @@ export const createReifiedRelation = async ({
103120 const authorized = getSetting ( "use-reified-relations" ) ;
104121 if ( authorized ) {
105122 return await createReifiedBlock ( {
106- destinationBlockUid : await getRelationPageUid ( ) ,
123+ destinationBlockUid : await getOrCreateRelationPageUid ( ) ,
107124 schemaUid : relationBlockUid ,
108125 parameterUids : {
109126 sourceUid,
0 commit comments