Skip to content
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

Support for skos:collection #200

Merged
merged 3 commits into from
Nov 8, 2022
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
65 changes: 58 additions & 7 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ exports.onPreBootstrap = async ({createContentDigest, actions}) => {
const ttlFiles = getTurtleFiles('./data', [])
console.info(`Found these turtle files:`)
ttlFiles.forEach(e => console.info(e))
ttlFiles.forEach(async f => {
for (const f of ttlFiles) {
const ttlString = fs.readFileSync(f).toString()
const doc = await jsonld.fromRDF(ttlString, { format: 'text/turtle' })
const compacted = await jsonld.compact(doc, context.jsonld)
await compacted['@graph'].forEach(graph => {
const {
narrower, narrowerTransitive, narrowMatch, broader, broaderTransitive,
broadMatch, exactMatch, closeMatch, related, relatedMatch,
inScheme, topConceptOf, hasTopConcept, ...properties
inScheme, topConceptOf, hasTopConcept, member, ...properties
} = graph
const type = Array.isArray(properties.type)
? properties.type.find(t => ['Concept', 'ConceptScheme'])
? properties.type.find(t => ['Concept', 'ConceptScheme', 'Collection'])
: properties.type
const node = {
...properties,
Expand All @@ -99,14 +99,15 @@ exports.onPreBootstrap = async ({createContentDigest, actions}) => {
internal: {
contentDigest: createContentDigest(graph),
type,
}
},
member___NODE: (member || []).map(member => member.id)
}
if (type === 'Concept') {
Object.assign(node, {})
}
['Concept', 'ConceptScheme'].includes(type) && createNode(node)
['Concept', 'ConceptScheme', 'Collection'].includes(type) && createNode(node)
})
})
}
}

exports.sourceNodes = async ({ actions }) => {
Expand All @@ -115,7 +116,56 @@ exports.sourceNodes = async ({ actions }) => {
}

exports.createPages = async ({ graphql, actions: { createPage } }) => {
const actorUrlTemplate = urlTemplate.parse(process.env.ACTOR)

const memberOf = {}

// Build collection pages
const collections = await graphql(queries.allCollection(languages))
await Promise.all(collections.data.allCollection.edges.map(async ({ node: collection }) => {
const indexes = Object.fromEntries([...languages].map(l => {
const index = flexsearch.create({
tokenize: "full",
})
index.addMatcher({
'[Ää]': "a", // replaces all 'ä' to 'a'
'[Öö]': "o",
'[Üü]': "u",
})
return [l, index]
}))

// store collection membership for concepts
collection.member.forEach((m) => {
if (memberOf.hasOwnProperty(m.id)) {
memberOf[m.id].push(collection);
} else {
memberOf[m.id] = [collection];
}
})

const json = omitEmpty(Object.assign({}, collection, context.jsonld))
const jsonld = omitEmpty(Object.assign({}, collection, context.jsonld))
languages.forEach(language => createPage({
path: getFilePath(collection.id, `${language}.html`),
component: path.resolve(`./src/components/Collection.js`),
context: {
language,
languages: Array.from(languages),
node: collection,
baseURL: process.env.BASEURL || ''
}
}))
createData({
path: getFilePath(collection.id, 'json'),
data: JSON.stringify(json, null, 2)
})
createData({
path: getFilePath(collection.id, 'jsonld'),
data: JSON.stringify(jsonld, null, 2)
})
languages.forEach(language => indexes[language].add(collection.id, i18n(language)(collection.prefLabel)))
}))

const conceptSchemes = await graphql(queries.allConceptScheme(languages))

conceptSchemes.errors && console.error(conceptSchemes.errors)
Expand Down Expand Up @@ -151,6 +201,7 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
language,
languages: Array.from(languages),
node: concept,
collections: memberOf.hasOwnProperty(concept.id) ? memberOf[concept.id] : [],
baseURL: process.env.BASEURL || ''
}
}))
Expand Down
26 changes: 26 additions & 0 deletions src/components/Collection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @jsx jsx */
import { jsx } from '@emotion/react'
import { Link } from 'gatsby'
import { i18n, getFilePath } from '../common'
import JsonLink from './JsonLink'

const Collection = ({ pageContext: { node: collection, language, baseURL } }) => (
<div className="content block">
<h1>{i18n(language)(collection.prefLabel)}</h1>
<h2>{collection.id}</h2>
<JsonLink to={baseURL + getFilePath(collection.id, "json")} />
<ul>
{
collection.member.map((member) => (
<li key={member.id}>
<Link to={getFilePath(member.id, `${language}.html`)}>
{i18n(language)(member.prefLabel)}
</Link>
</li>
))
}
</ul>
</div>
)

export default Collection
16 changes: 15 additions & 1 deletion src/components/Concept.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import JsonLink from './JsonLink'

import { i18n, getDomId, getFilePath } from '../common'

const Concept = ({ pageContext: { node: concept, language, baseURL } }) => (
const Concept = ({ pageContext: { node: concept, language, collections, baseURL } }) => (
<div className="content block" id={getDomId(concept.id)}>
<h1>
{concept.notation &&
Expand Down Expand Up @@ -140,6 +140,20 @@ const Concept = ({ pageContext: { node: concept, language, baseURL } }) => (
</ul>
</div>
)}
{collections && collections.length > 0 && (
<div className="collections">
<h3>in Collections</h3>
<ul>
{collections.map((collection) => (
<li key={collection.id}>
<Link to={getFilePath(collection.id, `${language}.html`)}>
{i18n(language)(collection.prefLabel)}
</Link>
</li>
))}
</ul>
</div>
)}
</div>
)

Expand Down
22 changes: 22 additions & 0 deletions src/queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
module.exports.allCollection = (languages) => `
{
allCollection {
edges {
node {
id
type
prefLabel {
${[...languages].join(' ')}
}
member {
id
prefLabel {
${[...languages].join(' ')}
}
}
}
}
}
}
`

module.exports.allConcept = (inScheme, languages) => `
{
allConcept(
Expand Down
4 changes: 2 additions & 2 deletions src/templates/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SEO from '../components/seo'
import { style } from '../styles/concepts.css.js'

const App = ({pageContext, children}) => {
const conceptSchemeId = pageContext.node.type === 'ConceptScheme'
const conceptSchemeId = (pageContext.node.type === 'ConceptScheme' || pageContext.node.type === 'Collection')
? pageContext.node.id
: pageContext.node.inScheme.id
const [index, setIndex] = useState(FlexSearch.create())
Expand All @@ -28,7 +28,7 @@ const App = ({pageContext, children}) => {
const idx = FlexSearch.create()
// add custom matcher to match umlaute at beginning of string
idx.addMatcher({
'[]': "a", // replaces all 'ä' to 'a'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that one :)

'[Ää]': "a", // replaces all 'ä' to 'a'
'[Öö]': "o",
'[Üü]': "u",
})
Expand Down
7 changes: 7 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = (languages) => `

type Collection implements Node {
type: String,
prefLabel: LanguageMap,
member: [Concept] @link(from: "member___NODE")
}

type ConceptScheme implements Node {
type: String,
title: LanguageMap,
Expand Down