var fs = require("fs");

var jsonld = require("jsonld");

var nquads = fs.readFileSync(process.argv[2], "utf8");

var cidocPrefix = "http://erlangen-crm.org/current/";

jsonld.fromRDF(nquads, {format: "application/nquads"}, function(err, records) {
    var record = records[1];
    var map = {};

    record["@graph"].forEach(function(item) {
        map[item["@id"]] = item;
    });

    var linkUpRecord = function(baseRecord) {
        delete baseRecord["@id"];

        //console.log(baseRecord)
        for (var key in baseRecord) {
            var val = baseRecord[key];

            if (key.indexOf(cidocPrefix) === 0) {
                delete baseRecord[key];
                key = key.replace(cidocPrefix, "");
                baseRecord[key] = val;
            }

            if (typeof val === "object") {
                baseRecord[key] = val.map(function(obj) {
                    if (typeof obj === "object") {
                        if ("@value" in obj) {
                            return obj["@value"];

                        } else if (obj["@id"] in map && map[obj["@id"]] !== obj) {
                            console.log("linking", obj["@id"])
                            if (obj["@id"] !== "http://collection.britishmuseum.org/id/object/JCF345") {
                                return map[obj["@id"]];
                            }
                        }
                    } else if (typeof obj === "string") {
                        return obj.replace(cidocPrefix, "");
                    }

                    return obj;
                });

                if (baseRecord[key].length === 1) {
                    baseRecord[key] = baseRecord[key][0];
                }
            }
        }

        return baseRecord;
    };

    for (var itemName in map) {
        linkUpRecord(map[itemName]);
    }

    var baseRecord = record["@graph"][0];

    //console.log(records.length)
    console.log(JSON.stringify(baseRecord, null, "    "));
});

@@ -166,62 +166,27 @@
 %22;%0A%0A
-jsonld.fromRDF(nquads, %7Bformat: %22application/nquads%22%7D,
+var processRecord =
  fun
@@ -195,20 +195,14 @@
 ion(
-err, 
 record
-s
 ) %7B%0A
@@ -209,17 +209,21 @@
     var 
-r
+baseR
 ecord = 
@@ -232,11 +232,56 @@
 cord
-s%5B1
+%5B%22@graph%22%5D%5B0%5D;%0A    var rootID = baseRecord%5B%22@id%22
 %5D;%0A 
@@ -420,32 +420,40 @@
 ction(baseRecord
+, rootID
 ) %7B%0A        dele
@@ -479,42 +479,8 @@
 %5D;%0A%0A
-        //console.log(baseRecord)%0A
     
@@ -1114,144 +1114,33 @@
     
-console.log(%22linking%22, obj%5B%22@id%22%5D)%0A                            if (obj%5B%22@id%22%5D !== %22http://collection.britishmuseum.org/id/object/JCF345%22
+if (obj%5B%22@id%22%5D !== rootID
 ) %7B%0A
@@ -1700,16 +1700,24 @@
 temName%5D
+, rootID
 );%0A    %7D
@@ -1718,27 +1718,30 @@
 %0A    %7D%0A%0A    
-var
+return
  baseRecord 
@@ -1743,66 +1743,147 @@
 cord
- = record%5B%22@graph%22%5D%5B0%5D;%0A%0A    //console.log(records.length)
+;%0A%7D%0A%0Ajsonld.fromRDF(nquads, %7Bformat: %22application/nquads%22%7D, function(err, records) %7B%0A    var results = records.slice(1).map(processRecord);
 %0A   
@@ -1914,32 +1914,15 @@
 ify(
-baseRecord, null, %22    %22
+results
 ));%0A

Handle multiple records.