src/index.js
function processGraph(data) {
  let dataset = {
    '@default': []
  };

  let subjects = data.subjects,
    htmlMapper = n => {
      let div = n.ownerDocument.createElement('div');
      div.appendChild(n.cloneNode(true));
      return div.innerHTML;
    };

  Object.keys(subjects).forEach(subject => {
    let predicates = subjects[subject].predicates;
    Object.keys(predicates).forEach(predicate => {
      // iterate over objects
      let objects = predicates[predicate].objects;
      for (let oi = 0; oi < objects.length; ++oi) {
        let object = objects[oi];

        // create RDF triple
        let triple = {};

        // add subject & predicate
        triple.subject = {
          type: subject.indexOf('_:') === 0 ? 'blank node' : 'IRI',
          value: subject
        };
        triple.predicate = {
          type: predicate.indexOf('_:') === 0 ? 'blank node' : 'IRI',
          value: predicate
        };
        triple.object = {};

        // serialize XML literal
        let value = object.value;
        // !!! TODO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // The below actually most likely does NOT work.
        // In most usage contexts this will be an HTML DOM, passing it to xmldom's XMLSerializer
        // will cause it to call .toString() on all the nodes it finds — this only works inside
        // xmldom.
        if (object.type === RDF_XML_LITERAL) {
          // initialize XMLSerializer
          let serializer = new XMLSerializer();
          value = Array.from(object.value)
            .map(n => serializer.serializeToString(n))
            .join('');
          triple.object.datatype = RDF_XML_LITERAL;
        }
        // serialise HTML literal
        else if (object.type === RDF_HTML_LITERAL) {
          value = Array.from(object.value)
            .map(htmlMapper)
            .join('');
          triple.object.datatype = RDF_HTML_LITERAL;
        }
        // object is an IRI
        else if (object.type === RDF_OBJECT) {
          if (object.value.indexOf('_:') === 0)
            triple.object.type = 'blank node';
          else triple.object.type = 'IRI';
        } else {
          // object is a literal
          triple.object.type = 'literal';
          if (object.type === RDF_PLAIN_LITERAL) {
            if (object.language) {
              triple.object.datatype = RDF_LANGSTRING;
              triple.object.language = object.language;
            } else {
              triple.object.datatype = XSD_STRING;
            }
          } else {
            triple.object.datatype = object.type;
          }
        }
        triple.object.value = value;

        // add triple to dataset in default graph
        dataset['@default'].push(triple);
      }
    });
  });

  return dataset;
}
function processGraph(data) {
  let dataset = {
    '@default': []
  };

  let subjects = data.subjects,
    htmlMapper = n => {
      let div = n.ownerDocument.createElement('div');
      div.appendChild(n.cloneNode(true));
      return div.innerHTML;
    };

  Object.keys(subjects).forEach(subject => {
    let predicates = subjects[subject].predicates;
    Object.keys(predicates).forEach(predicate => {
      // iterate over objects
      let objects = predicates[predicate].objects;
      for (let oi = 0; oi < objects.length; ++oi) {
        let object = objects[oi];

        // create RDF triple
        let triple = {};

        // add subject & predicate
        triple.subject = {
          type: subject.indexOf('_:') === 0 ? 'blank node' : 'IRI',
          value: subject
        };
        triple.predicate = {
          type: predicate.indexOf('_:') === 0 ? 'blank node' : 'IRI',
          value: predicate
        };
        triple.object = {};

        // serialize XML literal
        let value = object.value;
        // !!! TODO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // The below actually most likely does NOT work.
        // In most usage contexts this will be an HTML DOM, passing it to xmldom's XMLSerializer
        // will cause it to call .toString() on all the nodes it finds — this only works inside
        // xmldom.
        if (object.type === RDF_XML_LITERAL) {
          // initialize XMLSerializer
          let serializer = new XMLSerializer();
          value = Array.from(object.value)
            .map(n => serializer.serializeToString(n))
            .join('');
          triple.object.datatype = RDF_XML_LITERAL;
        }
        // serialise HTML literal
        else if (object.type === RDF_HTML_LITERAL) {
          value = Array.from(object.value)
            .map(htmlMapper)
            .join('');
          triple.object.datatype = RDF_HTML_LITERAL;
        }
        // object is an IRI
        else if (object.type === RDF_OBJECT) {
          if (object.value.indexOf('_:') === 0)
            triple.object.type = 'blank node';
          else triple.object.type = 'IRI';
        } else {
          // object is a literal
          triple.object.type = 'literal';
          if (object.type === RDF_PLAIN_LITERAL) {
            if (object.language) {
              triple.object.datatype = RDF_LANGSTRING;
              triple.object.language = object.language;
            } else {
              triple.object.datatype = XSD_STRING;
            }
          } else {
            triple.object.datatype = object.type;
          }
        }
        triple.object.value = value;

        // add triple to dataset in default graph
        dataset['@default'].push(triple);
      }
    });
  });

  return dataset;
}
function processGraph ( data ) { let dataset = { '@default' : [ ] } ; let subjects = data . subjects , htmlMapper = n => { let div = n . ownerDocument . createElement ( 'div' ) ; div . appendChild ( n . cloneNode ( true ) ) ; return div . innerHTML ; } ; Object . keys ( subjects ) . forEach ( subject => { let predicates = subjects [ subject ] . predicates ; Object . keys ( predicates ) . forEach ( predicate => { // iterate over objects let objects = predicates [ predicate ] . objects ; for ( let oi = 0 ; oi < objects . length ; ++ oi ) { let object = objects [ oi ] ; // create RDF triple let triple = { } ; // add subject & predicate triple . subject = { type : subject . indexOf ( '_:' ) === 0 ? 'blank node' : 'IRI' , value : subject } ; triple . predicate = { type : predicate . indexOf ( '_:' ) === 0 ? 'blank node' : 'IRI' , value : predicate } ; triple . object = { } ; // serialize XML literal let value = object . value ; // !!! TODO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // The below actually most likely does NOT work. // In most usage contexts this will be an HTML DOM, passing it to xmldom's XMLSerializer // will cause it to call .toString() on all the nodes it finds — this only works inside // xmldom. if ( object . type === RDF_XML_LITERAL ) { // initialize XMLSerializer let serializer = new XMLSerializer ( ) ; value = Array . from ( object . value ) . map ( n => serializer . serializeToString ( n ) ) . join ( '' ) ; triple . object . datatype = RDF_XML_LITERAL ; } // serialise HTML literal else if ( object . type === RDF_HTML_LITERAL ) { value = Array . from ( object . value ) . map ( htmlMapper ) . join ( '' ) ; triple . object . datatype = RDF_HTML_LITERAL ; } // object is an IRI else if ( object . type === RDF_OBJECT ) { if ( object . value . indexOf ( '_:' ) === 0 ) triple . object . type = 'blank node' ; else triple . object . type = 'IRI' ; } else { // object is a literal triple . object . type = 'literal' ; if ( object . type === RDF_PLAIN_LITERAL ) { if ( object . language ) { triple . object . datatype = RDF_LANGSTRING ; triple . object . language = object . language ; } else { triple . object . datatype = XSD_STRING ; } } else { triple . object . datatype = object . type ; } } triple . object . value = value ; // add triple to dataset in default graph dataset [ '@default' ] . push ( triple ) ; } } ) ; } ) ; return dataset ; }
This function is mostly taken from the jsonld.js lib but updated to
the latest green-turtle API, and for support for HTML
This function is mostly taken from the jsonld . js lib but updated to the latest green - turtle API and for support for HTML