@@ -7,7 +7,7 @@ import { parse } from 'yaml';
7
7
const jsonld = require ( 'jsonld' ) ;
8
8
const ShaclValidator = require ( 'schemarama/shaclValidator' ) . Validator ;
9
9
10
- import { report } from 'process' ;
10
+ import { rawListeners , report } from 'process' ;
11
11
import { RDFArrayRemove } from 'rdflib/lib/utils-js' ;
12
12
13
13
const STRICT_NQUADS_REGEX = / ( < \S + ?> | _ : \S + ) ? \s + ( < \S + ?> ) \s + ( < \S + ?> | _ : \S + ?| ( " .* " ( ^ ^ < .+ > ) ? ) ) \s + ( < \S + ?> | _ : \S + ?) \s * \. ( \s * # .+ ) ? / g;
@@ -61,19 +61,37 @@ function suggestContextPrefixes(json: any, rdfSerializer: any) {
61
61
}
62
62
}
63
63
64
+ async function JSONLDtoNQuads ( data : string ) {
65
+ // Convert JSON-LD to NQuads through jsonld.js as it is much better at parsing JSON-LD than rdflib.js
66
+ const data_object = JSON . parse ( data ) ;
67
+
68
+ return jsonld . toRDF ( data_object , { format : 'application/n-quads' } ) ;
69
+ }
70
+
71
+
64
72
65
73
// Load the RDF contained in the string into a triple store and return resolved Promise
66
74
function loadRDF ( data : string , rdfStore : $rdf . Store , mediaType : string ) {
67
75
if ( mediaType == undefined ) {
68
76
mediaType = "application/ld+json" ;
69
77
}
70
-
78
+
71
79
return new Promise < $rdf . Store > ( ( resolve , reject ) => {
72
80
try {
73
- $rdf . parse ( data , rdfStore , "https://example.com/test/" , mediaType , function ( ) {
74
- vscode . window . showInformationMessage ( "Successfully parsed: Statements in the graph: " + rdfStore . length ) ;
75
- resolve ( rdfStore ) ;
81
+
82
+ if ( mediaType == "application/ld+json" ) {
83
+ JSONLDtoNQuads ( data )
84
+ . then ( nquads => $rdf . parse ( nquads , rdfStore , "https://example.com/test/" , "application/n-quads" , function ( ) {
85
+ vscode . window . showInformationMessage ( "Successfully parsed: Statements in the graph: " + rdfStore . length ) ;
86
+ resolve ( rdfStore ) ;
87
+ } ) ) ;
88
+ } else {
89
+ $rdf . parse ( data , rdfStore , "https://example.com/test/" , mediaType , function ( ) {
90
+ vscode . window . showInformationMessage ( "Successfully parsed: Statements in the graph: " + rdfStore . length ) ;
91
+ resolve ( rdfStore ) ;
76
92
} ) ;
93
+ }
94
+
77
95
} catch ( err :any ) {
78
96
vscode . window . showErrorMessage ( `Failed to load data into triplestore as ${ mediaType } !` , err ) ;
79
97
reject ( err ) ;
@@ -312,7 +330,7 @@ async function updateGraphView(document:vscode.TextDocument, extensionUri: vscod
312
330
// Create and show a new webview
313
331
const panel = vscode . window . createWebviewPanel (
314
332
'linked-data' , // Identifies the type of the webview. Used internally
315
- 'JSON-LD Viewer : ' + documentName , // Title of the panel displayed to the user
333
+ 'Graph : ' + documentName , // Title of the panel displayed to the user
316
334
vscode . ViewColumn . Two , // Editor column to show the new webview panel in.
317
335
{
318
336
// Enable scripts in the webview
@@ -334,7 +352,7 @@ async function updateGraphView(document:vscode.TextDocument, extensionUri: vscod
334
352
<head>
335
353
<meta charset="UTF-8">
336
354
<meta name="viewport" content="width=device-width, initial-scale=1.0">
337
- <title>JSON-LD Viewer</title>
355
+ <title>Graph Viewer</title>
338
356
<!--
339
357
Use a content security policy to only allow loading images from https or from our extension directory,
340
358
and only allow scripts that have a specific nonce.
@@ -408,7 +426,7 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
408
426
}
409
427
} catch ( e : any ) {
410
428
vscode . window . showErrorMessage ( e . message ) ;
411
- console . log ( "Could not load context from file, perhaps it's not a file :-) " ) ;
429
+ console . log ( "Could not load context from files " ) ;
412
430
}
413
431
return json_object ;
414
432
}
@@ -419,18 +437,8 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
419
437
// your extension is activated the very first time the command is executed
420
438
export function activate ( context : vscode . ExtensionContext ) {
421
439
422
- // Use the console to output diagnostic information (console.log) and errors (console.error)
423
- // This line of code will only be executed once when your extension is activated
424
440
console . log ( 'The extension "linked-data" is now active!' ) ;
425
441
426
- // The command has been defined in the package.json file
427
- // Now provide the implementation of the command with registerCommand
428
- // The commandId parameter must match the command field in package.json
429
- // let disposable = vscode.commands.registerCommand('json-ld-viewer.helloWorld', () => {
430
- // // The code you place here will be executed every time your command is executed
431
- // // Display a message box to the user
432
- // vscode.window.showInformationMessage('Hello World from JSON-LD Viewer!');
433
- // });
434
442
435
443
let disposableViewer = vscode . commands . registerCommand ( 'linked-data.view' , async ( ) => {
436
444
const editor = vscode . window . activeTextEditor ;
0 commit comments