Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@rdfjs-elements/formats-pretty

zazuko47.4kMIT0.6.8TypeScript support: included

RDF/JS Sink Maps with serializers configured for pretty printing

readme

@rdfjs-elements/formats-pretty

Common RDF/JS parsers and serializers, the latter returning a nicely formatted output.

For basic usage see @rdfjs/formats-common

Serializers

Format Pretty or not Implementation
application/ld+json 😀 @rdfjs/serializer-jsonld-ext
application/trig 🤩 @graphy/content.trig.writer
text/n3 🤩 @graphy/content.ttl.writer
text/turtle 🤩 @graphy/content.ttl.writer
application/n-triples 😶 @rdfjs/serializer-ntriples
application/n-quads 😶 @rdfjs/serializer-ntriples
application/rdf+xml 😶 @graphy/content.xml.scribe

Individual serializer skins can also be created by importing from @rdfjs-elements/formats-pretty/serializers. This allows for initializing a sink preloaded with a given set of prefixes.

import prefixes from '@zazuko/prefixes'
import { TurtleSerializer } from '@rdfjs-elements/formats-pretty'

const { schema, dcterms, foaf } = prefixes

const sink = new TurtleSerializer({
  prefixes: { schema, dcterms, foaf, ex: 'http://example/org/' }
})

This sink can then be used to produce pretty-printed RDF

import rdf from '@rdfjs/data-model'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'

// Example data
const data = [
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
  rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]

const stream = await sink.import(Readable.from(data))
console.log(await getStream(stream))

// Outputs:

// @prefix schema: <http://schema.org/> .
// @prefix dcterms: <http://purl.org/dc/terms/> .
// @prefix foaf: <http://xmlns.com/foaf/0.1/> .
// @prefix ex: <http://example/org/> .
//
//   ex:s1 schema:name "Alice" ;
//        foaf:knows ex:o1 .
//
//   ex:o1 schema:name "Bob" .

To get an output in n-triples, n-quads or n3 format the sink map interface can be used where media type must be specified:

import rdf from '@rdfjs/data-model'
import prefixes from '@zazuko/prefixes'
import formats from '@rdfjs-elements/formats-pretty'
import { Readable } from 'readable-stream'
import getStream from 'get-stream'

// Example data
const data = [
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Alice')),
  rdf.quad(rdf.namedNode('http://example/org/s1'), rdf.namedNode('http://xmlns.com/foaf/0.1/knows'), rdf.namedNode('http://example/org/o1')),
  rdf.quad(rdf.namedNode('http://example/org/o1'), rdf.namedNode('http://schema.org/name'), rdf.literal('Bob'))
]

const { schema, dcterms, foaf } = prefixes

const stream = formats.serialisers.import('application/n-triples', Readable.from(data), { schema, dcterms, foaf, ex:'http://example/org/' })

console.log(await getStream(stream))

Parsers

Format Implementation
application/ld+json @rdfjs/parser-jsonld
application/trig @rdfjs/parser-n3
text/n3 @rdfjs/parser-n3
text/turtle @rdfjs/parser-n3
application/n-triples @rdfjs/parser-n3
application/n-quads @rdfjs/parser-n3
application/rdf+xml rdfxml-streaming-parser

changelog

@rdfjs-elements/formats-pretty

0.6.8

Patch Changes

  • f67b4da: Parsing n3 rules would fail. Using the package n3 to support them.

0.6.7

Patch Changes

  • 94a5e85: More strict dependency on @rdfjs/environment

0.6.6

Patch Changes

  • 91040b2: Update @graphy packages
  • aef831f: Support @rdfjs/environment v1

0.6.5

Patch Changes

  • bdb533b: Add CHANGELOG

0.6.4

Patch Changes

  • c8f0724: Preserve line breaks when serialising Turtle and Trig (fixes #139)
  • 80cf005: Improves the exported type of default export

0.6.3

Patch Changes

  • 55e0590: Remove that postinstall script after all

0.6.2

Patch Changes

  • 0e8db35: Using npx to run postinstall script

0.6.1

Patch Changes

  • def9930: Fix postinstall script which should run only in dev env

0.6.0

Minor Changes

  • 90ebc06: Rename the named export formats to mediaTypes
  • 90ebc06: The package is now ESM-only
  • 90ebc06: Removed exports of instances of sinks. Instead, classes are exported. For example

    -import { jsonld } from '@rdfjs-elements/formats-pretty'
    +import { JsonLdSerializer } from '@rdfjs-elements/formats-pretty'
    +
    +const jsonld = new JsonLdSerializer()
  • 90ebc06: Instead of exporting parsers and serializers, default-exports a Formats object

Patch Changes

  • 90ebc06: Update to ESM-only dependencies

0.5.2

Patch Changes

  • 4161f0b: List where some of their nodes are reused failed to serialize (fixes #15)
  • ffbbcba: Updated graphy packages to v4.3.4

0.5.1

Patch Changes

  • fbd5755: Update @tpluscode/rdf-ns-builders to v2

0.5.0

Minor Changes

  • 18dccf6: By default, RDF Lists will always be serialized in shorthand syntax, even if the intermediate nodes have additional properties.

    To preserve any additional properties, use preserveListNodeProperties option of the sinks' import method

    sink.import(graph, { preserveListNodeProperties: true })

0.4.3

Patch Changes

  • 0c89baf: Explicit rdf:type rdf:List would prevent shorthand list syntax
  • 965c98a: Shared RDF List nodes would fail to serialize to turtle (re blake-regalia/graphy.js#54)
  • 2de691c: Number would fail RDF/XML serialization (fixes #87)

0.4.2

Patch Changes

  • 005847c: Export serializer sink factories with option to set default prefixes

0.4.1

Patch Changes

  • 568d00e: Incorrect exports field prevented importing in node (ERR_PACKAGE_PATH_NOT_EXPORTED)

0.4.0

Minor Changes

  • e31f1ac: Update JSON-LD serializer. It is a potential breaking change for caller doing framing

0.3.4

Patch Changes

  • 5f52d07: Do not pass undefined to graphy parser

    The PR #73 apparently introduced a bug because undefined options are not gracefully handled by graphy

0.3.3

Patch Changes

  • 2519449: Options were not forwarded to graphy parsers

0.3.2

Patch Changes

  • 6d9de99: Update rdf-ns-builders

0.3.1

Patch Changes

  • 76d476a: Have numbers and booleans serialized in short form

0.3.0

Minor Changes

0.2.1

Patch Changes

  • 49551ba: Module ./parsers/graphy.js missing in build

0.2.0

Minor Changes

  • 34027af: Use @graphy parsers, which preserve blank node identifiers

0.1.1

Patch Changes

  • fac4158: Added LICENSE file

0.1.0

Minor Changes

  • 7a9bef2: Initial version of pretty-printing formats package