@manuscripts/transform 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -122,7 +122,7 @@ const parseJATSArticle = (doc) => {
122
122
  const type = article.getAttribute('article-type');
123
123
  manuscript.articleType = type || 'other';
124
124
  }
125
- if (references) {
125
+ if (references && references.items.size) {
126
126
  models.push(...createBibliographyModels(references));
127
127
  }
128
128
  if (marks.length) {
@@ -18,8 +18,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.JATSExporter = exports.createCounter = void 0;
21
+ exports.JATSExporter = exports.buildCitations = exports.createCounter = void 0;
22
22
  const json_schema_1 = require("@manuscripts/json-schema");
23
+ const library_1 = require("@manuscripts/library");
23
24
  const debug_1 = __importDefault(require("debug"));
24
25
  const prosemirror_model_1 = require("prosemirror-model");
25
26
  const w3c_xmlserializer_1 = __importDefault(require("w3c-xmlserializer"));
@@ -115,12 +116,23 @@ const chooseRefType = (objectType) => {
115
116
  }
116
117
  };
117
118
  const sortContributors = (a, b) => Number(a.priority) - Number(b.priority);
119
+ const buildCitations = (citations) => citations.map((citation) => ({
120
+ citationID: citation.attrs.id,
121
+ citationItems: citation.attrs.rids.map((rid) => ({
122
+ id: rid,
123
+ })),
124
+ properties: {
125
+ noteIndex: 0,
126
+ },
127
+ }));
128
+ exports.buildCitations = buildCitations;
118
129
  class JATSExporter {
119
130
  constructor() {
120
- this.serializeToJATS = async (fragment, modelMap, manuscriptID, options = {}) => {
121
- const { version = '1.2', doi, id, frontMatterOnly = false, links, idGenerator, mediaPathGenerator, } = options;
131
+ this.serializeToJATS = async (fragment, modelMap, manuscriptID, options) => {
132
+ const { version = '1.2', doi, id, frontMatterOnly = false, links, idGenerator, mediaPathGenerator, csl, } = options;
122
133
  this.modelMap = modelMap;
123
134
  this.models = Array.from(this.modelMap.values());
135
+ this.generateCitationTexts(fragment, csl);
124
136
  this.createSerializer();
125
137
  const versionIds = (0, jats_versions_1.selectVersionIds)(version);
126
138
  this.document = document.implementation.createDocument(null, 'article', document.implementation.createDocumentType('article', versionIds.publicId, versionIds.systemId));
@@ -462,10 +474,11 @@ class JATSExporter {
462
474
  refList = this.document.createElement('ref-list');
463
475
  }
464
476
  back.appendChild(refList);
465
- const bibliographyItems = this.models.filter((0, object_types_1.hasObjectType)(json_schema_1.ObjectTypes.BibliographyItem));
466
- for (const bibliographyItem of bibliographyItems) {
477
+ const [meta] = this.citationProvider.makeBibliography();
478
+ for (const id of meta.entry_ids) {
479
+ const bibliographyItem = this.modelMap.get(id[0]);
467
480
  const ref = this.document.createElement('ref');
468
- ref.setAttribute('id', normalizeID(bibliographyItem._id));
481
+ ref.setAttribute('id', normalizeID(id[0]));
469
482
  const updateCitationPubType = (citationEl, pubType) => {
470
483
  if (pubType) {
471
484
  switch (pubType) {
@@ -638,12 +651,11 @@ class JATSExporter {
638
651
  const xref = this.document.createElement('xref');
639
652
  xref.setAttribute('ref-type', 'bibr');
640
653
  xref.setAttribute('rid', normalizeID(rids.join(' ')));
641
- if (citation.attrs.contents) {
642
- const text = (0, html_1.textFromHTML)(node.attrs.contents);
643
- if (text !== null && text.length) {
644
- xref.textContent = text;
645
- }
654
+ const citationTextContent = this.citationTexts.get(node.attrs.id);
655
+ if (!citationTextContent) {
656
+ throw new Error(`No citation text found for ${node.attrs.id}`);
646
657
  }
658
+ xref.textContent = (0, html_1.textFromHTML)(citationTextContent);
647
659
  return xref;
648
660
  },
649
661
  cross_reference: (node) => {
@@ -1554,6 +1566,27 @@ class JATSExporter {
1554
1566
  return name;
1555
1567
  };
1556
1568
  }
1569
+ generateCitations(fragment) {
1570
+ const nodes = [];
1571
+ fragment.descendants((node) => {
1572
+ if ((0, schema_1.isCitationNode)(node)) {
1573
+ nodes.push(node);
1574
+ }
1575
+ });
1576
+ return (0, exports.buildCitations)(nodes);
1577
+ }
1578
+ generateCitationTexts(fragment, csl) {
1579
+ this.citationTexts = new Map();
1580
+ this.citationProvider = new library_1.CitationProvider({
1581
+ getLibraryItem: (id) => this.modelMap.get(id),
1582
+ locale: csl.locale,
1583
+ citationStyle: csl.style,
1584
+ });
1585
+ const citations = this.generateCitations(fragment);
1586
+ this.citationProvider.rebuildState(citations).forEach(([id, , output]) => {
1587
+ this.citationTexts.set(id, output);
1588
+ });
1589
+ }
1557
1590
  buildKeywords(articleMeta) {
1558
1591
  const keywords = [...this.modelMap.values()].filter((model) => model.objectType === json_schema_1.ObjectTypes.Keyword);
1559
1592
  const keywordGroups = new Map();
@@ -117,7 +117,7 @@ export const parseJATSArticle = (doc) => {
117
117
  const type = article.getAttribute('article-type');
118
118
  manuscript.articleType = type || 'other';
119
119
  }
120
- if (references) {
120
+ if (references && references.items.size) {
121
121
  models.push(...createBibliographyModels(references));
122
122
  }
123
123
  if (marks.length) {
@@ -14,13 +14,14 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { ObjectTypes, } from '@manuscripts/json-schema';
17
+ import { CitationProvider } from '@manuscripts/library';
17
18
  import debug from 'debug';
18
19
  import { DOMParser, DOMSerializer } from 'prosemirror-model';
19
20
  import serializeToXML from 'w3c-xmlserializer';
20
21
  import { nodeFromHTML, textFromHTML } from '../lib/html';
21
22
  import { normalizeStyleName } from '../lib/styled-content';
22
23
  import { iterateChildren } from '../lib/utils';
23
- import { schema, } from '../schema';
24
+ import { isCitationNode, schema, } from '../schema';
24
25
  import { generateAttachmentFilename } from '../transformer/filename';
25
26
  import { buildTargets } from '../transformer/labels';
26
27
  import { isExecutableNodeType, isNodeType } from '../transformer/node-types';
@@ -108,12 +109,22 @@ const chooseRefType = (objectType) => {
108
109
  }
109
110
  };
110
111
  const sortContributors = (a, b) => Number(a.priority) - Number(b.priority);
112
+ export const buildCitations = (citations) => citations.map((citation) => ({
113
+ citationID: citation.attrs.id,
114
+ citationItems: citation.attrs.rids.map((rid) => ({
115
+ id: rid,
116
+ })),
117
+ properties: {
118
+ noteIndex: 0,
119
+ },
120
+ }));
111
121
  export class JATSExporter {
112
122
  constructor() {
113
- this.serializeToJATS = async (fragment, modelMap, manuscriptID, options = {}) => {
114
- const { version = '1.2', doi, id, frontMatterOnly = false, links, idGenerator, mediaPathGenerator, } = options;
123
+ this.serializeToJATS = async (fragment, modelMap, manuscriptID, options) => {
124
+ const { version = '1.2', doi, id, frontMatterOnly = false, links, idGenerator, mediaPathGenerator, csl, } = options;
115
125
  this.modelMap = modelMap;
116
126
  this.models = Array.from(this.modelMap.values());
127
+ this.generateCitationTexts(fragment, csl);
117
128
  this.createSerializer();
118
129
  const versionIds = selectVersionIds(version);
119
130
  this.document = document.implementation.createDocument(null, 'article', document.implementation.createDocumentType('article', versionIds.publicId, versionIds.systemId));
@@ -455,10 +466,11 @@ export class JATSExporter {
455
466
  refList = this.document.createElement('ref-list');
456
467
  }
457
468
  back.appendChild(refList);
458
- const bibliographyItems = this.models.filter(hasObjectType(ObjectTypes.BibliographyItem));
459
- for (const bibliographyItem of bibliographyItems) {
469
+ const [meta] = this.citationProvider.makeBibliography();
470
+ for (const id of meta.entry_ids) {
471
+ const bibliographyItem = this.modelMap.get(id[0]);
460
472
  const ref = this.document.createElement('ref');
461
- ref.setAttribute('id', normalizeID(bibliographyItem._id));
473
+ ref.setAttribute('id', normalizeID(id[0]));
462
474
  const updateCitationPubType = (citationEl, pubType) => {
463
475
  if (pubType) {
464
476
  switch (pubType) {
@@ -631,12 +643,11 @@ export class JATSExporter {
631
643
  const xref = this.document.createElement('xref');
632
644
  xref.setAttribute('ref-type', 'bibr');
633
645
  xref.setAttribute('rid', normalizeID(rids.join(' ')));
634
- if (citation.attrs.contents) {
635
- const text = textFromHTML(node.attrs.contents);
636
- if (text !== null && text.length) {
637
- xref.textContent = text;
638
- }
646
+ const citationTextContent = this.citationTexts.get(node.attrs.id);
647
+ if (!citationTextContent) {
648
+ throw new Error(`No citation text found for ${node.attrs.id}`);
639
649
  }
650
+ xref.textContent = textFromHTML(citationTextContent);
640
651
  return xref;
641
652
  },
642
653
  cross_reference: (node) => {
@@ -1547,6 +1558,27 @@ export class JATSExporter {
1547
1558
  return name;
1548
1559
  };
1549
1560
  }
1561
+ generateCitations(fragment) {
1562
+ const nodes = [];
1563
+ fragment.descendants((node) => {
1564
+ if (isCitationNode(node)) {
1565
+ nodes.push(node);
1566
+ }
1567
+ });
1568
+ return buildCitations(nodes);
1569
+ }
1570
+ generateCitationTexts(fragment, csl) {
1571
+ this.citationTexts = new Map();
1572
+ this.citationProvider = new CitationProvider({
1573
+ getLibraryItem: (id) => this.modelMap.get(id),
1574
+ locale: csl.locale,
1575
+ citationStyle: csl.style,
1576
+ });
1577
+ const citations = this.generateCitations(fragment);
1578
+ this.citationProvider.rebuildState(citations).forEach(([id, , output]) => {
1579
+ this.citationTexts.set(id, output);
1580
+ });
1581
+ }
1550
1582
  buildKeywords(articleMeta) {
1551
1583
  const keywords = [...this.modelMap.values()].filter((model) => model.objectType === ObjectTypes.Keyword);
1552
1584
  const keywordGroups = new Map();
@@ -14,8 +14,9 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { Model } from '@manuscripts/json-schema';
17
+ import { CitationProvider } from '@manuscripts/library';
17
18
  import { DOMSerializer } from 'prosemirror-model';
18
- import { ManuscriptFragment, ManuscriptNode } from '../schema';
19
+ import { CitationNode, ManuscriptFragment, ManuscriptNode } from '../schema';
19
20
  import { Target } from '../transformer/labels';
20
21
  import { IDGenerator, MediaPathGenerator } from '../types';
21
22
  import { Version } from './jats-versions';
@@ -27,7 +28,12 @@ interface Links {
27
28
  export declare const createCounter: () => {
28
29
  increment: (field: string) => number;
29
30
  };
31
+ export type CSLOptions = {
32
+ style: string;
33
+ locale: string;
34
+ };
30
35
  export interface JATSExporterOptions {
36
+ csl: CSLOptions;
31
37
  version?: Version;
32
38
  doi?: string;
33
39
  id?: string;
@@ -37,13 +43,34 @@ export interface JATSExporterOptions {
37
43
  idGenerator?: IDGenerator;
38
44
  mediaPathGenerator?: MediaPathGenerator;
39
45
  }
46
+ export declare const buildCitations: (citations: CitationNode[]) => {
47
+ citationID: string;
48
+ citationItems: {
49
+ id: string;
50
+ }[];
51
+ properties: {
52
+ noteIndex: number;
53
+ };
54
+ }[];
40
55
  export declare class JATSExporter {
41
56
  protected document: Document;
42
57
  protected modelMap: Map<string, Model>;
43
58
  protected models: Model[];
44
59
  protected serializer: DOMSerializer;
45
60
  protected labelTargets?: Map<string, Target>;
46
- serializeToJATS: (fragment: ManuscriptFragment, modelMap: Map<string, Model>, manuscriptID: string, options?: JATSExporterOptions) => Promise<string>;
61
+ protected citationTexts: Map<string, string>;
62
+ protected citationProvider: CitationProvider;
63
+ protected generateCitations(fragment: ManuscriptFragment): {
64
+ citationID: string;
65
+ citationItems: {
66
+ id: string;
67
+ }[];
68
+ properties: {
69
+ noteIndex: number;
70
+ };
71
+ }[];
72
+ protected generateCitationTexts(fragment: ManuscriptFragment, csl: CSLOptions): void;
73
+ serializeToJATS: (fragment: ManuscriptFragment, modelMap: Map<string, Model>, manuscriptID: string, options: JATSExporterOptions) => Promise<string>;
47
74
  private nodeFromJATS;
48
75
  protected rewriteCrossReferenceTypes: () => void;
49
76
  protected rewriteMediaPaths: (mediaPathGenerator: MediaPathGenerator) => Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@manuscripts/transform",
3
3
  "description": "ProseMirror transformer for Manuscripts applications",
4
- "version": "2.0.1",
4
+ "version": "2.0.3",
5
5
  "repository": "github:Atypon-OpenSource/manuscripts-transform",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/cjs",
@@ -29,7 +29,8 @@
29
29
  "version": "yarn build"
30
30
  },
31
31
  "dependencies": {
32
- "@manuscripts/json-schema": "2.2.1",
32
+ "@manuscripts/json-schema": "^2.2.2",
33
+ "@manuscripts/library": "^1.3.1",
33
34
  "debug": "^4.3.4",
34
35
  "jszip": "^3.10.1",
35
36
  "mathjax-full": "^3.2.2",