Skip to content

Commit db53df0

Browse files
Merge pull request #7 from elsevierlabs-os/dev
More robust JSON-LD processing
2 parents 5a9f60c + f618926 commit db53df0

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @RinkeHoekstra

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"author": {"name": "Rinke Hoekstra", "email": "[email protected]"},
66
"repository": "https://github.com/elsevierlabs-os/linked-data",
77
"description": "This extension provides shortcuts for standard Linked Data operations, format conversions, validation, querying and a graph visualization.",
8-
"version": "1.0.4",
8+
"version": "1.0.5",
99
"icon": "media/img/lde-icon.png",
1010
"engines": {
1111
"vscode": "^1.59.0"

src/extension.ts

Lines changed: 26 additions & 18 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
@@ -334,7 +352,7 @@ async function updateGraphView(document:vscode.TextDocument, extensionUri: vscod
334352
<head>
335353
<meta charset="UTF-8">
336354
<meta name="viewport" content="width=device-width, initial-scale=1.0">
337-
<title>JSON-LD Viewer</title>
355+
<title>Graph Viewer</title>
338356
<!--
339357
Use a content security policy to only allow loading images from https or from our extension directory,
340358
and only allow scripts that have a specific nonce.
@@ -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)