@docscode/adapter-docx 1.0.0 → 1.0.1

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.cjs ADDED
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ DocxAdapter: () => DocxAdapter
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var Y = __toESM(require("yjs"), 1);
37
+ var import_docx = require("docx");
38
+ var import_core = require("@docscode/core");
39
+ var DocxAdapter = class {
40
+ format = "docx";
41
+ docling = new import_core.DoclingClient();
42
+ async read(source) {
43
+ const yDoc = new Y.Doc();
44
+ const canonical = new import_core.CanonicalDoc(yDoc);
45
+ const input = Buffer.isBuffer(source) ? `base64:${source.toString("base64")}` : source;
46
+ const result = await this.docling.convert(input);
47
+ canonical.metadata.set("title", result.metadata.title);
48
+ for (const block of result.content) {
49
+ if (block.type === "p") {
50
+ canonical.addParagraph(block.text);
51
+ }
52
+ }
53
+ return yDoc;
54
+ }
55
+ async write(doc) {
56
+ return this._serialize(doc);
57
+ }
58
+ async applyTrackedChanges(doc, suggestions) {
59
+ return this._serialize(doc, suggestions);
60
+ }
61
+ async _serialize(doc, suggestions = []) {
62
+ const canonical = new import_core.CanonicalDoc(doc);
63
+ const docx = new import_docx.Document({
64
+ sections: [{
65
+ children: canonical.content.toArray().map((block) => {
66
+ if (block.get("type") === "p") {
67
+ const yText = block.get("text");
68
+ const children = [new import_docx.TextRun(yText.toString())];
69
+ for (const s of suggestions) {
70
+ if (s.type === "insert") {
71
+ children.push(new import_docx.InsertedTextRun({
72
+ id: Math.floor(Math.random() * 1e4),
73
+ text: s.text || "",
74
+ author: s.author,
75
+ date: new Date(s.timestamp).toISOString()
76
+ }));
77
+ }
78
+ }
79
+ return new import_docx.Paragraph({ children });
80
+ }
81
+ return new import_docx.Paragraph("");
82
+ })
83
+ }]
84
+ });
85
+ return await import_docx.Packer.toBuffer(docx);
86
+ }
87
+ };
88
+ // Annotate the CommonJS export names for ESM import in node:
89
+ 0 && (module.exports = {
90
+ DocxAdapter
91
+ });
@@ -0,0 +1,13 @@
1
+ import * as Y from 'yjs';
2
+ import { FormatAdapter, Suggestion } from '@docscode/core';
3
+
4
+ declare class DocxAdapter implements FormatAdapter {
5
+ readonly format = "docx";
6
+ private docling;
7
+ read(source: Buffer | string): Promise<Y.Doc>;
8
+ write(doc: Y.Doc): Promise<Buffer>;
9
+ applyTrackedChanges(doc: Y.Doc, suggestions: Suggestion[]): Promise<Buffer>;
10
+ private _serialize;
11
+ }
12
+
13
+ export { DocxAdapter };
@@ -0,0 +1,13 @@
1
+ import * as Y from 'yjs';
2
+ import { FormatAdapter, Suggestion } from '@docscode/core';
3
+
4
+ declare class DocxAdapter implements FormatAdapter {
5
+ readonly format = "docx";
6
+ private docling;
7
+ read(source: Buffer | string): Promise<Y.Doc>;
8
+ write(doc: Y.Doc): Promise<Buffer>;
9
+ applyTrackedChanges(doc: Y.Doc, suggestions: Suggestion[]): Promise<Buffer>;
10
+ private _serialize;
11
+ }
12
+
13
+ export { DocxAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,56 @@
1
+ // src/index.ts
2
+ import * as Y from "yjs";
3
+ import { Document, Packer, Paragraph, TextRun, InsertedTextRun } from "docx";
4
+ import { CanonicalDoc, DoclingClient } from "@docscode/core";
5
+ var DocxAdapter = class {
6
+ format = "docx";
7
+ docling = new DoclingClient();
8
+ async read(source) {
9
+ const yDoc = new Y.Doc();
10
+ const canonical = new CanonicalDoc(yDoc);
11
+ const input = Buffer.isBuffer(source) ? `base64:${source.toString("base64")}` : source;
12
+ const result = await this.docling.convert(input);
13
+ canonical.metadata.set("title", result.metadata.title);
14
+ for (const block of result.content) {
15
+ if (block.type === "p") {
16
+ canonical.addParagraph(block.text);
17
+ }
18
+ }
19
+ return yDoc;
20
+ }
21
+ async write(doc) {
22
+ return this._serialize(doc);
23
+ }
24
+ async applyTrackedChanges(doc, suggestions) {
25
+ return this._serialize(doc, suggestions);
26
+ }
27
+ async _serialize(doc, suggestions = []) {
28
+ const canonical = new CanonicalDoc(doc);
29
+ const docx = new Document({
30
+ sections: [{
31
+ children: canonical.content.toArray().map((block) => {
32
+ if (block.get("type") === "p") {
33
+ const yText = block.get("text");
34
+ const children = [new TextRun(yText.toString())];
35
+ for (const s of suggestions) {
36
+ if (s.type === "insert") {
37
+ children.push(new InsertedTextRun({
38
+ id: Math.floor(Math.random() * 1e4),
39
+ text: s.text || "",
40
+ author: s.author,
41
+ date: new Date(s.timestamp).toISOString()
42
+ }));
43
+ }
44
+ }
45
+ return new Paragraph({ children });
46
+ }
47
+ return new Paragraph("");
48
+ })
49
+ }]
50
+ });
51
+ return await Packer.toBuffer(docx);
52
+ }
53
+ };
54
+ export {
55
+ DocxAdapter
56
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docscode/adapter-docx",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "DOCX Format Adapter for Kairo",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -13,5 +13,8 @@
13
13
  "@docscode/core": "*",
14
14
  "docx": "^9.1.1",
15
15
  "yjs": "^13.6.30"
16
- }
17
- }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ]
20
+ }
package/src/index.ts DELETED
@@ -1,69 +0,0 @@
1
- import * as Y from 'yjs';
2
- import { Document, Packer, Paragraph, TextRun, InsertedTextRun, DeletedTextRun } from 'docx';
3
- import { FormatAdapter, CanonicalDoc, DoclingClient, Suggestion } from '@docscode/core';
4
-
5
- export class DocxAdapter implements FormatAdapter {
6
- readonly format = 'docx';
7
- private docling = new DoclingClient();
8
-
9
- async read(source: Buffer | string): Promise<Y.Doc> {
10
- const yDoc = new Y.Doc();
11
- const canonical = new CanonicalDoc(yDoc);
12
-
13
- const input = Buffer.isBuffer(source)
14
- ? `base64:${source.toString('base64')}`
15
- : source;
16
-
17
- const result = await this.docling.convert(input);
18
-
19
- canonical.metadata.set('title', result.metadata.title);
20
-
21
- for (const block of result.content) {
22
- if (block.type === 'p') {
23
- canonical.addParagraph(block.text);
24
- }
25
- }
26
-
27
- return yDoc;
28
- }
29
-
30
- async write(doc: Y.Doc): Promise<Buffer> {
31
- return this._serialize(doc);
32
- }
33
-
34
- async applyTrackedChanges(doc: Y.Doc, suggestions: Suggestion[]): Promise<Buffer> {
35
- return this._serialize(doc, suggestions);
36
- }
37
-
38
- private async _serialize(doc: Y.Doc, suggestions: Suggestion[] = []): Promise<Buffer> {
39
- const canonical = new CanonicalDoc(doc);
40
- const docx = new Document({
41
- sections: [{
42
- children: canonical.content.toArray().map((block: any) => {
43
- if (block.get('type') === 'p') {
44
- const yText = block.get('text') as Y.Text;
45
- const children: any[] = [new TextRun(yText.toString())];
46
-
47
- // Map suggestions for this paragraph
48
- // (Simple implementation: append suggestions as tracked changes)
49
- for (const s of suggestions) {
50
- if (s.type === 'insert') {
51
- children.push(new InsertedTextRun({
52
- id: Math.floor(Math.random() * 10000),
53
- text: s.text || '',
54
- author: s.author,
55
- date: new Date(s.timestamp).toISOString()
56
- }));
57
- }
58
- }
59
-
60
- return new Paragraph({ children });
61
- }
62
- return new Paragraph('');
63
- })
64
- }]
65
- });
66
-
67
- return await Packer.toBuffer(docx) as Buffer;
68
- }
69
- }