myst-to-jats
Version:
Export from MyST Markdown to JATS
169 lines (168 loc) • 5.65 kB
JavaScript
export function citeToJatsRef(state, key, data) {
var _a, _b, _c, _d;
const publicationType = !data.type || data.type === 'article-journal' ? 'journal' : data.type;
const elements = [];
const authors = (_a = data.author) === null || _a === void 0 ? void 0 : _a.map((author) => {
if (!author.given && !author.family)
return undefined;
const authorChildren = [];
if (author.family) {
authorChildren.push({
type: 'element',
name: 'surname',
elements: [{ type: 'text', text: author.family }],
});
}
if (author.given) {
authorChildren.push({
type: 'element',
name: 'given-names',
elements: [{ type: 'text', text: author.given }],
});
}
const authorElem = {
type: 'element',
name: 'name',
elements: authorChildren,
};
return authorElem;
}).filter((author) => !!author);
if (authors && authors.length) {
elements.push({
type: 'element',
name: 'person-group',
attributes: { 'person-group-type': 'author' },
elements: authors,
});
}
if (data['container-title']) {
elements.push({
type: 'element',
name: 'source',
elements: [{ type: 'text', text: data['container-title'] }],
});
}
const year = (_d = (_c = (_b = data.issued) === null || _b === void 0 ? void 0 : _b['date-parts']) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d[0];
if (year) {
elements.push({
type: 'element',
name: 'year',
attributes: { 'iso-8601-date': String(year) },
elements: [{ type: 'text', text: String(year) }],
});
}
if (data.DOI) {
elements.push({
type: 'element',
name: 'pub-id',
attributes: { 'pub-id-type': 'doi' },
elements: [{ type: 'text', text: data.DOI }],
});
}
if (data.volume) {
elements.push({
type: 'element',
name: 'volume',
elements: [{ type: 'text', text: data.volume }],
});
}
if (data.issue) {
elements.push({
type: 'element',
name: 'issue',
elements: [{ type: 'text', text: data.issue }],
});
}
if (data.page) {
const [firstPage, lastPage] = data.page.split('-');
if (firstPage) {
elements.push({
type: 'element',
name: 'fpage',
elements: [{ type: 'text', text: firstPage }],
});
}
if (lastPage) {
elements.push({
type: 'element',
name: 'lpage',
elements: [{ type: 'text', text: lastPage }],
});
}
}
if (data.ISSN) {
elements.push({
type: 'element',
name: 'issn',
elements: [{ type: 'text', text: data.ISSN }],
});
}
// <element-citation> must have at least one child element to pass validation
// This ensures that if the citation is totally empty, it is given an empty title.
if (data.title || elements.length === 0) {
elements.unshift({
type: 'element',
name: 'article-title',
elements: data.title ? [{ type: 'text', text: data.title }] : [],
});
}
return {
type: 'element',
name: 'ref',
attributes: { id: key },
elements: [
{
type: 'element',
name: 'element-citation',
attributes: { 'publication-type': publicationType },
elements,
},
],
};
}
export function getRefList(state, order, citations) {
const elements = order === null || order === void 0 ? void 0 : order.map((key) => {
if (!(citations === null || citations === void 0 ? void 0 : citations[key])) {
state.warn(`unknown citation ${key}`);
return undefined;
}
return citeToJatsRef(state, key, citations[key].cite);
}).filter((e) => !!e);
if (!(elements === null || elements === void 0 ? void 0 : elements.length))
return [];
return [{ type: 'element', name: 'ref-list', elements }];
}
export function getFootnotes(footnotes) {
if (!(footnotes === null || footnotes === void 0 ? void 0 : footnotes.length))
return [];
return [{ type: 'element', name: 'fn-group', elements: footnotes }];
}
export function getExpressions(expressions) {
if (!(expressions === null || expressions === void 0 ? void 0 : expressions.length))
return [];
return [
{
type: 'element',
name: 'notes',
attributes: { 'notes-type': 'expressions' },
elements: expressions,
},
];
}
export function getBack(state, { citations, footnotes, expressions, referenceOrder, }) {
var _a;
const elements = [
...((_a = state.data.backSections) !== null && _a !== void 0 ? _a : []),
...getRefList(state, referenceOrder, citations),
...getFootnotes(footnotes),
...getExpressions(expressions),
...(state.data.acknowledgments ? [state.data.acknowledgments] : []),
// app-group
// bio
// glossary
// notes
];
if (!elements.length)
return [];
return [{ type: 'element', name: 'back', elements }];
}