@@ -1361,6 +1361,9 @@ class ExportJsonLdParams(OswBaseModel):
13611361 entities : Union [OswBaseModel , List [OswBaseModel ]]
13621362 """The entities to convert to JSON-LD. Can be a single entity or a list of
13631363 entities."""
1364+ id_keys : Optional [List [str ]] = ["osw_id" ]
1365+ """The keys to use as @id in the JSON-LD output. If not found in the entity at root
1366+ level, the full page title is used."""
13641367 resolve_context : Optional [bool ] = True
13651368 """If True, remote context URLs are resolved."""
13661369 mode : Optional [OSW .JsonLdMode ] = "expand"
@@ -1412,30 +1415,39 @@ def export_jsonld(self, params: ExportJsonLdParams) -> ExportJsonLdResult:
14121415 data = json .loads (e .json (exclude_none = True , indent = 4 , ensure_ascii = False ))
14131416
14141417 data ["@context" ] = []
1418+ if params .id_keys is not None :
1419+ # append "@id" mappings to the context in an additional object
1420+ id_mapping = {}
1421+ for k in params .id_keys :
1422+ id_mapping [k ] = "@id"
1423+ data ["@context" ].append (id_mapping )
14151424 if params .context is None :
14161425 for t in e .type :
14171426 data ["@context" ].append ("/wiki/" + t )
14181427 if params .context is not None :
14191428 data ["@context" ].append (params .context )
14201429 else :
1421- data ["@context" ] = {
1422- ** self .site .get_jsonld_context_prefixes (),
1423- ** params .context ,
1424- }
1430+ data ["@context" ].append (self .site .get_jsonld_context_prefixes ())
1431+ if isinstance (params .context , list ):
1432+ data ["@context" ].extend (params .context )
1433+ else :
1434+ data ["@context" ].append (params .context )
14251435 if params .additional_context is not None :
14261436 if data ["@context" ] is None :
14271437 data ["@context" ] = []
14281438 elif not isinstance (data ["@context" ], list ):
14291439 data ["@context" ] = [data ["@context" ]]
14301440 data ["@context" ].append (params .additional_context )
14311441
1432- data ["@id" ] = get_full_title (e )
1442+ # if none of the id_keys is found, use the full title
1443+ if not any (k in data for k in params .id_keys ):
1444+ data ["@id" ] = get_full_title (e )
14331445
14341446 if params .resolve_context :
14351447 graph_document ["@graph" ].append (jsonld .expand (data ))
14361448 if params .mode == "expand" :
14371449 data = jsonld .expand (data )
1438- if isinstance (data , list ):
1450+ if isinstance (data , list ) and len ( data ) > 0 :
14391451 data = data [0 ]
14401452 elif params .mode == "flatten" :
14411453 data = jsonld .flatten (data )
0 commit comments