Skip to content
Closed
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
39 changes: 38 additions & 1 deletion src/jats/__tests__/jats-exporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { parseJATSArticle } from '../importer/parse-jats-article'
import { DEFAULT_CSL_OPTIONS } from './citations'
import { sectionCategories } from './data/section-categories'
import { readAndParseFixture } from './files'
import { BibliographyItemNode, isBibliographyItemNode } from '../../schema'
import {
BibliographyItemNode,
isBibliographyItemNode,
isCitationNode,
} from '../../schema'

const parseXMLWithDTD = (data: string) =>
parseXml(data, {
Expand Down Expand Up @@ -197,6 +201,39 @@ describe('JATS exporter', () => {
)
})

test('export uncited references when includeUncitedReferences is set', async () => {
const input = await readAndParseFixture('jats-document.xml')
const node = parseJATSArticle(input, sectionCategories)

const bibliographyItemIDs = new Set<string>()
const citedIDs = new Set<string>()
node.descendants((n) => {
if (isBibliographyItemNode(n)) {
bibliographyItemIDs.add(n.attrs.id)
}
if (isCitationNode(n)) {
n.attrs.rids.forEach((rid: string) => citedIDs.add(rid))
}
})

const withoutUncited = await new JATSExporter().serializeToJATS(node, {
csl: DEFAULT_CSL_OPTIONS,
})
const withUncited = await new JATSExporter().serializeToJATS(node, {
csl: DEFAULT_CSL_OPTIONS,
includeUncitedReferences: true,
})

const countRefs = (xml: string) =>
parseXMLWithDTD(xml).find('//ref-list/ref').length

expect(countRefs(withoutUncited)).toBe(citedIDs.size)
expect(countRefs(withUncited)).toBe(bibliographyItemIDs.size)

const { errors } = parseXMLWithDTD(withUncited)
expect(errors).toHaveLength(0)
})

test('export footnotes', async () => {
const transformer = new JATSExporter()
const input = await readAndParseFixture('jats-fn-group.xml')
Expand Down
14 changes: 11 additions & 3 deletions src/jats/exporter/jats-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export type CSLOptions = {
export type ExportOptions = {
version?: Version
csl: CSLOptions
includeUncitedReferences?: boolean
}

export class JATSExporter {
Expand Down Expand Up @@ -191,7 +192,7 @@ export class JATSExporter {
): Promise<string> => {
this.manuscriptNode = manuscriptNode
this.populateNodesMap()
this.initCiteprocEngine(options.csl)
this.initCiteprocEngine(options)
this.createSerializer()
const versionIds = selectVersionIds(options.version ?? '1.2')

Expand Down Expand Up @@ -283,7 +284,8 @@ export class JATSExporter {
element.appendChild(caption)
}
}
private initCiteprocEngine = (csl: CSLOptions) => {
private initCiteprocEngine = (options: ExportOptions) => {
const { includeUncitedReferences, csl } = options
const bibitems: Map<string, CSL.Data> = new Map()
const citations: Map<string, Citeproc.Citation> = new Map()

Expand Down Expand Up @@ -313,7 +315,13 @@ export class JATSExporter {
)
engine.setOutputFormat('jats')

const output = engine.rebuildProcessorState([...citations.values()])
const uncitedItemIDs = includeUncitedReferences ? [...bibitems.keys()] : []

const output = engine.rebuildProcessorState(
[...citations.values()],
undefined,
uncitedItemIDs
)

this.engine = engine
this.renderedCitations = new Map(output.map((i) => [i[0], i[2]]))
Expand Down
40 changes: 40 additions & 0 deletions src/schema/__tests__/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,46 @@ export const v2_3_20 = {
},
],
},
{
type: 'figure_element',
attrs: {
id: 'MPFigureElement:ca6dac1a-b6ae-4227-be6e-75119186224b',
label: '',
sizeFraction: 0,
suppressCaption: true,
suppressTitle: true,
dataTracked: null,
},
content: [
{
type: 'figure',
attrs: {
id: 'MPFigure:d377ead2-6849-4597-9caa-f3e6b73bb629',
src: 'chemoji/chemoji8.png',
contentType: 'image/png',
dataTracked: null,
},
},
{
type: 'figcaption',
attrs: {
dataTracked: null,
},
},
{
type: 'listing',
attrs: {
id: '',
contents: '',
language: '',
languageKey: 'null',
isExpanded: false,
isExecuting: false,
dataTracked: null,
},
},
],
},
{
type: 'table_element',
attrs: {
Expand Down
32 changes: 20 additions & 12 deletions src/schema/migration/migration-scripts/4.3.34.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,26 @@ class Migration4334 implements MigrationScript {
const foundTitle = (figCaption?.content || []).find(
(n) => n.type === 'caption_title'
)
const foundCaption = (figCaption?.content || []).filter(
(node) => node.type === 'caption')
.reduce<JSONProsemirrorNode>(
(caption, { content }) => ({
...caption,
content: [
...(caption.content || []),
{ type: 'text_block', content, attrs: {} },
],
}),
{ type: 'caption', content: [], attrs: {} }
)
const captionNodes = (figCaption?.content || []).filter(
(node) => node.type === 'caption'
)
const foundCaption =
captionNodes.length === 0
? {
type: 'caption',
content: [{ type: 'text_block', attrs: {} }],
attrs: {},
}
: captionNodes.reduce<JSONProsemirrorNode>(
(caption, { content }) => ({
...caption,
content: [
...(caption.content || []),
{ type: 'text_block', content, attrs: {} },
],
}),
{ type: 'caption', content: [], attrs: {} }
)
Comment on lines +65 to +81

const cleanContent = node.content.filter((n) => n.type !== 'figcaption')

Expand Down