Skip to content

Commit

Permalink
👩‍🔬 Only add affiliations associated with authors to JATS and myst-te…
Browse files Browse the repository at this point in the history
…mplates (#662)

* 🎓 JATS only includes authors as contrib and author affiliations as aff

* 🎓 Only add author affiliations/contrib to template doc

* 👩‍🔬 Remove author contrib id

---------

Co-authored-by: Rowan Cockett <[email protected]>
  • Loading branch information
fwkoch and rowanc1 authored Oct 13, 2023
1 parent aecf616 commit d25debf
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 153 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-coins-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-templates': patch
---

Only add author affiliations/contrib to template doc
5 changes: 5 additions & 0 deletions .changeset/light-shirts-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-jats': patch
---

JATS only includes authors as top-level contribs
5 changes: 5 additions & 0 deletions .changeset/metal-pans-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-jats': patch
---

Remove contrib id, authors cannot be referenced.
5 changes: 5 additions & 0 deletions .changeset/purple-dancers-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-jats': patch
---

JATS only includes author affiliations in aff list
115 changes: 115 additions & 0 deletions packages/myst-templates/src/frontmatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,119 @@ describe('extendFrontmatter', () => {
},
]);
});
it('affiliations are only used from authors', async () => {
const frontmatter: PageFrontmatter = {
authors: [
{
name: 'John Doe',
affiliations: ['aff1'],
},
{
name: 'Jane Doe',
affiliations: ['aff1', 'col2'],
},
],
affiliations: [
{
id: 'aff1',
name: 'univ 1',
},
{
id: 'aff2',
name: 'univ 2',
},
{
id: 'col1',
name: 'group 1',
collaboration: true,
},
{
id: 'col2',
name: 'group 2',
collaboration: true,
},
],
};
const doc = extendFrontmatter(frontmatter);
expect(doc.authors).toEqual([
{
name: 'John Doe',
given_name: 'John',
surname: 'Doe',
index: 1,
letter: 'A',
affiliations: [
{
id: 'aff1',
name: 'univ 1',
value: {
id: 'aff1',
name: 'univ 1',
},
index: 1,
letter: 'A',
},
],
},
{
name: 'Jane Doe',
given_name: 'Jane',
surname: 'Doe',
index: 2,
letter: 'B',
affiliations: [
{
id: 'aff1',
name: 'univ 1',
value: {
id: 'aff1',
name: 'univ 1',
},
index: 1,
letter: 'A',
},
],
collaborations: [
{
id: 'col2',
name: 'group 2',
collaboration: true,
value: {
id: 'col2',
name: 'group 2',
collaboration: true,
},
index: 1,
letter: 'A',
},
],
},
]);
expect(doc.affiliations).toEqual([
{
id: 'aff1',
name: 'univ 1',
value: {
id: 'aff1',
name: 'univ 1',
},
index: 1,
letter: 'A',
},
]);
expect(doc.collaborations).toEqual([
{
id: 'col2',
name: 'group 2',
collaboration: true,
value: {
id: 'col2',
name: 'group 2',
collaboration: true,
},
index: 1,
letter: 'A',
},
]);
});
});
30 changes: 17 additions & 13 deletions packages/myst-templates/src/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Contributor, PageFrontmatter } from 'myst-frontmatter';
import type { Affiliation, Contributor, PageFrontmatter } from 'myst-frontmatter';
import type { RendererAuthor, RendererDoc, ValueAndIndex } from './types.js';

const ALPHA = 'ABCDEFGHIJKLMNOPQURSUVWXYZ';
Expand Down Expand Up @@ -71,18 +71,22 @@ function addIndicesToAuthors(

export function extendFrontmatter(frontmatter: PageFrontmatter): RendererDoc {
const datetime = frontmatter.date ? new Date(frontmatter.date) : new Date();
const affiliations =
frontmatter.affiliations
?.filter((aff) => aff.id && !aff.collaboration)
.map((aff, index) => {
return { ...aff, value: aff, ...indexAndLetter(index) };
}) ?? [];
const collaborations =
frontmatter.affiliations
?.filter((aff) => aff.id && aff.collaboration)
.map((aff, index) => {
return { ...aff, value: aff, ...indexAndLetter(index) };
}) ?? [];
// Only add affiliations from authors, not contributors
const affIds = [
...new Set(frontmatter.authors?.map((auth) => auth.affiliations ?? []).flat() ?? []),
];
const affiliations = affIds
.map((id) => frontmatter.affiliations?.find((aff) => aff.id === id))
.filter((aff): aff is Affiliation => !!aff?.id && !aff.collaboration)
.map((aff, index) => {
return { ...aff, value: aff, ...indexAndLetter(index) };
});
const collaborations = affIds
.map((id) => frontmatter.affiliations?.find((aff) => aff.id === id))
.filter((aff): aff is Affiliation => !!aff?.id && !!aff.collaboration)
.map((aff, index) => {
return { ...aff, value: aff, ...indexAndLetter(index) };
});
const doc: RendererDoc = {
...frontmatter,
date: {
Expand Down
170 changes: 86 additions & 84 deletions packages/myst-to-jats/src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export function getArticleAuthors(frontmatter: ProjectFrontmatter): Element[] {
const attributes: Record<string, any> = {};
const elements: Element[] = [];
if (type) attributes['contrib-type'] = type;
if (author.id) attributes.id = author.id;
if (author.corresponding) attributes.corresp = 'yes';
if (author.deceased) attributes['deceased'] = 'yes';
if (author.equal_contributor != null) {
Expand Down Expand Up @@ -228,16 +227,10 @@ export function getArticleAuthors(frontmatter: ProjectFrontmatter): Element[] {
const authorContribs = (frontmatter.authors ?? []).map((author): Element => {
return generateContrib(author, 'author');
});
const otherContribs = (frontmatter.contributors ?? []).map((contributor): Element => {
return generateContrib(contributor);
});
const contribGroups: Element[] = [];
if (authorContribs.length) {
contribGroups.push({ type: 'element', name: 'contrib-group', elements: authorContribs });
}
if (otherContribs.length) {
contribGroups.push({ type: 'element', name: 'contrib-group', elements: otherContribs });
}
return contribGroups;
}

Expand Down Expand Up @@ -309,84 +302,93 @@ function instWrapElementsFromAffiliation(affiliation: Affiliation, includeDept =
}

export function getArticleAffiliations(frontmatter: ProjectFrontmatter): Element[] {
const affs = frontmatter.affiliations?.map((affiliation): Element => {
const elements: Element[] = [];
const attributes: Record<string, any> = {};
if (affiliation.id) {
attributes.id = affiliation.id;
}
elements.push(...instWrapElementsFromAffiliation(affiliation));
if (affiliation.address) {
elements.push({
type: 'element',
name: 'addr-line',
elements: [{ type: 'text', text: affiliation.address }],
});
}
if (affiliation.city) {
elements.push({
type: 'element',
name: 'city',
elements: [{ type: 'text', text: affiliation.city }],
});
}
if (affiliation.state) {
elements.push({
type: 'element',
name: 'state',
elements: [{ type: 'text', text: affiliation.state }],
});
}
if (affiliation.postal_code) {
elements.push({
type: 'element',
name: 'postal-code',
elements: [{ type: 'text', text: affiliation.postal_code }],
});
}
if (affiliation.country) {
elements.push({
type: 'element',
name: 'country',
elements: [{ type: 'text', text: affiliation.country }],
});
}
if (affiliation.phone) {
elements.push({
type: 'element',
name: 'phone',
elements: [{ type: 'text', text: affiliation.phone }],
});
}
if (affiliation.fax) {
elements.push({
type: 'element',
name: 'fax',
elements: [{ type: 'text', text: affiliation.fax }],
});
}
if (affiliation.email) {
elements.push({
type: 'element',
name: 'email',
elements: [{ type: 'text', text: affiliation.email }],
});
}
if (affiliation.url) {
elements.push({
if (!frontmatter.affiliations?.length) return [];
// Only add affiliations from authors, not contributors
const affIds = [
...new Set(frontmatter.authors?.map((auth) => auth.affiliations ?? []).flat() ?? []),
];
if (!affIds?.length) return [];
const affs = affIds
.map((id) => frontmatter.affiliations?.find((aff) => aff.id === id))
.filter((aff): aff is Affiliation => !!aff)
.map((affiliation): Element => {
const elements: Element[] = [];
const attributes: Record<string, any> = {};
if (affiliation.id) {
attributes.id = affiliation.id;
}
elements.push(...instWrapElementsFromAffiliation(affiliation));
if (affiliation.address) {
elements.push({
type: 'element',
name: 'addr-line',
elements: [{ type: 'text', text: affiliation.address }],
});
}
if (affiliation.city) {
elements.push({
type: 'element',
name: 'city',
elements: [{ type: 'text', text: affiliation.city }],
});
}
if (affiliation.state) {
elements.push({
type: 'element',
name: 'state',
elements: [{ type: 'text', text: affiliation.state }],
});
}
if (affiliation.postal_code) {
elements.push({
type: 'element',
name: 'postal-code',
elements: [{ type: 'text', text: affiliation.postal_code }],
});
}
if (affiliation.country) {
elements.push({
type: 'element',
name: 'country',
elements: [{ type: 'text', text: affiliation.country }],
});
}
if (affiliation.phone) {
elements.push({
type: 'element',
name: 'phone',
elements: [{ type: 'text', text: affiliation.phone }],
});
}
if (affiliation.fax) {
elements.push({
type: 'element',
name: 'fax',
elements: [{ type: 'text', text: affiliation.fax }],
});
}
if (affiliation.email) {
elements.push({
type: 'element',
name: 'email',
elements: [{ type: 'text', text: affiliation.email }],
});
}
if (affiliation.url) {
elements.push({
type: 'element',
name: 'ext-link',
attributes: { 'ext-link-type': 'uri', 'xlink:href': affiliation.url },
elements: [{ type: 'text', text: affiliation.url }],
});
}
return {
type: 'element',
name: 'ext-link',
attributes: { 'ext-link-type': 'uri', 'xlink:href': affiliation.url },
elements: [{ type: 'text', text: affiliation.url }],
});
}
return {
type: 'element',
name: 'aff',
attributes,
elements,
};
});
name: 'aff',
attributes,
elements,
};
});
return affs ? affs : [];
}

Expand Down
Loading

0 comments on commit d25debf

Please sign in to comment.