Skip to content

Commit

Permalink
Merge branch '284-other-attributes-for-cs' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sroertgen committed Apr 24, 2024
2 parents f9ebb30 + d47e0bc commit b943680
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 28 deletions.
22 changes: 22 additions & 0 deletions cypress/e2e/conceptSchemeAndConcept.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,25 @@ describe("Parsing language from URL on Concept Schemes and Concepts", () => {
)
})
})

describe("DC properties for describing concept scheme are working", () => {
it("Title is present", () => {
cy.visit("/w3id.org/dc/index.html", {
onBeforeLoad(win) {
Object.defineProperty(win.navigator, "language", { value: "de-DE" })
},
})
//header
cy.get(".conceptScheme > a").should("have.text", "Test Vokabular DC")
// concept content block
cy.get("h1").should("include.text", "Test Vokabular DC")
})
it("Description is present", () => {
cy.visit("/w3id.org/dc/index.html", {
onBeforeLoad(win) {
Object.defineProperty(win.navigator, "language", { value: "de-DE" })
},
})
cy.get(".markdown").should("have.text", "Test Beschreibung DC")
})
})
5 changes: 4 additions & 1 deletion cypress/e2e/index.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Main Vocab Index page", () => {
},
})
// vocabs are found
cy.get(".centerPage > ul li").should("have.length", 7)
cy.get(".centerPage > ul li").should("have.length", 8)

/**
* What is tested by the existence of these links:
Expand Down Expand Up @@ -35,6 +35,9 @@ describe("Main Vocab Index page", () => {
cy.findByRole("link", {
name: "Test Vokabular in zwei Dateien",
}).should("exist")
cy.findByRole("link", {
name: "Test Vokabular DC",
}).should("exist")

// switch language
cy.get(".language-menu").contains("en").click()
Expand Down
1 change: 1 addition & 0 deletions cypress/prepare-cypress-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cp test/data/ttl/hashURIConceptScheme.ttl \
test/data/ttl/oneConceptSchemeTwoFiles_1.ttl \
test/data/ttl/oneConceptSchemeTwoFiles_2.ttl \
test/data/ttl/slashURIConceptScheme.ttl \
test/data/ttl/slashURIConceptSchemeDCproperties.ttl \
test/data/ttl/systematik.ttl \
test/data/ttl/twoConceptSchemesOneFile.ttl \
data/
Expand Down
24 changes: 21 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
getFilePath,
parseLanguages,
loadConfig,
replaceMultipleKeysInObject,
} = require("./src/common")
const context = require("./src/context")
const queries = require("./src/queries")
Expand Down Expand Up @@ -145,6 +146,8 @@ exports.onPreBootstrap = async ({ createContentDigest, actions, getNode }) => {
hasTopConcept,
member,
deprecated,
"dc:title": dc_title,
"dc:description": dc_description,
...properties
} = graph
const type = Array.isArray(properties.type)
Expand Down Expand Up @@ -204,6 +207,8 @@ exports.onPreBootstrap = async ({ createContentDigest, actions, getNode }) => {
type,
},
member___NODE: (member || []).map((member) => member.id),
dc_title,
dc_description,
}
if (type === "Concept") {
Object.assign(node, {})
Expand Down Expand Up @@ -382,17 +387,29 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
customDomain: config.customDomain,
},
})
const jsonldConceptScheme = replaceMultipleKeysInObject(conceptScheme, [
["dc_title", "dc:title"],
["dc_description", "dc:description"],
])

createData({
path: getFilePath(conceptScheme.id, "json", config.customDomain),
data: JSON.stringify(
omitEmpty(Object.assign({}, conceptScheme, context.jsonld), null, 2)
omitEmpty(
Object.assign({}, jsonldConceptScheme, context.jsonld),
null,
2
)
),
})
createData({
path: getFilePath(conceptScheme.id, "jsonld", config.customDomain),
data: JSON.stringify(
omitEmpty(Object.assign({}, conceptScheme, context.jsonld), null, 2)
omitEmpty(
Object.assign({}, jsonldConceptScheme, context.jsonld),
null,
2
)
),
})
// create index files
Expand All @@ -406,9 +423,10 @@ exports.createPages = async ({ graphql, actions: { createPage } }) => {
conceptSchemes.data.allConceptScheme.edges.map(({ node: cs }) => ({
id: cs.id,
title: cs.title,
dctitle: cs.dctitle,
dc_title: cs.dc_title,
prefLabel: cs.prefLabel,
description: cs.description,
dc_description: cs.dc_description,
languages: Array.from(languagesByCS[cs.id]),
}))
)
Expand Down
28 changes: 28 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,32 @@ const getLanguageFromUrl = (location) => {
return language
}

/**
* Replaces an oldKey against a new key
* @param {Object} obj
* @param {string} oldKey
* @param {string} newKey
* @returns {Object}
*/
const replaceKeyInObject = (obj, oldKey, newKey) => {
if (!(oldKey in obj)) return obj
const newObject = {}
delete Object.assign(newObject, obj, { [newKey]: obj[oldKey] })[oldKey]
return newObject
}

/**
* Replaces multiple keys of an object.
* Expects an array of arrays in the form [oldKey, newKey]
*/
const replaceMultipleKeysInObject = (obj, keys) => {
const replaced = keys.reduce(
(acc, val) => replaceKeyInObject(acc, val[0], val[1]),
obj
)
return replaced
}

module.exports = {
i18n,
getFilePath,
Expand All @@ -225,4 +251,6 @@ module.exports = {
parseLanguages,
loadConfig,
getLanguageFromUrl,
replaceKeyInObject,
replaceMultipleKeysInObject,
}
5 changes: 3 additions & 2 deletions src/components/ConceptScheme.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const ConceptScheme = ({
}, [data?.selectedLanguage])

const pathname = useLocation()
const description = conceptScheme?.description || conceptScheme?.dcdescription
const description =
conceptScheme?.description || conceptScheme?.dc_description
const title =
conceptScheme?.title || conceptScheme?.dctitle || conceptScheme?.prefLabel
conceptScheme?.title || conceptScheme?.dc_title || conceptScheme?.prefLabel
// got some hash uri to show
if (pathname.hash) {
const filtered = embed.find((c) => c.json.id.endsWith(pathname.hash))
Expand Down
16 changes: 12 additions & 4 deletions src/components/header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const Header = ({ siteTitle }) => {
`
const [languages, setLanguages] = useState([])
const [language, setLanguage] = useState("")
const [title, setTitle] = useState("")

// set page language
useEffect(() => {
Expand All @@ -125,6 +126,16 @@ const Header = ({ siteTitle }) => {
}
}, [data])

// set title
useEffect(() => {
const title =
data.currentScheme?.title?.[data.selectedLanguage] ||
data.currentScheme?.prefLabel?.[data.selectedLanguage] ||
data.currentScheme?.dc_title?.[data.selectedLanguage] ||
data.currentScheme?.id
setTitle(title)
}, [data])

return (
<header css={style}>
<div className="headerContent">
Expand Down Expand Up @@ -158,10 +169,7 @@ const Header = ({ siteTitle }) => {
config.customDomain
)}
>
{data.currentScheme?.title?.[data.selectedLanguage] ||
data.currentScheme?.prefLabel?.[data.selectedLanguage] ||
data.currentScheme?.dctitle?.[data.selectedLanguage] ||
data.currentScheme.id}
{title}
</Link>
</div>
</div>
Expand Down
9 changes: 5 additions & 4 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const jsonld = {
"@vocab": "http://www.w3.org/2004/02/skos/core#",
xsd: "http://www.w3.org/2001/XMLSchema#",
dct: "http://purl.org/dc/terms/",
dc: "http://purl.org/dc/elements/1.1/",
schema: "https://schema.org/",
vann: "http://purl.org/vocab/vann/",
ldp: "http://www.w3.org/ns/ldp#",
Expand All @@ -14,12 +15,12 @@ const jsonld = {
"@id": "dct:title",
"@container": "@language",
},
dctitle: {
"@id": "http://purl.org/dc/elements/1.1/title",
"dc:title": {
"@id": "dc:title",
"@container": "@language",
},
dcdescription: {
"@id": "http://purl.org/dc/elements/1.1/description",
"dc:description": {
"@id": "dc:description",
"@container": "@language",
},
description: {
Expand Down
22 changes: 15 additions & 7 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ const IndexPage = ({ location }) => {
})
}
}, [data?.languages, data?.selectedLanguage])

const getTitle = (conceptScheme) => {
const title =
i18n(language)(
conceptScheme?.title ||
conceptScheme?.prefLabel ||
conceptScheme?.dc_title
) || conceptScheme.id
if (title) {
return title
}
return conceptScheme.id
}

return (
<Layout language={language}>
<SEO title="Concept Schemes" keywords={["conceptSchemes"]} />
Expand All @@ -78,13 +92,7 @@ const IndexPage = ({ location }) => {
}
to={getFilePath(conceptScheme.id, `html`, customDomain)}
>
{(conceptScheme?.title &&
i18n(language)(conceptScheme.title)) ||
(conceptScheme?.prefLabel &&
i18n(language)(conceptScheme.prefLabel)) ||
(conceptScheme?.dctitle &&
i18n(language)(conceptScheme.dctitle)) ||
conceptScheme.id}
{getTitle(conceptScheme)}
</Link>
</li>
))}
Expand Down
4 changes: 2 additions & 2 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ module.exports.allConceptScheme = (languages) => `
prefLabel {
${[...languages].join(" ")}
}
dctitle {
dc_title {
${[...languages].join(" ")}
}
description {
${[...languages].join(" ")}
}
dcdescription {
dc_description {
${[...languages].join(" ")}
}
hasTopConcept {
Expand Down
3 changes: 1 addition & 2 deletions src/templates/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,11 @@ const App = ({ pageContext, children, location }) => {
inline: "nearest",
})
})

const toggleClick = (e) => setLabels({ ...labels, [e]: !labels[e] })
const title =
pageContext.node?.prefLabel ||
pageContext.node?.title ||
pageContext.node?.dctitle
pageContext.node?.dc_title

return (
<Layout>
Expand Down
4 changes: 2 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = (languages) => `
type ConceptScheme implements Node {
type: String,
title: LanguageMap,
dctitle: LanguageMap,
dc_title: LanguageMap,
prefLabel: LanguageMap,
description: LanguageMap,
dcdescription: LanguageMap,
dc_description: LanguageMap,
hasTopConcept: [Concept] @link(from: "hasTopConcept___NODE"),
languages: [String]
}
Expand Down
27 changes: 27 additions & 0 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
replaceFilePathInUrl,
getLinkPath,
getLanguageFromUrl,
replaceKeyInObject,
replaceMultipleKeysInObject,
} = require("../src/common")

describe("Translate", () => {
Expand Down Expand Up @@ -91,3 +93,28 @@ describe("getLanguageFromUrl", () => {
expect(getLanguageFromUrl(location)).toBeNull()
})
})

describe("replaceKeysInObject", () => {
it("replaces key in an object", () => {
const obj = { a: 1, b: 2 }
const newObj = replaceKeyInObject(obj, "a", "c")
expect(newObj).toStrictEqual({ c: 1, b: 2 })
})

it("also works if the key is not present", () => {
const obj = { a: 1, b: 2 }
const newObj = replaceKeyInObject(obj, "x", "c")
expect(newObj).toStrictEqual({ a: 1, b: 2 })
})
})

describe("replaceMultipleKeysInObject", () => {
it("replaces multiple keys in an object", () => {
const obj = { a: 1, b: 2 }
const newObj = replaceMultipleKeysInObject(obj, [
["a", "c"],
["b", "d"],
])
expect(newObj).toStrictEqual({ c: 1, d: 2 })
})
})
2 changes: 1 addition & 1 deletion test/conceptScheme.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe.concurrent("Concept", () => {
...ConceptSchemePC,
node: {
...ConceptSchemePC.node,
dctitle: {
dc_title: {
de: "dctitle DE",
},
},
Expand Down
Loading

0 comments on commit b943680

Please sign in to comment.