@@ -5,15 +5,23 @@ import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTit
55import getSubTree from "roamjs-components/util/getSubTree" ;
66import discourseNodeFormatToDatalog from "./discourseNodeFormatToDatalog" ;
77import conditionToDatalog , {
8+ getTitleDatalog ,
89 registerDatalogTranslator ,
910} from "./conditionToDatalog" ;
1011import { ANY_RELATION_REGEX } from "./deriveDiscourseNodeAttribute" ;
11- import getDiscourseNodes , { excludeDefaultNodes } from "./getDiscourseNodes" ;
12+ import getDiscourseNodes , {
13+ excludeDefaultNodes ,
14+ type DiscourseNode ,
15+ } from "./getDiscourseNodes" ;
1216import getDiscourseRelations from "./getDiscourseRelations" ;
1317import matchDiscourseNode from "./matchDiscourseNode" ;
1418import replaceDatalogVariables from "./replaceDatalogVariables" ;
1519import parseQuery from "./parseQuery" ;
1620import { fireQuerySync , getWhereClauses } from "./fireQuery" ;
21+ import { toVar } from "./compileDatalog" ;
22+
23+ const hasTag = ( node : DiscourseNode ) : node is DiscourseNode & { tag : string } =>
24+ ! ! node . tag ;
1725
1826const collectVariables = (
1927 clauses : ( DatalogClause | DatalogAndClause ) [ ] ,
@@ -45,6 +53,7 @@ const ANY_DISCOURSE_NODE = "Any Discourse Node";
4553const registerDiscourseDatalogTranslators = ( ) => {
4654 const discourseRelations = getDiscourseRelations ( ) ;
4755 const discourseNodes = getDiscourseNodes ( discourseRelations ) ;
56+
4857 const isACallback : Parameters <
4958 typeof registerDatalogTranslator
5059 > [ 0 ] [ "callback" ] = ( { source, target } ) => {
@@ -89,6 +98,64 @@ const registerDiscourseDatalogTranslators = () => {
8998 } )
9099 : [ ] ;
91100 } ;
101+ const isACandidateCallback : Parameters <
102+ typeof registerDatalogTranslator
103+ > [ 0 ] [ "callback" ] = ( { source, target } ) => {
104+ const nodeByTypeOrText = Object . fromEntries ( [
105+ ...discourseNodes . map ( ( n ) => [ n . type , n ] as const ) ,
106+ ...discourseNodes . map ( ( n ) => [ n . text , n ] as const ) ,
107+ ] ) ;
108+
109+ if ( target === ANY_DISCOURSE_NODE ) {
110+ const nodesWithTags = discourseNodes . filter ( hasTag ) ;
111+ if ( nodesWithTags . length === 0 ) return [ ] ;
112+ return [
113+ {
114+ type : "or-join-clause" as const ,
115+ variables : [ { type : "variable" as const , value : source } ] ,
116+ clauses : nodesWithTags . map ( ( node ) => {
117+ const variableRef = `${ toVar ( node . tag ) } -ref` ;
118+ return {
119+ type : "and-clause" as const ,
120+ clauses : [
121+ {
122+ type : "data-pattern" as const ,
123+ arguments : [
124+ { type : "variable" as const , value : source } ,
125+ { type : "constant" as const , value : ":block/refs" } ,
126+ {
127+ type : "variable" as const ,
128+ value : variableRef ,
129+ } ,
130+ ] ,
131+ } ,
132+ ...getTitleDatalog ( { source : variableRef , target : node . tag } ) ,
133+ ] ,
134+ } ;
135+ } ) ,
136+ } ,
137+ ] ;
138+ }
139+
140+ const targetNodeTag = nodeByTypeOrText [ target ] ?. tag ;
141+ if ( ! targetNodeTag ) return [ ] ;
142+ const variableRef = `${ toVar ( targetNodeTag ) } -ref` ;
143+
144+ return [
145+ {
146+ type : "data-pattern" as const ,
147+ arguments : [
148+ { type : "variable" as const , value : source } ,
149+ { type : "constant" as const , value : ":block/refs" } ,
150+ {
151+ type : "variable" as const ,
152+ value : variableRef ,
153+ } ,
154+ ] ,
155+ } ,
156+ ...getTitleDatalog ( { source : variableRef , target : targetNodeTag } ) ,
157+ ] ;
158+ } ;
92159 const unregisters = new Set < ( ) => void > ( ) ;
93160 unregisters . add (
94161 registerDatalogTranslator ( {
@@ -100,6 +167,18 @@ const registerDiscourseDatalogTranslators = () => {
100167 placeholder : "Enter a discourse node" ,
101168 } ) ,
102169 ) ;
170+
171+ unregisters . add (
172+ registerDatalogTranslator ( {
173+ key : "is a candidate" ,
174+ callback : isACandidateCallback ,
175+ targetOptions : discourseNodes
176+ . filter ( ( d ) => d . tag )
177+ . map ( ( d ) => d . text )
178+ . concat ( ANY_DISCOURSE_NODE ) ,
179+ placeholder : "Enter a discourse node" ,
180+ } ) ,
181+ ) ;
103182 unregisters . add (
104183 registerDatalogTranslator ( {
105184 key : "self" ,
0 commit comments