Skip to content

#318 Added script to sync Turtle & JSON-LD snippets #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ crashlytics.properties
crashlytics-build.properties

# Created by .ignore support plugin (hsz.mobi)

# Node.js packages
node_modules/
7 changes: 7 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Scripts

## sync-code-snippets.js

Verifies if the Turtle and JSON-LD code snippets are in sync and generates JSON-LD for missing or out-of-sync code snippets.

Call the script with `--help` for usage information.
119 changes: 119 additions & 0 deletions scripts/lib/prettyJsonld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
function countBlankNode (json, id) {
if (typeof json !== 'object') {
return 0
}

let count = 0

if (json['@id'] === id) {
count++
}

for (const child of Object.values(json)) {
count += countBlankNode(child, id)
}

return count
}

function findRef (json, id) {
if (typeof json !== 'object') {
return null
}

if (json['@id'] === id && Object.keys(json).length === 1) {
return json
}

for (const child of Object.values(json)) {
const ref = findRef(child, id)

if (ref) {
return ref
}
}

return null
}

function rebuildBlankNodeIds (json, blankNodeMap) {
if (typeof json !== 'object') {
return
}

if (json['@id'] && json['@id'].startsWith('_:')) {
const id = json['@id']

if (!blankNodeMap.has(id)) {
blankNodeMap.set(id, `_:b${blankNodeMap.size + 1}`)
}

json['@id'] = blankNodeMap.get(id)
}

for (const child of Object.values(json)) {
rebuildBlankNodeIds(child, blankNodeMap)
}
}

function prettyJsonld (json) {
if (!json['@graph']) {
return json
}

const toClean = new Set()
const toLink = new Set()

for (const child of json['@graph']) {
const id = child['@id']

if (id.startsWith('_:')) {
const count = countBlankNode(json, id)

if (count === 1) {
toClean.add(id)
}

if (count === 2) {
toLink.add(id)
}
}
}

json['@graph'] = json['@graph'].sort((a, b) => a['@id'].localeCompare(b['@id']))

for (const id of toClean) {
const node = json['@graph'].find(n => n['@id'] === id)

delete node['@id']
}

for (const id of toLink) {
const nodeIndex = json['@graph'].findIndex(n => n['@id'] === id)
const node = json['@graph'][nodeIndex]
const ref = findRef(json, id)

delete node['@id']
delete ref['@id']

for (const [key, values] of Object.entries(node)) {
ref[key] = values
}

json['@graph'].splice(nodeIndex, 1)
}

const blankNodeMap = new Map()

for (const node of json['@graph']) {
rebuildBlankNodeIds(node, blankNodeMap)
}

if (json['@graph'].length === 1) {
return json['@graph'][0]
}

return json
}

export default prettyJsonld
29 changes: 29 additions & 0 deletions scripts/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import rdf from 'rdf-ext'

async function parseJsonld (jsonld, context = {}) {
try {
rdf._data.blankNodeCounter = 0

return await rdf.io.dataset.fromText('application/ld+json', JSON.stringify({
...context,
...JSON.parse(jsonld)
}))
} catch (err) {
return null
}
}

async function parseTurtle (turtle, prefixes = '') {
try {
rdf._data.blankNodeCounter = 0

return await rdf.io.dataset.fromText('text/turtle', `${prefixes}\n${turtle}`)
} catch (err) {
return null
}
}

export {
parseJsonld,
parseTurtle
}
Loading