1- import getRoamUrl from "roamjs-components/dom/getRoamUrl" ;
21import type { Result } from "roamjs-components/types/query-builder" ;
32import type { DiscourseRelation } from "./getDiscourseRelations" ;
43import type { DiscourseNode } from "./getDiscourseNodes" ;
@@ -9,57 +8,46 @@ import { uniqJsonArray, getPageData } from "./exportUtils";
98import { getExportSettings } from "./getExportSettings" ;
109import canonicalRoamUrl from "./canonicalRoamUrl" ;
1110
12- export const getJsonLdData = async ( {
13- results,
11+ export const jsonLdContext = ( baseUrl : string ) : Record < string , string > => ( {
12+ rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ,
13+ rdfs : "http://www.w3.org/2000/01/rdf-schema#" ,
14+ owl : "http://www.w3.org/2002/07/owl#" ,
15+ dc : "http://purl.org/dc/elements/1.1/" ,
16+ prov : "http://www.w3.org/ns/prov#" ,
17+ sioc : "http://rdfs.org/sioc/ns#" ,
18+ dgb : "https://discoursegraphs.com/schema/dg_base" ,
19+ subClassOf : "rdfs:subClassOf" ,
20+ title : "dc:title" ,
21+ label : "rdfs:label" ,
22+ modified : "dc:modified" ,
23+ created : "dc:date" ,
24+ creator : "dc:creator" ,
25+ content : "sioc:content" ,
26+ source : "dgb:source" ,
27+ destination : "dgb:destination" ,
28+ predicate : "rdf:predicate" ,
29+ nodeSchema : "dgb:NodeSchema" ,
30+ relationDef : "dgb:RelationDef" ,
31+ relationInstance : "dgb:RelationInstance" ,
32+ inverseOf : "owl:inverse-of" ,
33+ pages : `${ baseUrl } /page/` ,
34+ } ) ;
35+
36+ export const getJsonLdSchema = async ( {
1437 allNodes,
1538 allRelations,
16- nodeLabelByType,
1739 updateExportProgress,
1840} : {
19- results : Result [ ] ;
2041 allNodes : DiscourseNode [ ] ;
2142 allRelations : DiscourseRelation [ ] ;
22- nodeLabelByType : Record < string , string > ;
23- updateExportProgress : ( progress : number ) => void ;
24- } ) => {
25- const roamUrl = canonicalRoamUrl ( ) ;
26- const getRelationData = ( ) =>
27- getRelationDataUtil ( allRelations , nodeLabelByType ) ;
28- updateExportProgress ( 0 ) ;
29- // skip a beat to let progress render
30- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
31- const pageData = await getPageData ( { results, allNodes } ) ;
32- const numPages = pageData . length + allNodes . length ;
43+ updateExportProgress : ( progress : number ) => Promise < void > ;
44+ } ) : Promise < Record < string , string > [ ] > => {
3345 let numTreatedPages = 0 ;
34- const context = {
35- rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ,
36- rdfs : "http://www.w3.org/2000/01/rdf-schema#" ,
37- owl : "http://www.w3.org/2002/07/owl#" ,
38- dc : "http://purl.org/dc/elements/1.1/" ,
39- prov : "http://www.w3.org/ns/prov#" ,
40- sioc : "http://rdfs.org/sioc/ns#" ,
41- dgb : "https://discoursegraphs.com/schema/dg_base" ,
42- subClassOf : "rdfs:subClassOf" ,
43- title : "dc:title" ,
44- label : "rdfs:label" ,
45- modified : "dc:modified" ,
46- created : "dc:date" ,
47- creator : "dc:creator" ,
48- content : "sioc:content" ,
49- source : "dgb:source" ,
50- destination : "dgb:destination" ,
51- predicate : "rdf:predicate" ,
52- nodeSchema : "dgb:NodeSchema" ,
53- relationDef : "dgb:RelationDef" ,
54- relationInstance : "dgb:RelationInstance" ,
55- pages : `${ roamUrl } /page/` ,
56- } ;
5746 const settings = {
5847 ...getExportSettings ( ) ,
5948 includeDiscourseContext : false ,
6049 } ;
6150 // TODO : Identify existing CURIES in the node definition
62- /* eslint-disable @typescript-eslint/naming-convention */
6351 const nodeSchemaData = await Promise . all (
6452 allNodes . map ( async ( node : DiscourseNode ) => {
6553 const { date, displayName, modified } = getPageMetadata ( node . text ) ;
@@ -71,11 +59,10 @@ export const getJsonLdData = async ({
7159 { ...settings , allNodes } ,
7260 ) ;
7361 numTreatedPages += 1 ;
74- updateExportProgress ( 0.1 + ( numTreatedPages / numPages ) * 0.75 ) ;
75- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
62+ await updateExportProgress ( numTreatedPages ) ;
7663 return {
77- "@id" : `pages:${ node . type } ` ,
78- "@type" : "nodeSchema" ,
64+ "@id" : `pages:${ node . type } ` , // eslint-disable-line @typescript-eslint/naming-convention
65+ "@type" : "nodeSchema" , // eslint-disable-line @typescript-eslint/naming-convention
7966 label : node . text ,
8067 content : r . content ,
8168 modified : modified ?. toJSON ( ) ,
@@ -85,42 +72,64 @@ export const getJsonLdData = async ({
8572 } ) ,
8673 ) ;
8774 const relSchemaData = allRelations . map ( ( r : DiscourseRelation ) => ( {
88- "@id" : `pages:${ r . id } ` ,
89- "@type" : "relationDef" ,
75+ "@id" : `pages:${ r . id } ` , // eslint-disable-line @typescript-eslint/naming-convention
76+ "@type" : "relationDef" , // eslint-disable-line @typescript-eslint/naming-convention
9077 domain : `pages:${ r . source } ` ,
9178 range : `pages:${ r . destination } ` ,
9279 label : r . label ,
9380 } ) ) ;
94- const schemaData = [ ...nodeSchemaData , ...relSchemaData ] ;
95-
96- const schemaUriByName = Object . fromEntries (
97- schemaData . map ( ( node ) => [ node . label , node [ "@id" ] ] ) ,
98- ) ;
99-
10081 const inverseRelSchemaData = allRelations . map ( ( r : DiscourseRelation ) => ( {
101- "@id" : `pages:${ r . id } -inverse` ,
102- "@type" : "relationDef" ,
82+ "@id" : `pages:${ r . id } -inverse` , // eslint-disable-line @typescript-eslint/naming-convention
83+ "@type" : "relationDef" , // eslint-disable-line @typescript-eslint/naming-convention
10384 domain : `pages:${ r . destination } ` ,
10485 range : `pages:${ r . source } ` ,
10586 label : r . complement ,
106- "owl:inverse-of" : `pages:${ r . id } ` ,
87+ inverseOf : `pages:${ r . id } ` ,
10788 } ) ) ;
89+ /* eslint-enable @typescript-eslint/naming-convention */
90+ return [ ...nodeSchemaData , ...relSchemaData , ...inverseRelSchemaData ] ;
91+ } ;
10892
109- await Promise . all (
110- pageData . map ( async ( page : Result ) => {
111- const r = await pageToMarkdown ( page , {
112- ...settings ,
113- allNodes,
114- } ) ;
115- page . content = r . content ;
116- numTreatedPages += 1 ;
117- updateExportProgress ( 0.1 + ( numTreatedPages / numPages ) * 0.75 ) ;
118- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
119- } ) ,
93+ export const getJsonLdData = async ( {
94+ results,
95+ allNodes,
96+ allRelations,
97+ nodeLabelByType,
98+ updateExportProgress,
99+ } : {
100+ results : Result [ ] ;
101+ allNodes : DiscourseNode [ ] ;
102+ allRelations : DiscourseRelation [ ] ;
103+ nodeLabelByType : Record < string , string > ;
104+ updateExportProgress : ( progress : number ) => Promise < void > ;
105+ } ) : Promise <
106+ Record < string , string | Record < string , string > | Record < string , string > [ ] >
107+ > => {
108+ const roamUrl = canonicalRoamUrl ( ) ;
109+ const getRelationData = ( ) =>
110+ getRelationDataUtil ( allRelations , nodeLabelByType ) ;
111+ await updateExportProgress ( 0 ) ;
112+ const pageData = await getPageData ( { results, allNodes } ) ;
113+ const numPages = pageData . length + allNodes . length ;
114+ let numTreatedPages = 0 ;
115+ const settings = {
116+ ...getExportSettings ( ) ,
117+ includeDiscourseContext : false ,
118+ } ;
119+ const schemaData = await getJsonLdSchema ( {
120+ allNodes,
121+ allRelations,
122+ updateExportProgress : async ( numTreatedPages : number ) => {
123+ await updateExportProgress ( 0.1 + ( numTreatedPages / numPages ) * 0.75 ) ;
124+ } ,
125+ } ) ;
126+
127+ const nodeSchemaUriByName = Object . fromEntries (
128+ schemaData
129+ . filter ( ( s ) => s . content !== undefined )
130+ . map ( ( node ) => [ node . label , node [ "@id" ] ] ) ,
120131 ) ;
121132
122- // skip a beat to let progress render
123- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
124133 await Promise . all (
125134 pageData . map ( async ( page : Result ) => {
126135 const r = await pageToMarkdown ( page , {
@@ -129,42 +138,41 @@ export const getJsonLdData = async ({
129138 } ) ;
130139 page . content = r . content ;
131140 numTreatedPages += 1 ;
132- updateExportProgress ( 0.1 + ( numTreatedPages / numPages ) * 0.75 ) ;
133- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
141+ await updateExportProgress ( 0.1 + ( numTreatedPages / numPages ) * 0.75 ) ;
134142 } ) ,
135143 ) ;
136144
137145 const nodes = pageData . map ( ( { text, uid, content, type } ) => {
138146 const { date, displayName, modified } = getPageMetadata ( text ) ;
139147 return {
140- "@id" : `pages:${ uid } ` ,
141- "@type" : schemaUriByName [ type ] ,
148+ "@id" : `pages:${ uid } ` , // eslint-disable-line @typescript-eslint/naming-convention
149+ "@type" : nodeSchemaUriByName [ type ] , // eslint-disable-line @typescript-eslint/naming-convention
142150 title : text ,
143- content,
151+ content : content as string ,
144152 modified : modified ?. toJSON ( ) ,
145153 created : date . toJSON ( ) ,
146154 creator : displayName ,
147155 } ;
148156 } ) ;
149157 const nodeSet = new Set ( pageData . map ( ( n ) => n . uid ) ) ;
150158 const rels = await getRelationData ( ) ;
151- updateExportProgress ( 1 ) ;
152- await new Promise ( ( resolve ) => setTimeout ( resolve ) ) ;
159+ await updateExportProgress ( 1 ) ;
153160 const relations = uniqJsonArray (
154161 rels . filter ( ( r ) => nodeSet . has ( r . source ) && nodeSet . has ( r . target ) ) ,
155162 ) ;
156- const relData = relations . map ( ( { source , target , label } ) => ( {
163+ const relData = relations . map ( ( { relUid , source , target } ) => ( {
157164 // no id yet, just a blank node
158- "@type" : "relationInstance" ,
159- predicate : schemaUriByName [ label ] ,
165+ "@type" : "relationInstance" , // eslint-disable-line @typescript-eslint/naming-convention
166+ predicate : `pages: ${ relUid } ` ,
160167 source : `pages:${ source } ` ,
161168 destination : `pages:${ target } ` ,
162169 } ) ) ;
163170 return {
164- "@context" : context ,
171+ /* eslint-disable @typescript-eslint/naming-convention */
172+ "@context" : jsonLdContext ( roamUrl ) ,
165173 "@id" : roamUrl ,
166174 "prov:generatedAtTime" : new Date ( ) . toISOString ( ) ,
167- "@graph" : [ ...schemaData , ...inverseRelSchemaData , ...nodes , ...relData ] ,
175+ "@graph" : [ ...schemaData , ...nodes , ...relData ] ,
176+ /* eslint-enable @typescript-eslint/naming-convention */
168177 } ;
169- /* eslint-enable @typescript-eslint/naming-convention */
170178} ;
0 commit comments