Skip to content

Commit

Permalink
feat: Structured interoperable export.
Browse files Browse the repository at this point in the history
  • Loading branch information
about-code committed Oct 9, 2021
1 parent 97f8c8b commit 6d68976
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
13 changes: 9 additions & 4 deletions conf/v5/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,24 @@
,"glossaryFile": {
"type": "object"
,"properties": {
"file": {
"description": "Name of the glossary file. Conventional default is *glossary.md*. You can use a glob pattern to enable cross-linking of headings across multiple files. Note that 'termHint' and 'title' will be ignored if 'file' is a glob pattern."
"export": {
"description": "Export terms from the markdown file as a JSON glossary. Output will contain JSON-LD mappings onto http://w3.org/skos for interoperability with knowledge organization systems supporting SKOS."
,"type": "string"
,"since": "6.0.0"
}
,"termHint": {
"description": "A symbol to append to a link to denote that the term refers to a glossary term."
,"file": {
"description": "Name of the glossary file. Conventional default is *glossary.md*. You can use a glob pattern to enable cross-linking of headings across multiple files. Note that 'termHint' and 'title' will be ignored if 'file' is a glob pattern."
,"type": "string"
}
,"sort": {
"description": "If present, sort terms in output glossary. Default: None. See also i18n options."
,"type": "string"
,"enum": ["asc", "desc"]
}
,"termHint": {
"description": "A symbol to append to a link to denote that the term refers to a glossary term."
,"type": "string"
}
,"uri": {
"description": "A namespace or vocabulary identifier used as a prefix to construct URIs for glossary terms. Term URIs may be used to identify a concept within a Semantic Web or Linked Data Context or just to locate an external web page with a human readable definition. See also option `linking.baseUrl`."
,"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion lib/ast/with/term-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { collator } from "../../text/collator.js";
import { getHash8 } from "../../text/tools.js";


const SYMBOL = Symbol("term-definition");
const SYMBOL = "term-definition";
export class TermDefinitionNode {

/**
Expand Down
66 changes: 66 additions & 0 deletions lib/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { visit } from "unist-util-visit";
import { VFile } from "vfile";
import { TermDefinitionNode } from "./ast/with/term-definition.js";

const LD_CONTEXT = {
"@context": {
"@vocab": "https://about-code.github.io/vocab/glossarify-md/2021/10/#"
,"skos": "http://www.w3.org/2004/02/skos/core#"
,"dc": "http://purl.org/dc/terms/"
,"uri": "@id"
,"type": "@type"
,"title": "dc:title"
,"terms": {
"@container": "@index"
,"@reverse": "skos:inScheme"
}
,"Glossary": "skos:ConceptScheme"
,"Term": {
"@id": "skos:Concept"
,"@context": {
"label": "skos:prefLabel"
,"definition": "skos:definition"
,"aliases": {
"@id": "skos:altLabel"
,"@container": "@set"
}
,"abstract": "dc:abstract"
}
}
}
};

export function exporter(context) {
const { i18n } = context.conf;
return (tree, vFile) => {
if (vFile.isGlossary !== true) {
return;
}
if (! vFile.export) {
return;
}
const output = {
...LD_CONTEXT
,type: "Glossary"
,uri: vFile.uri
,title: vFile.title
,language: i18n.locale
,terms: {}
};
output["@context"]["@language"] = i18n.locale;
visit(tree, TermDefinitionNode.type, (node) => {
output.terms[node.uri] = {
type: "Term"
,uri: node.uri
,label: node.value
,definition: node.longDesc
,abstract: node.shortDesc
,aliases: [... node.aliases ]
};
});
context.vFiles.push(new VFile({
path: vFile.export
,value: JSON.stringify(output, null, 2)
}));
};
}
2 changes: 1 addition & 1 deletion lib/model/glossary.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Glossary {
this.file = data.file || "./glossary.md";

/** @type {VFile} */
this.vFile = data.vFile || null;
this.export = data.export || "";

/** @type {string} */
this.termHint = data.termHint || "";
Expand Down
4 changes: 3 additions & 1 deletion lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TermDefinitionNode } from "./ast/with/term-definition.js";
import { TermOccurrenceNode } from "./ast/with/term-occurrence.js";
import { TocInstructionNode } from "./ast/with/toc-instruction.js";
import { counter } from "./counter.js";
import { exporter } from "./exporter.js";
import { identifier } from "./identifier.js";
import { indexes as anchorIndexes } from "./index/anchors.js";
import { indexes as termsIndexes } from "./index/terms.js";
Expand Down Expand Up @@ -48,6 +49,7 @@ export function readGlossaries(context) {
.use(identifier, { context })
.use(terminator, { context })
.use(indexer, { context, indexes: [...termsIndexes] })
.use(exporter, context)
,cwd: baseDir
,files: toForwardSlash(
glossaries
Expand Down Expand Up @@ -136,7 +138,7 @@ export function readDocumentFiles(context) {
if (err) {
reject(err);
} else {
context.vFiles = [...uContext.files];
context.vFiles = [...context.vFiles, ...uContext.files];
resolve(context);
}
});
Expand Down

0 comments on commit 6d68976

Please sign in to comment.