Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👩‍🔬 Only add affiliations associated with authors to JATS and myst-templates #662

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -149,7 +149,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 @@ -217,16 +216,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;
Comment on lines -227 to 223
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, can bring something else back when we do "editors" etc.

}

Expand Down Expand Up @@ -298,84 +291,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
Loading