@storyteller-platform/align 0.1.18 → 0.1.20

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.
@@ -1,63 +1,91 @@
1
1
  import "../chunk-BIEQXUOY.js";
2
2
  import { Epub } from "@storyteller-platform/epub";
3
3
  import { TextNode } from "./model.js";
4
- function serializeDom(doc) {
5
- return doc.children.map((child) => serializeDomNode(child));
6
- }
7
- function serializeDomNode(node) {
8
- if (node instanceof TextNode) {
9
- return Epub.createXmlTextNode(node.text);
4
+ class Serializer {
5
+ constructor(doc) {
6
+ this.doc = doc;
10
7
  }
11
- return Epub.createXmlElement(
12
- node.tagName,
13
- node.attrs,
14
- serializeDomNodes(node.children)
15
- );
16
- }
17
- function serializeDomNodes(nodes) {
18
- const partitioned = nodes.reduce((acc, child) => {
19
- const lastPartition = acc.at(-1);
20
- if (!lastPartition) {
21
- return [[child]];
22
- }
23
- const lastChild = lastPartition.at(-1);
24
- if (!lastChild) {
25
- return [...acc.slice(0, acc.length), [child]];
8
+ serializedIds = /* @__PURE__ */ new Set();
9
+ serialize() {
10
+ return this.doc.children.map((child) => this.serializeDomNode(child));
11
+ }
12
+ serializeDomNode(node) {
13
+ if (node instanceof TextNode) {
14
+ return Epub.createXmlTextNode(node.text);
26
15
  }
27
- const childFirstMark = child.marks[0];
28
- const lastChildFirstMark = lastChild.marks[0];
29
- if (childFirstMark === lastChildFirstMark || childFirstMark?.eq(lastChildFirstMark)) {
30
- return [
31
- ...acc.slice(0, acc.length - 1),
32
- [...lastPartition.slice(0, lastPartition.length), child]
33
- ];
16
+ let attrs = node.attrs;
17
+ const id = node.attrs["id"];
18
+ if (id) {
19
+ if (this.serializedIds.has(id)) {
20
+ const { id: _id, ...remaining } = node.attrs;
21
+ attrs = remaining;
22
+ } else {
23
+ this.serializedIds.add(id);
24
+ }
34
25
  }
35
- return [...acc, [child]];
36
- }, []);
37
- const xmlChildren = [];
38
- for (const partition of partitioned) {
39
- xmlChildren.push(...serializePartition(partition));
26
+ return Epub.createXmlElement(
27
+ node.tagName,
28
+ attrs,
29
+ this.serializeDomNodes(node.children)
30
+ );
40
31
  }
41
- return xmlChildren;
42
- }
43
- function serializePartition(nodes) {
44
- const firstChild = nodes[0];
45
- if (!firstChild) return [];
46
- const firstMark = firstChild.marks[0];
47
- if (!firstMark) {
48
- return nodes.map((child) => serializeDomNode(child));
32
+ serializeDomNodes(nodes) {
33
+ const partitioned = nodes.reduce((acc, child) => {
34
+ const lastPartition = acc.at(-1);
35
+ if (!lastPartition) {
36
+ return [[child]];
37
+ }
38
+ const lastChild = lastPartition.at(-1);
39
+ if (!lastChild) {
40
+ return [...acc.slice(0, acc.length), [child]];
41
+ }
42
+ const childFirstMark = child.marks[0];
43
+ const lastChildFirstMark = lastChild.marks[0];
44
+ if (childFirstMark === lastChildFirstMark || childFirstMark?.eq(lastChildFirstMark)) {
45
+ return [
46
+ ...acc.slice(0, acc.length - 1),
47
+ [...lastPartition.slice(0, lastPartition.length), child]
48
+ ];
49
+ }
50
+ return [...acc, [child]];
51
+ }, []);
52
+ const xmlChildren = [];
53
+ for (const partition of partitioned) {
54
+ xmlChildren.push(...this.serializePartition(partition));
55
+ }
56
+ return xmlChildren;
49
57
  }
50
- return [
51
- Epub.createXmlElement(
52
- firstMark.tagName,
53
- firstMark.attrs,
54
- serializeDomNodes(
55
- nodes.map((node) => node.copy({ marks: node.marks.slice(1) }))
58
+ serializePartition(nodes) {
59
+ const firstChild = nodes[0];
60
+ if (!firstChild) return [];
61
+ const firstMark = firstChild.marks[0];
62
+ if (!firstMark) {
63
+ return nodes.map((child) => this.serializeDomNode(child));
64
+ }
65
+ let attrs = firstMark.attrs;
66
+ const id = firstMark.attrs["id"];
67
+ if (id) {
68
+ if (this.serializedIds.has(id)) {
69
+ const { id: _id, ...remaining } = firstMark.attrs;
70
+ attrs = remaining;
71
+ } else {
72
+ this.serializedIds.add(id);
73
+ }
74
+ }
75
+ return [
76
+ Epub.createXmlElement(
77
+ firstMark.tagName,
78
+ attrs,
79
+ this.serializeDomNodes(
80
+ nodes.map((node) => node.copy({ marks: node.marks.slice(1) }))
81
+ )
56
82
  )
57
- )
58
- ];
83
+ ];
84
+ }
85
+ }
86
+ function serializeDom(doc) {
87
+ return new Serializer(doc).serialize();
59
88
  }
60
89
  export {
61
- serializeDom,
62
- serializeDomNode
90
+ serializeDom
63
91
  };
@@ -58,7 +58,12 @@ function liftText(root) {
58
58
  if (node.isBlock) {
59
59
  return !!node.textContent.match(/\S/);
60
60
  }
61
- if (!(node instanceof import_model.TextNode)) return true;
61
+ if (!(node instanceof import_model.TextNode)) {
62
+ if (node.isLeaf && parent.isBlock && index === parent.children.length - 1 && !text.endsWith("\n")) {
63
+ text += "\n";
64
+ }
65
+ return true;
66
+ }
62
67
  if (mapping.map(pos) - mapping.map(lastTextEnd)) {
63
68
  mapping.appendMap(
64
69
  new import_map.StepMap([
@@ -38,7 +38,12 @@ function liftText(root) {
38
38
  if (node.isBlock) {
39
39
  return !!node.textContent.match(/\S/);
40
40
  }
41
- if (!(node instanceof TextNode)) return true;
41
+ if (!(node instanceof TextNode)) {
42
+ if (node.isLeaf && parent.isBlock && index === parent.children.length - 1 && !text.endsWith("\n")) {
43
+ text += "\n";
44
+ }
45
+ return true;
46
+ }
42
47
  if (mapping.map(pos) - mapping.map(lastTextEnd)) {
43
48
  mapping.appendMap(
44
49
  new StepMap([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storyteller-platform/align",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "A library and CLI for automatically aligning audiobooks and EPUBs to produce Media Overlays",
5
5
  "author": "Shane Friedman",
6
6
  "license": "MIT",
@@ -61,8 +61,8 @@
61
61
  "@optique/run": "^0.10.7",
62
62
  "@storyteller-platform/audiobook": "^0.3.10",
63
63
  "@storyteller-platform/epub": "^0.4.8",
64
- "@storyteller-platform/ghost-story": "^0.1.6",
65
- "@storyteller-platform/transliteration": "^3.1.1",
64
+ "@storyteller-platform/ghost-story": "^0.1.7",
65
+ "@storyteller-platform/transliteration": "^3.1.2",
66
66
  "chalk": "^5.4.1",
67
67
  "cli-progress": "^3.12.0",
68
68
  "esbuild": "^0.27.3",