Skip to content

Commit 1e8c307

Browse files
committed
Now parsing JSON-LD data first through jsonld.js to produce NQuads to circumvent buggy rdflib.js parser.
1 parent 368dd55 commit 1e8c307

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/extension.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { parse } from 'yaml';
77
const jsonld = require('jsonld');
88
const ShaclValidator = require('schemarama/shaclValidator').Validator;
99

10-
import { report } from 'process';
10+
import { rawListeners, report } from 'process';
1111
import { RDFArrayRemove } from 'rdflib/lib/utils-js';
1212

1313
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) {
6161
}
6262
}
6363

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+
6472

6573
// Load the RDF contained in the string into a triple store and return resolved Promise
6674
function loadRDF(data: string, rdfStore: $rdf.Store, mediaType: string) {
6775
if(mediaType == undefined) {
6876
mediaType = "application/ld+json";
6977
}
70-
78+
7179
return new Promise<$rdf.Store>((resolve, reject) => {
7280
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);
7692
});
93+
}
94+
7795
} catch (err:any) {
7896
vscode.window.showErrorMessage(`Failed to load data into triplestore as ${mediaType}!`, err);
7997
reject(err);
@@ -312,7 +330,7 @@ async function updateGraphView(document:vscode.TextDocument, extensionUri: vscod
312330
// Create and show a new webview
313331
const panel = vscode.window.createWebviewPanel(
314332
'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
316334
vscode.ViewColumn.Two, // Editor column to show the new webview panel in.
317335
{
318336
// Enable scripts in the webview
@@ -408,7 +426,7 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
408426
}
409427
} catch (e: any) {
410428
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");
412430
}
413431
return json_object;
414432
}
@@ -419,18 +437,8 @@ async function getJSONwithEmbeddedContext(json_document: vscode.TextDocument) {
419437
// your extension is activated the very first time the command is executed
420438
export function activate(context: vscode.ExtensionContext) {
421439

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
424440
console.log('The extension "linked-data" is now active!');
425441

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-
// });
434442

435443
let disposableViewer = vscode.commands.registerCommand('linked-data.view', async () => {
436444
const editor = vscode.window.activeTextEditor;

0 commit comments

Comments
 (0)