myst-to-jats
Version:
Export from MyST Markdown to JATS
56 lines (55 loc) • 2.22 kB
JavaScript
/**
* Replace ids of affiliations associated with authors
*
* This does not change the ids of affiliations that are
* not referenced anywhere or only referenced in funding,
* as those ids do not come through to JATS.
*
* Also, it treats each frontmatter separately, so if one
* affiliation appears in multiple articles/sub-articles,
* it will be given a different id for each and be part
* of the JATS frontmatter for each.
*/
export function affiliationIdTransform(frontmatters, idPrefix) {
let affCount = 0;
frontmatters.forEach((frontmatter) => {
var _a, _b, _c;
const idLookup = {};
// Only modify ids of affiliations associated with authors
(_a = frontmatter.authors) === null || _a === void 0 ? void 0 : _a.forEach((auth) => {
var _a;
if (!((_a = auth.affiliations) === null || _a === void 0 ? void 0 : _a.length))
return;
auth.affiliations = auth.affiliations.map((aff) => {
if (idLookup[aff]) {
return idLookup[aff];
}
else {
affCount += 1;
const id = typeof idPrefix === 'function' ? idPrefix(affCount) : `${idPrefix}-${affCount}`;
idLookup[aff] = id;
return id;
}
});
});
// Replace affiliation values in funding sources
(_b = frontmatter.funding) === null || _b === void 0 ? void 0 : _b.forEach((funding) => {
var _a;
(_a = funding.awards) === null || _a === void 0 ? void 0 : _a.forEach((award) => {
if (!award.sources)
return;
award.sources = award.sources.map((source) => {
if (idLookup[source])
return idLookup[source];
return source;
});
});
});
// Replace affiliation ids in affiliation list
(_c = frontmatter.affiliations) === null || _c === void 0 ? void 0 : _c.forEach((aff) => {
if (aff.id && idLookup[aff.id]) {
aff.id = idLookup[aff.id];
}
});
});
}