-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
739ad35
commit 4447f72
Showing
6 changed files
with
244 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { readFile } from "node:fs"; | ||
import path from "node:path"; | ||
import { VFile } from "vfile"; | ||
|
||
const SKOS = "http://www.w3.org/2004/02/skos/core#"; | ||
const SKOS_= "skos"; | ||
const DC = "http://purl.org/dc/terms/"; | ||
const DC_= "dc"; | ||
const LD_CONTEXT = { | ||
[SKOS_]: SKOS | ||
,[DC_]: DC | ||
}; | ||
const SkosConceptSchemeType = { | ||
uri: `${SKOS_}:ConceptScheme` | ||
,title: `${DC_}:title` | ||
}; | ||
const SkosConceptType = { | ||
uri: `${SKOS_}:Concept` | ||
,prefLabel: `${SKOS_}:prefLabel` | ||
,altLabels: `${SKOS_}:altLabel` | ||
,definition: `${SKOS_}:definition` | ||
,abstract: `${DC_}:abstract` | ||
}; | ||
/** | ||
* @typedef {import("./model/context.js").Context } Context | ||
* | ||
* @param {Context} context | ||
*/ | ||
export function importGlossaries(context) { | ||
const {baseDir, glossaries} = context.conf; | ||
return Promise.all(glossaries.map(g => { | ||
const importConf = g.import; | ||
if (! (importConf && g.file)) { | ||
return Promise.resolve(); | ||
} else if (g.file.match(/[*|{}()]/g)) { | ||
console.error( | ||
`⚠ Config "glossaries": [{ "file": "${g.file}", "import": ... }]:` | ||
+ "\nAttempts to write imported terms to multiple files using a glob." | ||
+ "\nNot supported. Skipping import." | ||
); | ||
g.import = undefined; | ||
return Promise.resolve(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const jsonFileName = importConf.file; | ||
const jsonFile = path.resolve(baseDir, jsonFileName); | ||
readFile(jsonFile, (err, jsonStr) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
jsonParse(jsonStr).then(jsonData => { | ||
try { | ||
const vFile = new VFile({ | ||
path: g.file | ||
,value: json2md(jsonData) | ||
}); | ||
g.uri = jsonData.uri; | ||
g.title = jsonData.title; | ||
g.vFile = vFile; | ||
context.writeFiles.push(vFile); | ||
resolve(); | ||
} catch (parseErr) { | ||
console.error(`${jsonFileName}: ${err.message}`); | ||
reject(parseErr); | ||
} | ||
}); | ||
}); | ||
}); | ||
})).then(() => context); | ||
} | ||
|
||
|
||
function json2md(imported) { | ||
const terms = imported.terms || {}; | ||
const termsStr = Object | ||
.keys(terms) | ||
.reduce(getTermReducer(terms), ""); | ||
|
||
const glossaryStr = `# ${imported.title || "Glossary" }\n${termsStr}`; | ||
return glossaryStr; | ||
} | ||
|
||
function getTermReducer(terms) { | ||
return function (acc, uri) { | ||
const { value = "", definition = "", aliases = [] } = terms[uri]; | ||
const attrs = { | ||
uri: uri | ||
,aliases: aliases.join(", ") | ||
}; | ||
const termAttrs = ["<!--", JSON.stringify(attrs, null, 2), "-->"].join(""); | ||
if (value) { | ||
const md = | ||
` | ||
## ${value} | ||
${termAttrs} | ||
${definition} | ||
`; | ||
return md; | ||
} else { | ||
return ""; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Parses the given JSON string. | ||
* Uses plain JSON.parse() by default to deserialize JSON import data. | ||
* Without 'jsonld' available it expects the data to align with glossarify-md's | ||
* import format. | ||
* | ||
* If installed uses 'jsonld' to map JSON object onto a graph and interpret the | ||
* JavaScript object using SKOS vocabulary. This enables to read other SKOS | ||
* JSON-LD data formats. Installing jsonld is supposed to be done by users who | ||
* wanto to import other SKOS-JSON formats with LD-Mappings. | ||
*/ | ||
function jsonParse(jsonStr) { | ||
try { | ||
const jsonData = JSON.parse(jsonStr); | ||
const glossary = import("jsonld") | ||
.then(module => module.default) | ||
.then(jsonld => { | ||
jsonld.documentLoader = (url) => { | ||
console.error(new Error(`${url}:\nLoading external JSON-LD documents not implemented.\n`)); | ||
}; | ||
return jsonld; | ||
}) | ||
.then(jsonld => jsonld.flatten(jsonData, LD_CONTEXT)) | ||
.then(flattened => flattened["@graph"]) | ||
.then(ldNodes => { | ||
const conceptScheme = ldNodes.find(ldNode => ldNode["@type"] === SkosConceptSchemeType.uri); | ||
return { | ||
// Map skos:ConceptScheme onto our 'Glossary' import format type | ||
type: "Glossary" | ||
,uri: ldValue(conceptScheme["@id"]) | ||
,title: ldValue(conceptScheme[SkosConceptSchemeType.title]) | ||
,terms: ldNodes | ||
.filter(ldNode => ldNode["@type"] === SkosConceptType.uri) | ||
.map(conceptInstance => { | ||
// Map skos:Concept onto our 'Term' import format type | ||
let aliases = ldValue(conceptInstance[SkosConceptType.altLabels]) || []; | ||
if (aliases && !Array.isArray(aliases)) { | ||
aliases = [aliases]; | ||
} | ||
return { | ||
type: "Term" | ||
,uri: ldValue(conceptInstance["@id"]) | ||
,value: ldValue(conceptInstance[SkosConceptType.prefLabel]) | ||
,definition: ldValue(conceptInstance[SkosConceptType.definition]) | ||
,aliases: aliases | ||
}; | ||
}) | ||
}; | ||
}); | ||
|
||
return glossary.catch(err => { | ||
// console.log(err); | ||
return jsonData; | ||
}); | ||
} catch (err) { | ||
Promise.reject(err); | ||
} | ||
} | ||
|
||
function ldValue(v) { | ||
if (Array.isArray(v)) { | ||
if (v.length >= 1) { | ||
return v[0]["@value"]; | ||
} | ||
} else if (v && typeof v === "object") { | ||
return v["@value"]; | ||
} else { | ||
return v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters