jats-utils 1.1.0 → 1.1.2

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/utils.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import type { GenericNode, GenericParent } from 'myst-common';
2
2
  import type { Element } from 'xml-js';
3
- export declare function convertToUnist(node: Element): GenericNode | GenericParent | undefined;
3
+ export type ConvertToUnistContext = {
4
+ insidePreformat?: boolean;
5
+ };
6
+ export declare function convertToUnist(node: Element, ctx?: ConvertToUnistContext): GenericNode | GenericParent | undefined;
4
7
  export declare function convertToXml(node: GenericNode): Element;
5
8
  export declare function escapeForXML(text: string): string;
6
9
  export declare function toDate(date?: GenericParent): Date | undefined;
@@ -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;AAGtC,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"}
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;AAGtC,MAAM,MAAM,qBAAqB,GAAG;IAAE,eAAe,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAElE,wBAAgB,cAAc,CAC5B,IAAI,EAAE,OAAO,EACb,GAAG,CAAC,EAAE,qBAAqB,GAC1B,WAAW,GAAG,aAAa,GAAG,SAAS,CAqDzC;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"}
package/dist/utils.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import { toText } from 'myst-common';
2
2
  import { select } from 'unist-util-select';
3
- export function convertToUnist(node) {
3
+ export function convertToUnist(node, ctx) {
4
+ var _a;
5
+ const insidePreformat = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.insidePreformat) !== null && _a !== void 0 ? _a : false;
4
6
  switch (node.type) {
5
7
  case 'element': {
6
8
  const { name, attributes, elements } = node;
7
- const children = elements === null || elements === void 0 ? void 0 : elements.map(convertToUnist).filter((n) => !!n);
9
+ const nextInside = insidePreformat || name === 'preformat';
10
+ const children = elements === null || elements === void 0 ? void 0 : elements.map((el) => convertToUnist(el, { insidePreformat: nextInside })).filter((n) => !!n);
8
11
  const { type, ...attrs } = attributes !== null && attributes !== void 0 ? attributes : {};
9
12
  if (type !== undefined)
10
13
  attrs._type = type;
@@ -18,18 +21,24 @@ export function convertToUnist(node) {
18
21
  }
19
22
  case 'text': {
20
23
  const { attributes, text } = node;
24
+ const raw = String(text);
25
+ if (!insidePreformat && !raw.trim()) {
26
+ return undefined;
27
+ }
28
+ const value = insidePreformat ? raw : raw.replace(/\n(\s+)$/, '');
21
29
  return {
22
30
  type: 'text',
23
31
  ...attributes,
24
- value: String(text).replace(/\n(\s+)$/, ''),
32
+ value,
25
33
  };
26
34
  }
27
35
  case 'cdata': {
28
36
  const { attributes, cdata } = node;
37
+ const str = String(cdata);
29
38
  return {
30
39
  type: 'cdata',
31
40
  ...attributes,
32
- cdata: String(cdata).trim(),
41
+ cdata: insidePreformat ? str : str.trim(),
33
42
  };
34
43
  }
35
44
  case 'comment': {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "jats-utils",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Utility functions for working with JATS in Typescript",
5
- "author": "Rowan Cockett <rowan@continuous.foundation>",
5
+ "author": "Rowan Cockett <rowan@continuousfoundation.org>",
6
6
  "homepage": "https://github.com/continuous-foundation/jats",
7
7
  "license": "MIT",
8
8
  "sideEffects": false,
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "unist-util-select": "^4.0.0",
39
- "jats-tags": "^1.1.0"
39
+ "jats-tags": "^1.1.2"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "xml-js": "^1"