@fiduswriter/books-document 0.1.1 → 0.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.
Files changed (80) hide show
  1. package/README.md +3 -3
  2. package/dist/exporter/bits/index.d.ts.map +1 -1
  3. package/dist/exporter/bits/index.js +7 -3
  4. package/dist/exporter/bits/index.js.map +1 -1
  5. package/dist/exporter/bits/templates.d.ts.map +1 -1
  6. package/dist/exporter/bits/templates.js.map +1 -1
  7. package/dist/exporter/docx/index.d.ts.map +1 -1
  8. package/dist/exporter/docx/index.js +4 -2
  9. package/dist/exporter/docx/index.js.map +1 -1
  10. package/dist/exporter/docx/render.d.ts.map +1 -1
  11. package/dist/exporter/docx/render.js.map +1 -1
  12. package/dist/exporter/epub/index.d.ts.map +1 -1
  13. package/dist/exporter/epub/index.js +12 -5
  14. package/dist/exporter/epub/index.js.map +1 -1
  15. package/dist/exporter/epub/templates.d.ts.map +1 -1
  16. package/dist/exporter/epub/templates.js.map +1 -1
  17. package/dist/exporter/epub/tools.d.ts.map +1 -1
  18. package/dist/exporter/epub/tools.js.map +1 -1
  19. package/dist/exporter/html/index.d.ts.map +1 -1
  20. package/dist/exporter/html/index.js +5 -2
  21. package/dist/exporter/html/index.js.map +1 -1
  22. package/dist/exporter/html/templates.d.ts.map +1 -1
  23. package/dist/exporter/html/templates.js +1 -3
  24. package/dist/exporter/html/templates.js.map +1 -1
  25. package/dist/exporter/html/tools.d.ts.map +1 -1
  26. package/dist/exporter/html/tools.js +2 -1
  27. package/dist/exporter/html/tools.js.map +1 -1
  28. package/dist/exporter/latex/index.d.ts.map +1 -1
  29. package/dist/exporter/latex/index.js.map +1 -1
  30. package/dist/exporter/latex/templates.d.ts.map +1 -1
  31. package/dist/exporter/latex/templates.js.map +1 -1
  32. package/dist/exporter/native/index.d.ts.map +1 -1
  33. package/dist/exporter/native/index.js +1 -1
  34. package/dist/exporter/native/index.js.map +1 -1
  35. package/dist/exporter/odt/index.d.ts.map +1 -1
  36. package/dist/exporter/odt/index.js +4 -2
  37. package/dist/exporter/odt/index.js.map +1 -1
  38. package/dist/exporter/odt/render.d.ts.map +1 -1
  39. package/dist/exporter/odt/render.js.map +1 -1
  40. package/dist/exporter/print/index.d.ts.map +1 -1
  41. package/dist/exporter/print/index.js +1 -2
  42. package/dist/exporter/print/index.js.map +1 -1
  43. package/dist/exporter/print/templates.js.map +1 -1
  44. package/dist/exporter/tools.d.ts.map +1 -1
  45. package/dist/exporter/tools.js +4 -2
  46. package/dist/exporter/tools.js.map +1 -1
  47. package/dist/importer/native/index.d.ts.map +1 -1
  48. package/dist/importer/native/index.js +6 -5
  49. package/dist/importer/native/index.js.map +1 -1
  50. package/dist/importer/native/reader.d.ts.map +1 -1
  51. package/dist/importer/native/reader.js +3 -1
  52. package/dist/importer/native/reader.js.map +1 -1
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js.map +1 -1
  55. package/dist/types.d.ts.map +1 -1
  56. package/package.json +103 -93
  57. package/src/exporter/bits/index.ts +31 -27
  58. package/src/exporter/bits/templates.ts +4 -4
  59. package/src/exporter/docx/index.ts +32 -33
  60. package/src/exporter/docx/render.ts +17 -16
  61. package/src/exporter/epub/index.ts +43 -23
  62. package/src/exporter/epub/templates.ts +14 -12
  63. package/src/exporter/epub/tools.ts +5 -3
  64. package/src/exporter/html/index.ts +46 -32
  65. package/src/exporter/html/templates.ts +13 -15
  66. package/src/exporter/html/tools.ts +9 -4
  67. package/src/exporter/latex/index.ts +20 -17
  68. package/src/exporter/latex/templates.ts +6 -2
  69. package/src/exporter/native/index.ts +14 -14
  70. package/src/exporter/odt/index.ts +26 -27
  71. package/src/exporter/odt/render.ts +17 -16
  72. package/src/exporter/print/index.ts +11 -15
  73. package/src/exporter/print/templates.ts +1 -1
  74. package/src/exporter/tools.ts +44 -15
  75. package/src/importer/native/index.ts +30 -23
  76. package/src/importer/native/reader.ts +18 -9
  77. package/src/index.ts +1 -1
  78. package/src/modules.d.ts +1 -1
  79. package/src/types.ts +2 -2
  80. package/src/global.d.ts +0 -2
@@ -4,13 +4,13 @@
4
4
  * Uses @vivliostyle/print to render the book in the browser.
5
5
  */
6
6
 
7
- import type {Schema} from "prosemirror-model"
8
- import type {CSL, User} from "@fiduswriter/document"
9
- import {printHTML} from "@vivliostyle/print"
7
+ import type { Schema } from "prosemirror-model"
8
+ import type { CSL, User } from "@fiduswriter/document"
9
+ import { printHTML } from "@vivliostyle/print"
10
10
 
11
- import type {Book, BookStyles, DocumentListEntry} from "../../types.js"
12
- import {HTMLBookExporter} from "../html/index.js"
13
- import {chapterTemplate} from "./templates.js"
11
+ import type { Book, BookStyles, DocumentListEntry } from "../../types.js"
12
+ import { HTMLBookExporter } from "../html/index.js"
13
+ import { chapterTemplate } from "./templates.js"
14
14
 
15
15
  export class PrintBookExporter extends HTMLBookExporter {
16
16
  constructor(
@@ -37,13 +37,10 @@ export class PrintBookExporter extends HTMLBookExporter {
37
37
  let contents = bookStyle.contents
38
38
  bookStyle.bookstylefile_set.forEach(
39
39
  ([url, filename]: [string, string]) =>
40
- (contents = contents.replace(
41
- new RegExp(filename, "g"),
42
- url
43
- ))
40
+ (contents = contents.replace(new RegExp(filename, "g"), url))
44
41
  )
45
42
 
46
- this.styleSheets.push({contents})
43
+ this.styleSheets.push({ contents })
47
44
  return true
48
45
  }
49
46
 
@@ -54,7 +51,7 @@ export class PrintBookExporter extends HTMLBookExporter {
54
51
  if (!htmlDoc) {
55
52
  return
56
53
  }
57
- const config: any = {title: this.book.title}
54
+ const config: any = { title: this.book.title }
58
55
 
59
56
  if (navigator.userAgent.includes("Gecko/")) {
60
57
  config.printCallback = (iframeWin: Window) => {
@@ -70,8 +67,7 @@ export class PrintBookExporter extends HTMLBookExporter {
70
67
  document.body.appendChild(backgroundStyle)
71
68
  window.print()
72
69
  document.body = oldBody
73
- delete document.body.parentElement!.dataset
74
- .vivliostylePaginated
70
+ delete document.body.parentElement!.dataset.vivliostylePaginated
75
71
  }
76
72
  }
77
73
 
@@ -82,7 +78,7 @@ export class PrintBookExporter extends HTMLBookExporter {
82
78
  url?: string
83
79
  filename?: string
84
80
  contents?: string
85
- }): Promise<{url?: string; filename?: string; contents?: string}> {
81
+ }): Promise<{ url?: string; filename?: string; contents?: string }> {
86
82
  if (sheet.url) {
87
83
  sheet.filename = sheet.url
88
84
  delete sheet.url
@@ -1,4 +1,4 @@
1
- import {escapeText} from "fwtoolkit"
1
+ import { escapeText } from "fwtoolkit"
2
2
 
3
3
  export const chapterTemplate = ({
4
4
  part,
@@ -7,10 +7,10 @@
7
7
  * decrypt end-to-end encrypted chapters.
8
8
  */
9
9
 
10
- import type {Schema} from "prosemirror-model"
11
- import {getSettings} from "@fiduswriter/document/schema/convert"
12
- import {acceptAllNoInsertions} from "@fiduswriter/document/transform"
13
- import {addAlert} from "fwtoolkit"
10
+ import type { Schema } from "prosemirror-model"
11
+ import { getSettings } from "@fiduswriter/document/schema/convert"
12
+ import { acceptAllNoInsertions } from "@fiduswriter/document/transform"
13
+ import { addAlert, gettext } from "fwtoolkit"
14
14
 
15
15
  import type {
16
16
  ChapterLoader,
@@ -60,8 +60,11 @@ export async function getMissingChapterData(
60
60
  e2ee?: E2EEStrategy
61
61
  } = {}
62
62
  ): Promise<void> {
63
- const {rawContent = false, loader = noopChapterLoader, e2ee = noopE2EEStrategy} =
64
- options
63
+ const {
64
+ rawContent = false,
65
+ loader = noopChapterLoader,
66
+ e2ee = noopE2EEStrategy
67
+ } = options
65
68
 
66
69
  const bookDocuments = book.chapters.map(chapter =>
67
70
  documentList.find(doc => doc.id === chapter.text)
@@ -131,7 +134,9 @@ async function decryptE2EEChapters(
131
134
  "error",
132
135
  `${gettext("No encryption key found for chapter:")} ${chapterLabel}. ${gettext("The key may not have been shared with you.")}`
133
136
  )
134
- throw new Error(`No encryption key for document ${String(doc.id)}`)
137
+ throw new Error(
138
+ `No encryption key for document ${String(doc.id)}`
139
+ )
135
140
  }
136
141
 
137
142
  if (!doc.e2ee_salt) {
@@ -142,7 +147,11 @@ async function decryptE2EEChapters(
142
147
  const salt = base64ToUint8Array(doc.e2ee_salt)
143
148
  const iterations = doc.e2ee_iterations || 600000
144
149
 
145
- const key = await e2ee.resolvePasswordToKey(password, salt, iterations)
150
+ const key = await e2ee.resolvePasswordToKey(
151
+ password,
152
+ salt,
153
+ iterations
154
+ )
146
155
 
147
156
  try {
148
157
  const decryptedContent = (await e2ee.decryptObject(
@@ -151,11 +160,18 @@ async function decryptE2EEChapters(
151
160
  )) as Record<string, unknown>
152
161
 
153
162
  // Update the plaintext title.
154
- const titleNode = (decryptedContent.content as Record<string, unknown>[] | undefined)?.[0]
155
- const titleContent = (titleNode?.content as Record<string, unknown>[] | undefined) || []
163
+ const titleNode = (
164
+ decryptedContent.content as
165
+ Record<string, unknown>[] | undefined
166
+ )?.[0]
167
+ const titleContent =
168
+ (titleNode?.content as
169
+ Record<string, unknown>[] | undefined) || []
156
170
  let title = ""
157
171
  titleContent.forEach(child => {
158
- const marks = (child.marks as Record<string, unknown>[] | undefined) || []
172
+ const marks =
173
+ (child.marks as
174
+ Record<string, unknown>[] | undefined) || []
159
175
  if (!marks.some(m => (m.type as string) === "deletion")) {
160
176
  title += (child.text as string) || ""
161
177
  }
@@ -163,23 +179,36 @@ async function decryptE2EEChapters(
163
179
  if (title) {
164
180
  doc.title = title.substring(0, 255)
165
181
  if (typeof sessionStorage !== "undefined") {
166
- sessionStorage.setItem(`e2ee_title_${String(doc.id)}`, doc.title)
182
+ sessionStorage.setItem(
183
+ `e2ee_title_${String(doc.id)}`,
184
+ doc.title
185
+ )
167
186
  }
168
187
  }
169
188
 
170
189
  // Parse ProseMirror content.
171
190
  if (rawContent) {
172
191
  doc.rawContent = JSON.parse(
173
- JSON.stringify(schema.nodeFromJSON(decryptedContent as Record<string, unknown>).toJSON())
192
+ JSON.stringify(
193
+ schema
194
+ .nodeFromJSON(
195
+ decryptedContent as Record<string, unknown>
196
+ )
197
+ .toJSON()
198
+ )
174
199
  )
175
200
  }
176
201
  doc.content = acceptAllNoInsertions(
177
- schema.nodeFromJSON(decryptedContent as Record<string, unknown>)
202
+ schema.nodeFromJSON(
203
+ decryptedContent as Record<string, unknown>
204
+ )
178
205
  ).toJSON()
179
206
  doc.settings = getSettings(doc.content)
180
207
 
181
208
  // Decrypt encrypted images.
182
- const encryptedImageEntries = Object.entries(doc.images || {}).filter(
209
+ const encryptedImageEntries = Object.entries(
210
+ doc.images || {}
211
+ ).filter(
183
212
  ([, entry]) =>
184
213
  entry.file_type === "application/octet-stream" &&
185
214
  typeof entry.image === "string"
@@ -5,15 +5,14 @@
5
5
  * chapter and delegates creation of the book record to a `BookImporterBackend`.
6
6
  */
7
7
 
8
- import type {NativeImporterBackend} from "@fiduswriter/document"
9
- import {NativeImporter} from "@fiduswriter/document/importer/native"
10
- import {addAlert} from "fwtoolkit"
8
+ import type { NativeImporterBackend } from "@fiduswriter/document"
9
+ import { NativeImporter } from "@fiduswriter/document/importer/native"
10
+ import { addAlert, gettext } from "fwtoolkit"
11
11
 
12
- import type {BookImporterBackend, Chapter, User} from "../../types.js"
13
- import {readFidusBookFile} from "./reader.js"
12
+ import type { BookImporterBackend, Chapter, User } from "../../types.js"
14
13
 
15
- export {FIDUSBOOK_VERSION} from "./reader.js"
16
- export {readFidusBookFile, FidusBookReader} from "./reader.js"
14
+ export { FIDUSBOOK_VERSION } from "./reader.js"
15
+ export { readFidusBookFile, FidusBookReader } from "./reader.js"
17
16
 
18
17
  export class NativeBookImporter {
19
18
  file: Blob
@@ -69,8 +68,8 @@ export class NativeBookImporter {
69
68
  }
70
69
 
71
70
  async readZip(): Promise<void> {
72
- let textFiles: Array<{filename: string; content: string}> = []
73
- let binaryFiles: Array<{filename: string; content: Blob}> = []
71
+ let textFiles: Array<{ filename: string; content: string }> = []
72
+ let binaryFiles: Array<{ filename: string; content: Blob }> = []
74
73
 
75
74
  try {
76
75
  const JSZip = (await import("jszip")).default
@@ -103,7 +102,7 @@ export class NativeBookImporter {
103
102
  }
104
103
  })
105
104
  )
106
- } catch (error) {
105
+ } catch (_error) {
107
106
  this.statusText = gettext(
108
107
  "The uploaded file does not appear to be a Fidusbook file."
109
108
  )
@@ -114,18 +113,15 @@ export class NativeBookImporter {
114
113
  }
115
114
 
116
115
  async processFidusbookFile(
117
- textFiles: Array<{filename: string; content: string}>,
118
- binaryFiles: Array<{filename: string; content: Blob}>
116
+ textFiles: Array<{ filename: string; content: string }>,
117
+ binaryFiles: Array<{ filename: string; content: Blob }>
119
118
  ): Promise<void> {
120
119
  const versionEntry = textFiles.find(
121
120
  f => f.filename === "filetype-version"
122
121
  )
123
122
  const filetypeVersion = Number.parseFloat(versionEntry?.content || "")
124
123
 
125
- if (
126
- filetypeVersion < 1.0 ||
127
- filetypeVersion > 1.0
128
- ) {
124
+ if (filetypeVersion < 1.0 || filetypeVersion > 1.0) {
129
125
  this.statusText =
130
126
  gettext(
131
127
  "The Fidusbook file version is not supported by this server: "
@@ -148,9 +144,13 @@ export class NativeBookImporter {
148
144
  textFiles.find(f => f.filename === "book.json")?.content || "{}"
149
145
  ) as Record<string, any>
150
146
 
151
- const sortedChapters = [...(bookData.chapters as Array<{chapter_index: number; number: number; part?: string}>)].sort(
152
- (a, b) => a.chapter_index - b.chapter_index
153
- )
147
+ const sortedChapters = [
148
+ ...(bookData.chapters as Array<{
149
+ chapter_index: number
150
+ number: number
151
+ part?: string
152
+ }>)
153
+ ].sort((a, b) => a.chapter_index - b.chapter_index)
154
154
 
155
155
  const importedDocIds: Record<number, number> = {}
156
156
 
@@ -177,7 +177,10 @@ export class NativeBookImporter {
177
177
  }
178
178
 
179
179
  const docJson = JSON.parse(docFile.content) as Record<string, any>
180
- const imagesJson = JSON.parse(imagesFile.content) as Record<string, any>
180
+ const imagesJson = JSON.parse(imagesFile.content) as Record<
181
+ string,
182
+ any
183
+ >
181
184
  const bibJson = JSON.parse(bibFile.content) as Record<string, any>
182
185
 
183
186
  const chapterPrefix = `chapters/${ci}/images/`
@@ -194,7 +197,7 @@ export class NativeBookImporter {
194
197
  const importer = new NativeImporter(
195
198
  docJson,
196
199
  bibJson,
197
- {db: imagesJson},
200
+ { db: imagesJson },
198
201
  chapterOtherFiles,
199
202
  this.user,
200
203
  this.nativeBackend,
@@ -205,7 +208,7 @@ export class NativeBookImporter {
205
208
 
206
209
  let doc
207
210
  try {
208
- ;({doc} = await importer.init())
211
+ ;({ doc } = await importer.init())
209
212
  } catch (error) {
210
213
  addAlert(
211
214
  "error",
@@ -225,7 +228,11 @@ export class NativeBookImporter {
225
228
  part: chapter.part || ""
226
229
  }))
227
230
 
228
- const book = await this.bookBackend.createBook(bookData as any, chapters, false)
231
+ const book = await this.bookBackend.createBook(
232
+ bookData as any,
233
+ chapters,
234
+ false
235
+ )
229
236
  this.bookId = book.id || null
230
237
  this.ok = true
231
238
  this.statusText = `"${bookData.title}" ${gettext("successfully imported.")}`
@@ -6,7 +6,7 @@
6
6
  * exporters. No server interaction.
7
7
  */
8
8
 
9
- import type {DocumentListEntry} from "../../types.js"
9
+ import type { DocumentListEntry } from "../../types.js"
10
10
 
11
11
  export const FIDUSBOOK_VERSION = "1.0"
12
12
  export const MIN_FIDUSBOOK_VERSION = 1.0
@@ -58,7 +58,7 @@ export async function readFidusBookFile(
58
58
  const content = await zipfs.files[filename].async(
59
59
  isText ? "string" : "blob"
60
60
  )
61
- const entry = {filename, content}
61
+ const entry = { filename, content }
62
62
  if (isText) {
63
63
  textFiles.push(entry as ArchiveFile)
64
64
  } else {
@@ -93,9 +93,9 @@ export async function readFidusBookFile(
93
93
  textFiles.find(f => f.filename === "book.json")?.content as string
94
94
  ) as Record<string, unknown>
95
95
 
96
- const sortedChapters = [...(bookData.chapters as Array<{chapter_index: number}>)].sort(
97
- (a, b) => a.chapter_index - b.chapter_index
98
- )
96
+ const sortedChapters = [
97
+ ...(bookData.chapters as Array<{ chapter_index: number }>)
98
+ ].sort((a, b) => a.chapter_index - b.chapter_index)
99
99
 
100
100
  const documentList: DocumentListEntry[] = []
101
101
 
@@ -116,9 +116,18 @@ export async function readFidusBookFile(
116
116
  throw new Error(`Missing chapter data for index ${ci}`)
117
117
  }
118
118
 
119
- const docJson = JSON.parse(docFile.content as string) as Record<string, unknown>
120
- const imagesJson = JSON.parse(imagesFile.content as string) as Record<string, {image: string; file?: Blob}>
121
- const bibJson = JSON.parse(bibFile.content as string) as Record<string, unknown>
119
+ const docJson = JSON.parse(docFile.content as string) as Record<
120
+ string,
121
+ unknown
122
+ >
123
+ const imagesJson = JSON.parse(imagesFile.content as string) as Record<
124
+ string,
125
+ { image: string; file?: Blob }
126
+ >
127
+ const bibJson = JSON.parse(bibFile.content as string) as Record<
128
+ string,
129
+ unknown
130
+ >
122
131
 
123
132
  const chapterPrefix = `chapters/${ci}/images/`
124
133
  const chapterImages: Record<string, Blob> = {}
@@ -155,7 +164,7 @@ export async function readFidusBookFile(
155
164
  } as DocumentListEntry)
156
165
  }
157
166
 
158
- return {book: bookData, documentList}
167
+ return { book: bookData, documentList }
159
168
  }
160
169
 
161
170
  export class FidusBookReader {
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export {FIDUSBOOK_VERSION} from "./schema/index.js"
1
+ export { FIDUSBOOK_VERSION } from "./schema/index.js"
2
2
  export type {
3
3
  Book,
4
4
  BookBaseMetadata,
package/src/modules.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module "pretty" {
2
- function pretty(html: string, options?: {ocd?: boolean}): string
2
+ function pretty(html: string, options?: { ocd?: boolean }): string
3
3
  export default pretty
4
4
  }
5
5
 
package/src/types.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Core types for the @fiduswriter/books-document package.
3
3
  */
4
4
 
5
- import type {Schema} from "prosemirror-model"
5
+ import type { Schema } from "prosemirror-model"
6
6
  import type {
7
7
  BibDB,
8
8
  CSL,
@@ -12,7 +12,7 @@ import type {
12
12
  User
13
13
  } from "@fiduswriter/document"
14
14
 
15
- export type {CSL, User} from "@fiduswriter/document"
15
+ export type { CSL, User } from "@fiduswriter/document"
16
16
 
17
17
  /** A single chapter entry inside a book. */
18
18
  export interface Chapter {
package/src/global.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare function gettext(text: string): string
2
- declare function interpolate(fmt: string, ...args: unknown[]): string