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

🐛 Citations in JATS can't use citation-js #340

Merged
merged 1 commit into from
Mar 30, 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
6 changes: 6 additions & 0 deletions .changeset/sour-ants-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'citation-js-utils': patch
'myst-to-jats': patch
---

Improve citation-js types, remove dependency on citation-js in jats export.
30 changes: 22 additions & 8 deletions packages/citation-js-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ import type { CitationFormatOptions } from 'citation-js';
import Cite from 'citation-js';
import sanitizeHtml from 'sanitize-html';

// This is duplicated in citation-js types, which are not exported
export type CitationJson = {
type?: 'article-journal' | string;
id: string;
author?: { given: string; family: string }[];
issued?: { 'date-parts': number[][] };
publisher?: string;
title?: string;
'citation-key'?: string;
'container-title'?: string;
abstract?: string;
DOI?: string;
ISBN?: string;
ISSN?: string;
issue?: string;
keyword?: string;
page?: string;
volume?: string;
} & Record<string, any>;

export type InlineNode = {
type: string;
value?: string;
Expand Down Expand Up @@ -48,13 +68,7 @@ const defaultString: CitationFormatOptions = {
style: CitationJSStyles.apa,
};

export function getCiteData(c: Cite) {
const cite = new Cite();
return cite.set(c).data[0];
}

export function getInlineCitation(c: Cite, kind: InlineCite, opts?: InlineOptions) {
const data = getCiteData(c);
export function getInlineCitation(data: CitationJson, kind: InlineCite, opts?: InlineOptions) {
let authors = data.author;
if (!authors || authors.length === 0) {
authors = data.editor;
Expand Down Expand Up @@ -95,7 +109,7 @@ export type CitationRenderer = Record<
render: (style?: CitationJSStyles) => string;
inline: (kind?: InlineCite, opts?: InlineOptions) => InlineNode[];
getDOI: () => string | undefined;
cite: any;
cite: CitationJson;
}
>;

Expand Down
13 changes: 12 additions & 1 deletion packages/citation-js-utils/types/citation-js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ declare module 'citation-js' {
lang: 'en-US' | 'fr-FR' | 'es-ES' | 'de-DE' | 'nl-NL';
};

// This is duplicated for export in index.ts
export type CitationJson = {
type?: 'article-journal' | string;
id: string;
author?: { given: string; family: string }[];
issued: { 'date-parts': number[][] };
issued?: { 'date-parts': number[][] };
publisher?: string;
title?: string;
'citation-key'?: string;
'container-title'?: string;
abstract?: string;
DOI?: string;
ISBN?: string;
ISSN?: string;
issue?: string;
keyword?: string;
page?: string;
volume?: string;
} & Record<string, any>;

export class Cite {
Expand Down
14 changes: 6 additions & 8 deletions packages/myst-to-jats/src/backmatter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { CitationRenderer } from 'citation-js-utils';
import { getCiteData } from 'citation-js-utils';
import type { CitationRenderer, CitationJson } from 'citation-js-utils';
import type { Element } from './types';

export function citeToJatsRef(key: string, cite: any): Element {
const data = getCiteData(cite);
export function citeToJatsRef(key: string, data: CitationJson): Element {
const publicationType = !data.type || data.type === 'article-journal' ? 'journal' : data.type;
const elements: Element[] = [];
const authors: Element[] | undefined = data.author
?.map((author: { given?: string; family?: string }) => {
?.map((author) => {
if (!author.given && !author.family) return undefined;
const authorChildren: Element[] = [];
if (author.family) {
Expand All @@ -31,7 +29,7 @@ export function citeToJatsRef(key: string, cite: any): Element {
};
return authorElem;
})
.filter((author: Element | undefined) => !!author);
.filter((author: Element | undefined): author is Element => !!author);
if (authors && authors.length) {
elements.push({
type: 'element',
Expand Down Expand Up @@ -59,8 +57,8 @@ export function citeToJatsRef(key: string, cite: any): Element {
elements.push({
type: 'element',
name: 'year',
attributes: { 'iso-8601-date': year },
elements: [{ type: 'text', text: year }],
attributes: { 'iso-8601-date': String(year) },
elements: [{ type: 'text', text: String(year) }],
});
}
if (data.DOI) {
Expand Down