jats-xml 1.0.6 → 1.0.7

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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as version } from './version.js';
2
2
  export { Jats } from './jats.js';
3
- export { convertToUnist } from './utils.js';
3
+ export { convertToUnist, convertToXml, escapeForXML } from './utils.js';
4
+ export * from './serialize.js';
4
5
  export * from './session.js';
5
6
  export * from './types.js';
6
7
  export * from './validate/index.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default as version } from './version.js';
2
2
  export { Jats } from './jats.js';
3
- export { convertToUnist } from './utils.js';
3
+ export { convertToUnist, convertToXml, escapeForXML } from './utils.js';
4
+ export * from './serialize.js';
4
5
  export * from './session.js';
5
6
  export * from './types.js';
6
7
  export * from './validate/index.js';
package/dist/jats.cjs CHANGED
@@ -5741,7 +5741,7 @@ var require_js2xml = __commonJS({
5741
5741
  // ../../node_modules/xml-js/lib/json2xml.js
5742
5742
  var require_json2xml = __commonJS({
5743
5743
  "../../node_modules/xml-js/lib/json2xml.js"(exports, module2) {
5744
- var js2xml = require_js2xml();
5744
+ var js2xml2 = require_js2xml();
5745
5745
  module2.exports = function(json2, options) {
5746
5746
  if (json2 instanceof Buffer) {
5747
5747
  json2 = json2.toString();
@@ -5756,7 +5756,7 @@ var require_json2xml = __commonJS({
5756
5756
  } else {
5757
5757
  js = json2;
5758
5758
  }
5759
- return js2xml(js, options);
5759
+ return js2xml2(js, options);
5760
5760
  };
5761
5761
  }
5762
5762
  });
@@ -5766,12 +5766,12 @@ var require_lib2 = __commonJS({
5766
5766
  "../../node_modules/xml-js/lib/index.js"(exports, module2) {
5767
5767
  var xml2js2 = require_xml2js();
5768
5768
  var xml2json = require_xml2json();
5769
- var js2xml = require_js2xml();
5769
+ var js2xml2 = require_js2xml();
5770
5770
  var json2xml = require_json2xml();
5771
5771
  module2.exports = {
5772
5772
  xml2js: xml2js2,
5773
5773
  xml2json,
5774
- js2xml,
5774
+ js2xml: js2xml2,
5775
5775
  json2xml
5776
5776
  };
5777
5777
  }
@@ -13022,7 +13022,7 @@ var {
13022
13022
  } = import_index.default;
13023
13023
 
13024
13024
  // src/version.ts
13025
- var version = "1.0.6";
13025
+ var version = "1.0.7";
13026
13026
  var version_default = version;
13027
13027
 
13028
13028
  // src/cli/parse.ts
@@ -15266,7 +15266,7 @@ function getSession(logger) {
15266
15266
  }
15267
15267
 
15268
15268
  // src/jats.ts
15269
- var import_xml_js = __toESM(require_lib2(), 1);
15269
+ var import_xml_js2 = __toESM(require_lib2(), 1);
15270
15270
 
15271
15271
  // src/utils.ts
15272
15272
  function convertToUnist(node) {
@@ -15275,7 +15275,7 @@ function convertToUnist(node) {
15275
15275
  const { name: name2, attributes, elements } = node;
15276
15276
  const children = elements == null ? void 0 : elements.map(convertToUnist).filter((n) => !!n);
15277
15277
  const { type: type2, ...attrs } = attributes != null ? attributes : {};
15278
- if (type2)
15278
+ if (type2 !== void 0)
15279
15279
  attrs._type = type2;
15280
15280
  const next = { type: name2 != null ? name2 : "unknown", ...attrs };
15281
15281
  if (name2 === "code") {
@@ -15289,7 +15289,7 @@ function convertToUnist(node) {
15289
15289
  return {
15290
15290
  type: "text",
15291
15291
  ...attributes,
15292
- value: String(text).replace(/\n(\s+)$/, "").replace("\n", " ")
15292
+ value: String(text).replace(/\n(\s+)$/, "")
15293
15293
  };
15294
15294
  }
15295
15295
  case "cdata": {
@@ -15312,6 +15312,35 @@ function convertToUnist(node) {
15312
15312
  throw new Error(`found ${node.type} ${node.name}`);
15313
15313
  }
15314
15314
  }
15315
+ function convertToXml(node) {
15316
+ const { type: type2, ...rest } = node;
15317
+ switch (type2) {
15318
+ case "text": {
15319
+ const { value, ...attributes } = rest;
15320
+ return { type: "text", attributes, text: value };
15321
+ }
15322
+ case "code": {
15323
+ const { value, ...attributes } = rest;
15324
+ return { type: "element", name: type2, attributes, elements: [{ type: "text", text: value }] };
15325
+ }
15326
+ case "comment": {
15327
+ return { type: "comment", comment: rest.value };
15328
+ }
15329
+ case "cdata": {
15330
+ const { cdata, ...attributes } = rest;
15331
+ return { type: "cdata", attributes, cdata };
15332
+ }
15333
+ default: {
15334
+ const { children, _type, ...attributes } = rest;
15335
+ if (_type !== void 0)
15336
+ attributes.type = _type;
15337
+ return { type: "element", name: type2, attributes, elements: children == null ? void 0 : children.map(convertToXml) };
15338
+ }
15339
+ }
15340
+ }
15341
+ function escapeForXML(text) {
15342
+ return text.replace(/&(?!amp;)/g, "&amp;").replace(/</g, "&lt;");
15343
+ }
15315
15344
  var MonthLookup = {
15316
15345
  jan: 0,
15317
15346
  january: 0,
@@ -15395,11 +15424,156 @@ function authorAndAffiliation(node, article) {
15395
15424
  return author;
15396
15425
  }
15397
15426
 
15427
+ // src/order.ts
15428
+ var articleMetaOrder = [
15429
+ "article-id",
15430
+ "article-version",
15431
+ "article-version-alternatives",
15432
+ "article-categories",
15433
+ "title-group",
15434
+ "contrib-group",
15435
+ "aff",
15436
+ "aff-alternatives",
15437
+ "x",
15438
+ "author-notes",
15439
+ "pub-date",
15440
+ "pub-date-not-available",
15441
+ "volume",
15442
+ "volume-id",
15443
+ "volume-series",
15444
+ "issue",
15445
+ "issue-id",
15446
+ "issue-title",
15447
+ "issue-title-group",
15448
+ "issue-sponsor",
15449
+ "issue-part",
15450
+ "volume-issue-group",
15451
+ "isbn",
15452
+ "supplement",
15453
+ "fpage",
15454
+ "lpage",
15455
+ "page-range",
15456
+ "elocation-id",
15457
+ "email",
15458
+ "ext-link",
15459
+ "uri",
15460
+ "product",
15461
+ "supplementary-material",
15462
+ "history",
15463
+ "pub-history",
15464
+ "permissions",
15465
+ "self-uri",
15466
+ "related-article",
15467
+ "related-object",
15468
+ "abstract",
15469
+ "trans-abstract",
15470
+ "kwd-group",
15471
+ "funding-group",
15472
+ "support-group",
15473
+ "conference",
15474
+ "counts",
15475
+ "custom-meta-group"
15476
+ ];
15477
+
15478
+ // src/serialize.ts
15479
+ var import_xml_js = __toESM(require_lib2(), 1);
15480
+ var both = [
15481
+ "\\?xml",
15482
+ "article",
15483
+ "sub-article",
15484
+ "front",
15485
+ "front-stub",
15486
+ "journal-meta",
15487
+ "journal-title-group",
15488
+ "publisher",
15489
+ "article-meta",
15490
+ "article-categories",
15491
+ "title-group",
15492
+ "contrib-group",
15493
+ "contrib",
15494
+ "institution-wrap",
15495
+ "aff",
15496
+ "permissions",
15497
+ "license",
15498
+ "kwd-group",
15499
+ "history",
15500
+ "self-uri",
15501
+ "funding-group",
15502
+ "award-group",
15503
+ "principal-award-recipient",
15504
+ "custom-meta-group",
15505
+ "date",
15506
+ "pub-date",
15507
+ "abstract",
15508
+ "counts",
15509
+ "body",
15510
+ "sec",
15511
+ "fig",
15512
+ "disp-formula",
15513
+ "table-wrap",
15514
+ "caption",
15515
+ "table",
15516
+ "thead",
15517
+ "ref-list",
15518
+ "ref",
15519
+ "back"
15520
+ ];
15521
+ var first = [
15522
+ "journal-id",
15523
+ "journal-title",
15524
+ "issn",
15525
+ "publisher-name",
15526
+ "publisher-loc",
15527
+ "article-id",
15528
+ "article-title",
15529
+ "alt-title",
15530
+ "subtitle",
15531
+ "kwd",
15532
+ "name",
15533
+ "email",
15534
+ "contrib-id",
15535
+ "role",
15536
+ "institution",
15537
+ "institution-id",
15538
+ "award-id",
15539
+ "meta-name",
15540
+ "meta-value",
15541
+ "title",
15542
+ "p",
15543
+ "tr",
15544
+ "label",
15545
+ "graphic",
15546
+ "mixed-citation"
15547
+ ];
15548
+ function indentXML(xml) {
15549
+ return xml.replace(RegExp(`<(\\/)?(${both.join("|")})( [^>]*)?>`, "g"), "<$1$2$3>\n").replace(RegExp(`([^
15550
+ ])<(\\/)?(${both.join("|")})( [^>]*)?>`, "g"), "$1\n<$2$3$4>").replace(RegExp(`([^
15551
+ ])<(${first.join("|")})( [^>]*)?>`, "g"), "$1\n<$2$3>").replace(RegExp(`<\\/(${first.join("|")})(\\s*)>([^
15552
+ ])`, "g"), "</$1>\n$3");
15553
+ }
15554
+ function serializeJatsXml(element, opts) {
15555
+ const { format: format2 } = { format: "pretty", ...opts };
15556
+ const xml = (0, import_xml_js.js2xml)(element, {
15557
+ compact: false,
15558
+ // No way to write XML with new lines, but no indentation with js2xml.
15559
+ // If you use 0 or '', you get a single line.
15560
+ spaces: format2 === "flat" || format2 === "pretty" ? 0 : format2 || 1,
15561
+ attributeValueFn: escapeForXML
15562
+ });
15563
+ if (format2 === 0) {
15564
+ return xml.replace(/\n(\s*)</g, "\n<");
15565
+ } else if (format2 === "pretty") {
15566
+ return indentXML(xml);
15567
+ }
15568
+ return xml;
15569
+ }
15570
+
15398
15571
  // src/jats.ts
15399
15572
  function select2(selector, node) {
15400
15573
  var _a;
15401
15574
  return (_a = select(selector, node)) != null ? _a : void 0;
15402
15575
  }
15576
+ var DEFAULT_DOCTYPE = 'article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD with MathML3 v1.3 20210610//EN" "http://jats.nlm.nih.gov/publishing/1.3/JATS-archivearticle1-3-mathml3.dtd"';
15403
15577
  var Jats = class {
15404
15578
  constructor(data, opts) {
15405
15579
  var _a;
@@ -15408,7 +15582,7 @@ var Jats = class {
15408
15582
  if (opts == null ? void 0 : opts.source)
15409
15583
  this.source = opts.source;
15410
15584
  try {
15411
- this.raw = (0, import_xml_js.xml2js)(data, { compact: false });
15585
+ this.raw = (0, import_xml_js2.xml2js)(data, { compact: false });
15412
15586
  } catch (error) {
15413
15587
  throw new Error("Problem parsing the JATS document, please ensure it is XML");
15414
15588
  }
@@ -15426,6 +15600,7 @@ var Jats = class {
15426
15600
  var _a, _b, _c, _d;
15427
15601
  const title = this.articleTitle;
15428
15602
  const subtitle = this.articleSubtitle;
15603
+ const short_title = this.articleAltTitle;
15429
15604
  const date = this.publicationDate;
15430
15605
  const authors = this.articleAuthors;
15431
15606
  const firstSubject = select2(Tags.subject, (_a = this.articleCategories) != null ? _a : this.front);
@@ -15433,6 +15608,7 @@ var Jats = class {
15433
15608
  return {
15434
15609
  title: title ? toText(title) : void 0,
15435
15610
  subtitle: subtitle ? toText(subtitle) : void 0,
15611
+ short_title: short_title ? toText(short_title) : void 0,
15436
15612
  doi: (_b = this.doi) != null ? _b : void 0,
15437
15613
  date: date ? (_c = toDate(date)) == null ? void 0 : _c.toISOString() : void 0,
15438
15614
  authors: authors == null ? void 0 : authors.map((a) => authorAndAffiliation(a, this.tree)),
@@ -15444,7 +15620,10 @@ var Jats = class {
15444
15620
  get front() {
15445
15621
  return select2(Tags.front, this.tree);
15446
15622
  }
15447
- get premissions() {
15623
+ get articleMeta() {
15624
+ return select2(Tags.articleMeta, this.tree);
15625
+ }
15626
+ get permissions() {
15448
15627
  return select2(Tags.permissions, this.front);
15449
15628
  }
15450
15629
  get doi() {
@@ -15465,7 +15644,7 @@ var Jats = class {
15465
15644
  return this.publicationDates.find((d) => !!select2(Tags.day, d));
15466
15645
  }
15467
15646
  get license() {
15468
- return select2(Tags.license, this.premissions);
15647
+ return select2(Tags.license, this.permissions);
15469
15648
  }
15470
15649
  get keywordGroup() {
15471
15650
  return select2(Tags.kwdGroup, this.front);
@@ -15489,6 +15668,9 @@ var Jats = class {
15489
15668
  get articleSubtitle() {
15490
15669
  return select2(Tags.subtitle, this.titleGroup);
15491
15670
  }
15671
+ get articleAltTitle() {
15672
+ return select2(Tags.altTitle, this.titleGroup);
15673
+ }
15492
15674
  get abstract() {
15493
15675
  return select2(Tags.abstract, this.front);
15494
15676
  }
@@ -15519,6 +15701,32 @@ var Jats = class {
15519
15701
  get references() {
15520
15702
  return selectAll(Tags.ref, this.refList);
15521
15703
  }
15704
+ sort() {
15705
+ var _a;
15706
+ if (this.articleMeta) {
15707
+ this.articleMeta.children = (_a = this.articleMeta) == null ? void 0 : _a.children.sort(
15708
+ (a, b) => articleMetaOrder.findIndex((x2) => x2 === a.type) - articleMetaOrder.findIndex((x2) => x2 === b.type)
15709
+ );
15710
+ }
15711
+ }
15712
+ serialize(opts) {
15713
+ var _a;
15714
+ this.sort();
15715
+ const body = convertToXml(this.tree);
15716
+ const element = (opts == null ? void 0 : opts.bodyOnly) ? body : {
15717
+ type: "element",
15718
+ elements: [
15719
+ {
15720
+ type: "doctype",
15721
+ doctype: this.doctype || DEFAULT_DOCTYPE
15722
+ },
15723
+ body
15724
+ ],
15725
+ declaration: { attributes: (_a = this.declaration) != null ? _a : { version: "1.0", encoding: "UTF-8" } }
15726
+ };
15727
+ const xml = serializeJatsXml(element, opts);
15728
+ return xml;
15729
+ }
15522
15730
  };
15523
15731
  function hasSingleArticle(element) {
15524
15732
  var _a;
@@ -19631,14 +19839,14 @@ function isPlainSafeLast(c) {
19631
19839
  return !isWhitespace(c) && c !== CHAR_COLON;
19632
19840
  }
19633
19841
  function codePointAt(string, pos) {
19634
- var first = string.charCodeAt(pos), second;
19635
- if (first >= 55296 && first <= 56319 && pos + 1 < string.length) {
19842
+ var first2 = string.charCodeAt(pos), second;
19843
+ if (first2 >= 55296 && first2 <= 56319 && pos + 1 < string.length) {
19636
19844
  second = string.charCodeAt(pos + 1);
19637
19845
  if (second >= 56320 && second <= 57343) {
19638
- return (first - 55296) * 1024 + second - 56320 + 65536;
19846
+ return (first2 - 55296) * 1024 + second - 56320 + 65536;
19639
19847
  }
19640
19848
  }
19641
- return first;
19849
+ return first2;
19642
19850
  }
19643
19851
  function needIndentIndicator(string) {
19644
19852
  var leadingSpaceRe = /^\n* /;
package/dist/jats.d.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  import type { GenericParent } from 'myst-common';
2
2
  import type { Element, DeclarationAttributes } from 'xml-js';
3
3
  import type { PageFrontmatter } from 'myst-frontmatter';
4
- import type { Front, Body, Back, SubArticle, RefList, Reference, TitleGroup, ArticleTitle, Subtitle, Permissions, PubDate, License, Abstract, ContribGroup, Contrib, KeywordGroup, Keyword, ArticleCategories } from 'jats-tags';
4
+ import type { Front, Body, Back, SubArticle, RefList, Reference, TitleGroup, ArticleTitle, Subtitle, Permissions, PubDate, License, Abstract, ContribGroup, Contrib, KeywordGroup, Keyword, ArticleCategories, ArticleMeta } from 'jats-tags';
5
5
  import type { Logger } from 'myst-cli-utils';
6
+ import { type SerializationOptions } from './serialize.js';
6
7
  type Options = {
7
8
  log?: Logger;
8
9
  source?: string;
9
10
  };
11
+ type WriteOptions = SerializationOptions & {
12
+ bodyOnly?: boolean;
13
+ };
10
14
  export declare class Jats {
11
15
  declaration?: DeclarationAttributes;
12
16
  doctype?: string;
@@ -17,7 +21,8 @@ export declare class Jats {
17
21
  constructor(data: string, opts?: Options);
18
22
  get frontmatter(): PageFrontmatter;
19
23
  get front(): Front | undefined;
20
- get premissions(): Permissions | undefined;
24
+ get articleMeta(): ArticleMeta | undefined;
25
+ get permissions(): Permissions | undefined;
21
26
  get doi(): string | undefined;
22
27
  get pmc(): string | undefined;
23
28
  get pmid(): string | undefined;
@@ -32,6 +37,7 @@ export declare class Jats {
32
37
  get titleGroup(): TitleGroup | undefined;
33
38
  get articleTitle(): ArticleTitle | undefined;
34
39
  get articleSubtitle(): Subtitle | undefined;
40
+ get articleAltTitle(): Subtitle | undefined;
35
41
  get abstract(): Abstract | undefined;
36
42
  get abstracts(): Abstract[];
37
43
  get contribGroup(): ContribGroup | undefined;
@@ -42,6 +48,8 @@ export declare class Jats {
42
48
  get subArticles(): SubArticle[];
43
49
  get refList(): RefList | undefined;
44
50
  get references(): Reference[];
51
+ sort(): void;
52
+ serialize(opts?: WriteOptions): string;
45
53
  }
46
54
  export {};
47
55
  //# sourceMappingURL=jats.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"jats.d.ts","sourceRoot":"","sources":["../src/jats.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,aAAa,EAAE,MAAM,aAAa,CAAC;AAI9D,OAAO,KAAK,EAAE,OAAO,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAIxD,OAAO,KAAK,EACV,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,UAAU,EACV,OAAO,EACP,SAAS,EACT,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,OAAO,EACP,OAAO,EACP,QAAQ,EACR,YAAY,EACZ,OAAO,EACP,YAAY,EACZ,OAAO,EACP,iBAAiB,EAClB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAG7C,KAAK,OAAO,GAAG;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAMjD,qBAAa,IAAI;IACf,WAAW,CAAC,EAAE,qBAAqB,CAAC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC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,IAAI,OAAO,GAAG,SAAS,CAEjC;IAED,IAAI,UAAU,IAAI,SAAS,EAAE,CAE5B;IAED,IAAI;IAUJ,SAAS,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,MAAM;CAoBvC"}
package/dist/jats.js CHANGED
@@ -3,12 +3,15 @@ import { xml2js } from 'xml-js';
3
3
  import { doi } from 'doi-utils';
4
4
  import { select as unistSelect, selectAll } from 'unist-util-select';
5
5
  import { Tags } from 'jats-tags';
6
- import { authorAndAffiliation, convertToUnist, findArticleId, toDate } from './utils.js';
6
+ import { authorAndAffiliation, convertToUnist, convertToXml, findArticleId, toDate, } from './utils.js';
7
7
  import { tic } from 'myst-cli-utils';
8
+ import { articleMetaOrder } from './order.js';
9
+ import { serializeJatsXml } from './serialize.js';
8
10
  function select(selector, node) {
9
11
  var _a;
10
12
  return ((_a = unistSelect(selector, node)) !== null && _a !== void 0 ? _a : undefined);
11
13
  }
14
+ const DEFAULT_DOCTYPE = 'article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD with MathML3 v1.3 20210610//EN" "http://jats.nlm.nih.gov/publishing/1.3/JATS-archivearticle1-3-mathml3.dtd"';
12
15
  export class Jats {
13
16
  constructor(data, opts) {
14
17
  var _a;
@@ -36,6 +39,7 @@ export class Jats {
36
39
  var _a, _b, _c, _d;
37
40
  const title = this.articleTitle;
38
41
  const subtitle = this.articleSubtitle;
42
+ const short_title = this.articleAltTitle;
39
43
  const date = this.publicationDate;
40
44
  const authors = this.articleAuthors;
41
45
  const firstSubject = select(Tags.subject, (_a = this.articleCategories) !== null && _a !== void 0 ? _a : this.front);
@@ -43,6 +47,7 @@ export class Jats {
43
47
  return {
44
48
  title: title ? toText(title) : undefined,
45
49
  subtitle: subtitle ? toText(subtitle) : undefined,
50
+ short_title: short_title ? toText(short_title) : undefined,
46
51
  doi: (_b = this.doi) !== null && _b !== void 0 ? _b : undefined,
47
52
  date: date ? (_c = toDate(date)) === null || _c === void 0 ? void 0 : _c.toISOString() : undefined,
48
53
  authors: authors === null || authors === void 0 ? void 0 : authors.map((a) => authorAndAffiliation(a, this.tree)),
@@ -54,7 +59,10 @@ export class Jats {
54
59
  get front() {
55
60
  return select(Tags.front, this.tree);
56
61
  }
57
- get premissions() {
62
+ get articleMeta() {
63
+ return select(Tags.articleMeta, this.tree);
64
+ }
65
+ get permissions() {
58
66
  return select(Tags.permissions, this.front);
59
67
  }
60
68
  get doi() {
@@ -75,7 +83,7 @@ export class Jats {
75
83
  return this.publicationDates.find((d) => !!select(Tags.day, d));
76
84
  }
77
85
  get license() {
78
- return select(Tags.license, this.premissions);
86
+ return select(Tags.license, this.permissions);
79
87
  }
80
88
  get keywordGroup() {
81
89
  return select(Tags.kwdGroup, this.front);
@@ -99,6 +107,9 @@ export class Jats {
99
107
  get articleSubtitle() {
100
108
  return select(Tags.subtitle, this.titleGroup);
101
109
  }
110
+ get articleAltTitle() {
111
+ return select(Tags.altTitle, this.titleGroup);
112
+ }
102
113
  get abstract() {
103
114
  return select(Tags.abstract, this.front);
104
115
  }
@@ -129,6 +140,33 @@ export class Jats {
129
140
  get references() {
130
141
  return selectAll(Tags.ref, this.refList);
131
142
  }
143
+ sort() {
144
+ var _a;
145
+ if (this.articleMeta) {
146
+ this.articleMeta.children = (_a = this.articleMeta) === null || _a === void 0 ? void 0 : _a.children.sort((a, b) => articleMetaOrder.findIndex((x) => x === a.type) -
147
+ articleMetaOrder.findIndex((x) => x === b.type));
148
+ }
149
+ }
150
+ serialize(opts) {
151
+ var _a;
152
+ this.sort();
153
+ const body = convertToXml(this.tree);
154
+ const element = (opts === null || opts === void 0 ? void 0 : opts.bodyOnly)
155
+ ? body
156
+ : {
157
+ type: 'element',
158
+ elements: [
159
+ {
160
+ type: 'doctype',
161
+ doctype: this.doctype || DEFAULT_DOCTYPE,
162
+ },
163
+ body,
164
+ ],
165
+ declaration: { attributes: (_a = this.declaration) !== null && _a !== void 0 ? _a : { version: '1.0', encoding: 'UTF-8' } },
166
+ };
167
+ const xml = serializeJatsXml(element, opts);
168
+ return xml;
169
+ }
132
170
  }
133
171
  function hasSingleArticle(element) {
134
172
  var _a;
@@ -0,0 +1,2 @@
1
+ export declare const articleMetaOrder: string[];
2
+ //# sourceMappingURL=order.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order.d.ts","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,UAgD5B,CAAC"}
package/dist/order.js ADDED
@@ -0,0 +1,49 @@
1
+ export const articleMetaOrder = [
2
+ 'article-id',
3
+ 'article-version',
4
+ 'article-version-alternatives',
5
+ 'article-categories',
6
+ 'title-group',
7
+ 'contrib-group',
8
+ 'aff',
9
+ 'aff-alternatives',
10
+ 'x',
11
+ 'author-notes',
12
+ 'pub-date',
13
+ 'pub-date-not-available',
14
+ 'volume',
15
+ 'volume-id',
16
+ 'volume-series',
17
+ 'issue',
18
+ 'issue-id',
19
+ 'issue-title',
20
+ 'issue-title-group',
21
+ 'issue-sponsor',
22
+ 'issue-part',
23
+ 'volume-issue-group',
24
+ 'isbn',
25
+ 'supplement',
26
+ 'fpage',
27
+ 'lpage',
28
+ 'page-range',
29
+ 'elocation-id',
30
+ 'email',
31
+ 'ext-link',
32
+ 'uri',
33
+ 'product',
34
+ 'supplementary-material',
35
+ 'history',
36
+ 'pub-history',
37
+ 'permissions',
38
+ 'self-uri',
39
+ 'related-article',
40
+ 'related-object',
41
+ 'abstract',
42
+ 'trans-abstract',
43
+ 'kwd-group',
44
+ 'funding-group',
45
+ 'support-group',
46
+ 'conference',
47
+ 'counts',
48
+ 'custom-meta-group',
49
+ ];
@@ -0,0 +1,12 @@
1
+ import { type Element } from 'xml-js';
2
+ export type SerializationOptions = {
3
+ /**
4
+ * When 'pretty', the default, the xml be formatted in a custom, opinionated way
5
+ * When 'flat', the xml will be on a single line
6
+ * When `0`, the XML will be on different lines with 0 spaces.
7
+ * When any other value (e.g. `2` or `\t`) the XML will be indented at the start of the line by that amount.
8
+ */
9
+ format?: number | 'flat' | 'pretty' | '\t';
10
+ };
11
+ export declare function serializeJatsXml(element: Element, opts?: SerializationOptions): string;
12
+ //# sourceMappingURL=serialize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../src/serialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,OAAO,EAAE,MAAM,QAAQ,CAAC;AAG9C,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CAAC;CAC5C,CAAC;AA+EF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,oBAAoB,UAgB7E"}
@@ -0,0 +1,95 @@
1
+ import { js2xml } from 'xml-js';
2
+ import { escapeForXML } from './utils.js';
3
+ const both = [
4
+ '\\?xml',
5
+ 'article',
6
+ 'sub-article',
7
+ 'front',
8
+ 'front-stub',
9
+ 'journal-meta',
10
+ 'journal-title-group',
11
+ 'publisher',
12
+ 'article-meta',
13
+ 'article-categories',
14
+ 'title-group',
15
+ 'contrib-group',
16
+ 'contrib',
17
+ 'institution-wrap',
18
+ 'aff',
19
+ 'permissions',
20
+ 'license',
21
+ 'kwd-group',
22
+ 'history',
23
+ 'self-uri',
24
+ 'funding-group',
25
+ 'award-group',
26
+ 'principal-award-recipient',
27
+ 'custom-meta-group',
28
+ 'date',
29
+ 'pub-date',
30
+ 'abstract',
31
+ 'counts',
32
+ 'body',
33
+ 'sec',
34
+ 'fig',
35
+ 'disp-formula',
36
+ 'table-wrap',
37
+ 'caption',
38
+ 'table',
39
+ 'thead',
40
+ 'ref-list',
41
+ 'ref',
42
+ 'back',
43
+ ];
44
+ const first = [
45
+ 'journal-id',
46
+ 'journal-title',
47
+ 'issn',
48
+ 'publisher-name',
49
+ 'publisher-loc',
50
+ 'article-id',
51
+ 'article-title',
52
+ 'alt-title',
53
+ 'subtitle',
54
+ 'kwd',
55
+ 'name',
56
+ 'email',
57
+ 'contrib-id',
58
+ 'role',
59
+ 'institution',
60
+ 'institution-id',
61
+ 'award-id',
62
+ 'meta-name',
63
+ 'meta-value',
64
+ 'title',
65
+ 'p',
66
+ 'tr',
67
+ 'label',
68
+ 'graphic',
69
+ 'mixed-citation',
70
+ ];
71
+ function indentXML(xml) {
72
+ return xml
73
+ .replace(RegExp(`<(\\/)?(${both.join('|')})( [^>]*)?>`, 'g'), '<$1$2$3>\n')
74
+ .replace(RegExp(`([^\n])<(\\/)?(${both.join('|')})( [^>]*)?>`, 'g'), '$1\n<$2$3$4>')
75
+ .replace(RegExp(`([^\n])<(${first.join('|')})( [^>]*)?>`, 'g'), '$1\n<$2$3>')
76
+ .replace(RegExp(`<\\/(${first.join('|')})(\\s*)>([^\n])`, 'g'), '</$1>\n$3');
77
+ }
78
+ export function serializeJatsXml(element, opts) {
79
+ const { format } = { format: 'pretty', ...opts };
80
+ const xml = js2xml(element, {
81
+ compact: false,
82
+ // No way to write XML with new lines, but no indentation with js2xml.
83
+ // If you use 0 or '', you get a single line.
84
+ spaces: format === 'flat' || format === 'pretty' ? 0 : format || 1,
85
+ attributeValueFn: escapeForXML,
86
+ });
87
+ if (format === 0) {
88
+ // either `0` or `''`
89
+ return xml.replace(/\n(\s*)</g, '\n<');
90
+ }
91
+ else if (format === 'pretty') {
92
+ return indentXML(xml);
93
+ }
94
+ return xml;
95
+ }
package/dist/utils.d.ts CHANGED
@@ -2,6 +2,8 @@ import type { GenericNode, GenericParent } from 'myst-common';
2
2
  import type { Element } from 'xml-js';
3
3
  import type { Contributor } from 'myst-frontmatter';
4
4
  export declare function convertToUnist(node: Element): GenericNode | GenericParent | undefined;
5
+ export declare function convertToXml(node: GenericNode): Element;
6
+ export declare function escapeForXML(text: string): string;
5
7
  export declare function toDate(date?: GenericParent): Date | undefined;
6
8
  export declare function formatDate(date?: Date): string | undefined;
7
9
  export type PubIdTypes = 'doi' | 'pmc' | 'pmid' | 'publisher-id' | string;
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAKtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,GAAG,aAAa,GAAG,SAAS,CA6CrF;AA6BD,wBAAgB,MAAM,CAAC,IAAI,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAa7D;AAED,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,sBAGrC;AAED,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,cAAc,GAAG,MAAM,CAAC;AAE1E,wBAAgB,aAAa,CAC3B,IAAI,EAAE,aAAa,GAAG,SAAS,EAC/B,SAAS,GAAE,UAAkB,GAC5B,MAAM,GAAG,SAAS,CAQpB;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,GAAG,WAAW,CAyC7F"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE9D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAKtC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,GAAG,aAAa,GAAG,SAAS,CA2CrF;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAwBvD;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,UAExC;AA6BD,wBAAgB,MAAM,CAAC,IAAI,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,CAa7D;AAED,wBAAgB,UAAU,CAAC,IAAI,CAAC,EAAE,IAAI,sBAGrC;AAED,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,cAAc,GAAG,MAAM,CAAC;AAE1E,wBAAgB,aAAa,CAC3B,IAAI,EAAE,aAAa,GAAG,SAAS,EAC/B,SAAS,GAAE,UAAkB,GAC5B,MAAM,GAAG,SAAS,CAQpB;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,GAAG,WAAW,CAyC7F"}
package/dist/utils.js CHANGED
@@ -8,7 +8,7 @@ export function convertToUnist(node) {
8
8
  const { name, attributes, elements } = node;
9
9
  const children = elements === null || elements === void 0 ? void 0 : elements.map(convertToUnist).filter((n) => !!n);
10
10
  const { type, ...attrs } = attributes !== null && attributes !== void 0 ? attributes : {};
11
- if (type)
11
+ if (type !== undefined)
12
12
  attrs._type = type;
13
13
  const next = { type: name !== null && name !== void 0 ? name : 'unknown', ...attrs };
14
14
  if (name === 'code') {
@@ -23,9 +23,7 @@ export function convertToUnist(node) {
23
23
  return {
24
24
  type: 'text',
25
25
  ...attributes,
26
- value: String(text)
27
- .replace(/\n(\s+)$/, '')
28
- .replace('\n', ' '),
26
+ value: String(text).replace(/\n(\s+)$/, ''),
29
27
  };
30
28
  }
31
29
  case 'cdata': {
@@ -51,6 +49,35 @@ export function convertToUnist(node) {
51
49
  throw new Error(`found ${node.type} ${node.name}`);
52
50
  }
53
51
  }
52
+ export function convertToXml(node) {
53
+ const { type, ...rest } = node;
54
+ switch (type) {
55
+ case 'text': {
56
+ const { value, ...attributes } = rest;
57
+ return { type: 'text', attributes, text: value };
58
+ }
59
+ case 'code': {
60
+ const { value, ...attributes } = rest;
61
+ return { type: 'element', name: type, attributes, elements: [{ type: 'text', text: value }] };
62
+ }
63
+ case 'comment': {
64
+ return { type: 'comment', comment: rest.value };
65
+ }
66
+ case 'cdata': {
67
+ const { cdata, ...attributes } = rest;
68
+ return { type: 'cdata', attributes, cdata };
69
+ }
70
+ default: {
71
+ const { children, _type, ...attributes } = rest;
72
+ if (_type !== undefined)
73
+ attributes.type = _type;
74
+ return { type: 'element', name: type, attributes, elements: children === null || children === void 0 ? void 0 : children.map(convertToXml) };
75
+ }
76
+ }
77
+ }
78
+ export function escapeForXML(text) {
79
+ return text.replace(/&(?!amp;)/g, '&amp;').replace(/</g, '&lt;');
80
+ }
54
81
  const MonthLookup = {
55
82
  jan: 0,
56
83
  january: 0,
package/dist/version.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const version = "1.0.6";
1
+ declare const version = "1.0.7";
2
2
  export default version;
3
3
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
- const version = '1.0.6';
1
+ const version = '1.0.7';
2
2
  export default version;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jats-xml",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Types and utilities for working with JATS in Typescript",
5
5
  "author": "Rowan Cockett <rowan@curvenote.com>",
6
6
  "homepage": "https://github.com/curvenote/jats",
@@ -48,8 +48,8 @@
48
48
  "adm-zip": "^0.5.10",
49
49
  "doi-utils": "^2.0.0",
50
50
  "fair-principles": "^2.0.0",
51
- "jats-fetch": "^1.0.6",
52
- "jats-tags": "^1.0.6",
51
+ "jats-fetch": "^1.0.7",
52
+ "jats-tags": "^1.0.7",
53
53
  "js-yaml": "^4.1.0",
54
54
  "node-fetch": "^3.3.1",
55
55
  "unist-util-is": "^5.2.1",