@ai-matrx/office 0.4.0 → 0.5.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.
@@ -0,0 +1,147 @@
1
+ import { IDocumentData } from '@univerjs/core';
2
+
3
+ /**
4
+ * markdown-to-univer-doc — convert a markdown string into a Univer
5
+ * `IDocumentData` snapshot that the document editor (preset-docs-core) can
6
+ * hydrate directly.
7
+ *
8
+ * Why this exists: the document system stores opaque Univer `IDocumentData`
9
+ * (see `document-service.ts` + `components/DocumentEditor.tsx`). To send any
10
+ * markdown surface (an assistant message, a note, a scraper result) into a
11
+ * cloud document, we need markdown → IDocumentData. There was no converter, so
12
+ * "Add to docs" was a stub. This is the missing primitive — reuse it for every
13
+ * markdown → document export, never re-implement a one-off.
14
+ *
15
+ * The output is RENDERED content, not raw markdown text: `**bold**` becomes a
16
+ * bold text run, `# Heading` becomes a larger bold paragraph, `- item` becomes
17
+ * a bulleted line. The document never contains literal markdown syntax.
18
+ *
19
+ * Univer body model (the part we build):
20
+ * - `dataStream` — the text. `\r` terminates a paragraph; a trailing `\n`
21
+ * terminates the (single) section. Mirrors the empty doc
22
+ * `"\r\n"` shape in DocumentEditor.defaultEmptyDocument().
23
+ * - `paragraphs` — one entry per `\r`, `startIndex` = the `\r` offset.
24
+ * - `textRuns` — `{ st, ed, ts }` style overlays on character ranges.
25
+ * - `sectionBreaks` — the trailing `\n`.
26
+ *
27
+ * Scope: headings, paragraphs, bold/italic/strikethrough/inline-code, links
28
+ * (Univer hyperlink ranges keep the address), unordered/ordered lists, blockquotes, fenced code
29
+ * blocks, horizontal rules, and GFM tables (real Univer tables). Nested
30
+ * emphasis is supported. Anything exotic degrades to plain readable text rather
31
+ * than leaking syntax.
32
+ */
33
+
34
+ /**
35
+ * Convert markdown to a Univer `IDocumentData` snapshot. Pure — no I/O. Pass
36
+ * the result straight to `saveDocumentSnapshot`.
37
+ */
38
+ declare function markdownToUniverDoc(markdown: string, title?: string): Partial<IDocumentData>;
39
+ /**
40
+ * Derive a sensible document name from markdown: first heading, else first
41
+ * non-empty line, trimmed of markdown syntax. Falls back to a dated default.
42
+ */
43
+ declare function deriveDocumentName(markdown: string): string;
44
+
45
+ /**
46
+ * univerDocToMarkdown — the reverse of `markdown-to-univer-doc.ts`.
47
+ *
48
+ * Why it exists: the canvas pane for a cloud document (`udt_document`) offers a
49
+ * `Source` tab, and the rule in `features/canvas/core/canvasSource.ts` is that
50
+ * Source shows THE ITEM'S OWN SOURCE — for a document, its markdown — never the
51
+ * redux envelope. A document's content lives as an opaque Univer
52
+ * `IDocumentData` snapshot in `workbench.udt_document_snapshots`, so something
53
+ * has to read it back out. This is that something, and it is the ONLY one:
54
+ * never re-implement a second snapshot reader.
55
+ *
56
+ * What it reads (the half `markdownToUniverDoc` writes):
57
+ * - `body.dataStream` — the text; `\r` terminates a paragraph, a trailing
58
+ * `\n` terminates the section.
59
+ * - `body.paragraphs` — one entry per `\r` (`startIndex` = the `\r` offset).
60
+ * - `body.textRuns` — `{ st, ed, ts }` style overlays. `ts.bl` + a heading
61
+ * font size is how `markdownToUniverDoc` encodes
62
+ * `#`..`######`, so the level is recovered from `fs`.
63
+ *
64
+ * Fidelity is deliberately MODEST and honest: headings, bold, italic,
65
+ * strikethrough and inline/monospace code round-trip; everything else comes
66
+ * back as the plain text the user actually sees. A document typed by hand in
67
+ * Univer never had markdown syntax in the first place — printing its text is
68
+ * the truthful answer, not a reconstruction.
69
+ *
70
+ * PURE — no React, no Redux, no IO.
71
+ */
72
+ /** Inverse of markdown-to-univer-doc's HEADING_FONT_SIZE. */
73
+ declare const HEADING_LEVEL_BY_FONT_SIZE: Record<number, number>;
74
+ interface DrawingLike {
75
+ source?: unknown;
76
+ title?: unknown;
77
+ description?: unknown;
78
+ docTransform?: {
79
+ size?: {
80
+ width?: unknown;
81
+ height?: unknown;
82
+ };
83
+ };
84
+ }
85
+ /**
86
+ * A picture's markdown source. Our own pictures are `matrx-file:<id>`; the
87
+ * drawn size rides as a `#WxH` fragment so a round trip keeps the shape.
88
+ */
89
+ declare function imageMarkdownSource(drawing: DrawingLike): string | null;
90
+ /** Every picture in a snapshot, as its markdown source (`matrx-file:<id>#WxH` or a URL). */
91
+ declare function documentImageSources(snapshot: unknown): string[];
92
+ interface DocToMarkdownOptions {
93
+ /**
94
+ * Where a picture's markdown points. Return null to leave it out (the caller
95
+ * names what was left out). Default: the stored source.
96
+ */
97
+ imageUrl?: (source: string) => string | null;
98
+ }
99
+ /**
100
+ * Read a Univer document snapshot back as markdown. Returns `""` for an empty
101
+ * or unreadable snapshot so the caller can say "no source" honestly rather than
102
+ * printing an envelope.
103
+ */
104
+ declare function univerDocToMarkdown(snapshot: unknown, options?: DocToMarkdownOptions): string;
105
+
106
+ declare const DOCUMENT_UPLOAD_ACCEPT = ".docx,.md,.markdown,.txt,.html,.htm";
107
+ type Source = "imported_docx" | "imported_md" | "imported_txt";
108
+ declare function documentFileKind(name: string): "docx" | "md" | "txt" | "html" | null;
109
+ /** Word's document.xml -> markdown: headings, bold/italic runs, list paragraphs, tables as rows. */
110
+ declare function docxXmlToMarkdown(xml: string): string;
111
+ /** HTML -> markdown for the blocks a document keeps (headings, paragraphs, lists, emphasis, links). */
112
+ declare function htmlToDocumentMarkdown(html: string): string;
113
+ declare function documentFileToMarkdown(file: File): Promise<{
114
+ markdown: string;
115
+ source: Source;
116
+ }>;
117
+ /** Create a document from an uploaded file; returns its id. */
118
+ declare function importDocumentFile(file: File, organizationId: string): Promise<{
119
+ id: string;
120
+ name: string;
121
+ }>;
122
+
123
+ /**
124
+ * File → Download → EPUB / OpenDocument (.odt) / Rich Text (.rtf).
125
+ * EPUB the print package's EPUB engine, straight from the live text.
126
+ * ODT/RTF the same engine writes the .docx, then aidream's LibreOffice route
127
+ * (`POST /office/convert-document`) converts it. Nothing is stored.
128
+ * Engine, converter and saver are injected so the forcing test runs the real logic.
129
+ */
130
+ type OpenFormat = "epub" | "odt" | "rtf";
131
+ declare const OPEN_FORMATS: Record<OpenFormat, {
132
+ label: string;
133
+ extension: string;
134
+ mime: string;
135
+ }>;
136
+ interface OpenFormatDeps {
137
+ engine: (markdown: string, format: "docx" | "epub", fileName: string) => Promise<{
138
+ bytes: Uint8Array;
139
+ fileName: string;
140
+ }>;
141
+ convert: (docx: Uint8Array, fileName: string, target: "odt" | "rtf") => Promise<Uint8Array>;
142
+ save: (fileName: string, bytes: Uint8Array, mime: string) => void;
143
+ }
144
+ declare function exportOpenFormat(markdown: string, format: OpenFormat, title: string | null | undefined, deps: OpenFormatDeps): Promise<string>;
145
+ declare function downloadOpenFormat(markdown: string, format: OpenFormat, title: string | null | undefined): Promise<string>;
146
+
147
+ export { DOCUMENT_UPLOAD_ACCEPT, type DocToMarkdownOptions, HEADING_LEVEL_BY_FONT_SIZE, OPEN_FORMATS, type OpenFormat, type OpenFormatDeps, deriveDocumentName, documentFileKind, documentFileToMarkdown, documentImageSources, docxXmlToMarkdown, downloadOpenFormat, exportOpenFormat, htmlToDocumentMarkdown, imageMarkdownSource, importDocumentFile, markdownToUniverDoc, univerDocToMarkdown };
@@ -0,0 +1,147 @@
1
+ import { IDocumentData } from '@univerjs/core';
2
+
3
+ /**
4
+ * markdown-to-univer-doc — convert a markdown string into a Univer
5
+ * `IDocumentData` snapshot that the document editor (preset-docs-core) can
6
+ * hydrate directly.
7
+ *
8
+ * Why this exists: the document system stores opaque Univer `IDocumentData`
9
+ * (see `document-service.ts` + `components/DocumentEditor.tsx`). To send any
10
+ * markdown surface (an assistant message, a note, a scraper result) into a
11
+ * cloud document, we need markdown → IDocumentData. There was no converter, so
12
+ * "Add to docs" was a stub. This is the missing primitive — reuse it for every
13
+ * markdown → document export, never re-implement a one-off.
14
+ *
15
+ * The output is RENDERED content, not raw markdown text: `**bold**` becomes a
16
+ * bold text run, `# Heading` becomes a larger bold paragraph, `- item` becomes
17
+ * a bulleted line. The document never contains literal markdown syntax.
18
+ *
19
+ * Univer body model (the part we build):
20
+ * - `dataStream` — the text. `\r` terminates a paragraph; a trailing `\n`
21
+ * terminates the (single) section. Mirrors the empty doc
22
+ * `"\r\n"` shape in DocumentEditor.defaultEmptyDocument().
23
+ * - `paragraphs` — one entry per `\r`, `startIndex` = the `\r` offset.
24
+ * - `textRuns` — `{ st, ed, ts }` style overlays on character ranges.
25
+ * - `sectionBreaks` — the trailing `\n`.
26
+ *
27
+ * Scope: headings, paragraphs, bold/italic/strikethrough/inline-code, links
28
+ * (Univer hyperlink ranges keep the address), unordered/ordered lists, blockquotes, fenced code
29
+ * blocks, horizontal rules, and GFM tables (real Univer tables). Nested
30
+ * emphasis is supported. Anything exotic degrades to plain readable text rather
31
+ * than leaking syntax.
32
+ */
33
+
34
+ /**
35
+ * Convert markdown to a Univer `IDocumentData` snapshot. Pure — no I/O. Pass
36
+ * the result straight to `saveDocumentSnapshot`.
37
+ */
38
+ declare function markdownToUniverDoc(markdown: string, title?: string): Partial<IDocumentData>;
39
+ /**
40
+ * Derive a sensible document name from markdown: first heading, else first
41
+ * non-empty line, trimmed of markdown syntax. Falls back to a dated default.
42
+ */
43
+ declare function deriveDocumentName(markdown: string): string;
44
+
45
+ /**
46
+ * univerDocToMarkdown — the reverse of `markdown-to-univer-doc.ts`.
47
+ *
48
+ * Why it exists: the canvas pane for a cloud document (`udt_document`) offers a
49
+ * `Source` tab, and the rule in `features/canvas/core/canvasSource.ts` is that
50
+ * Source shows THE ITEM'S OWN SOURCE — for a document, its markdown — never the
51
+ * redux envelope. A document's content lives as an opaque Univer
52
+ * `IDocumentData` snapshot in `workbench.udt_document_snapshots`, so something
53
+ * has to read it back out. This is that something, and it is the ONLY one:
54
+ * never re-implement a second snapshot reader.
55
+ *
56
+ * What it reads (the half `markdownToUniverDoc` writes):
57
+ * - `body.dataStream` — the text; `\r` terminates a paragraph, a trailing
58
+ * `\n` terminates the section.
59
+ * - `body.paragraphs` — one entry per `\r` (`startIndex` = the `\r` offset).
60
+ * - `body.textRuns` — `{ st, ed, ts }` style overlays. `ts.bl` + a heading
61
+ * font size is how `markdownToUniverDoc` encodes
62
+ * `#`..`######`, so the level is recovered from `fs`.
63
+ *
64
+ * Fidelity is deliberately MODEST and honest: headings, bold, italic,
65
+ * strikethrough and inline/monospace code round-trip; everything else comes
66
+ * back as the plain text the user actually sees. A document typed by hand in
67
+ * Univer never had markdown syntax in the first place — printing its text is
68
+ * the truthful answer, not a reconstruction.
69
+ *
70
+ * PURE — no React, no Redux, no IO.
71
+ */
72
+ /** Inverse of markdown-to-univer-doc's HEADING_FONT_SIZE. */
73
+ declare const HEADING_LEVEL_BY_FONT_SIZE: Record<number, number>;
74
+ interface DrawingLike {
75
+ source?: unknown;
76
+ title?: unknown;
77
+ description?: unknown;
78
+ docTransform?: {
79
+ size?: {
80
+ width?: unknown;
81
+ height?: unknown;
82
+ };
83
+ };
84
+ }
85
+ /**
86
+ * A picture's markdown source. Our own pictures are `matrx-file:<id>`; the
87
+ * drawn size rides as a `#WxH` fragment so a round trip keeps the shape.
88
+ */
89
+ declare function imageMarkdownSource(drawing: DrawingLike): string | null;
90
+ /** Every picture in a snapshot, as its markdown source (`matrx-file:<id>#WxH` or a URL). */
91
+ declare function documentImageSources(snapshot: unknown): string[];
92
+ interface DocToMarkdownOptions {
93
+ /**
94
+ * Where a picture's markdown points. Return null to leave it out (the caller
95
+ * names what was left out). Default: the stored source.
96
+ */
97
+ imageUrl?: (source: string) => string | null;
98
+ }
99
+ /**
100
+ * Read a Univer document snapshot back as markdown. Returns `""` for an empty
101
+ * or unreadable snapshot so the caller can say "no source" honestly rather than
102
+ * printing an envelope.
103
+ */
104
+ declare function univerDocToMarkdown(snapshot: unknown, options?: DocToMarkdownOptions): string;
105
+
106
+ declare const DOCUMENT_UPLOAD_ACCEPT = ".docx,.md,.markdown,.txt,.html,.htm";
107
+ type Source = "imported_docx" | "imported_md" | "imported_txt";
108
+ declare function documentFileKind(name: string): "docx" | "md" | "txt" | "html" | null;
109
+ /** Word's document.xml -> markdown: headings, bold/italic runs, list paragraphs, tables as rows. */
110
+ declare function docxXmlToMarkdown(xml: string): string;
111
+ /** HTML -> markdown for the blocks a document keeps (headings, paragraphs, lists, emphasis, links). */
112
+ declare function htmlToDocumentMarkdown(html: string): string;
113
+ declare function documentFileToMarkdown(file: File): Promise<{
114
+ markdown: string;
115
+ source: Source;
116
+ }>;
117
+ /** Create a document from an uploaded file; returns its id. */
118
+ declare function importDocumentFile(file: File, organizationId: string): Promise<{
119
+ id: string;
120
+ name: string;
121
+ }>;
122
+
123
+ /**
124
+ * File → Download → EPUB / OpenDocument (.odt) / Rich Text (.rtf).
125
+ * EPUB the print package's EPUB engine, straight from the live text.
126
+ * ODT/RTF the same engine writes the .docx, then aidream's LibreOffice route
127
+ * (`POST /office/convert-document`) converts it. Nothing is stored.
128
+ * Engine, converter and saver are injected so the forcing test runs the real logic.
129
+ */
130
+ type OpenFormat = "epub" | "odt" | "rtf";
131
+ declare const OPEN_FORMATS: Record<OpenFormat, {
132
+ label: string;
133
+ extension: string;
134
+ mime: string;
135
+ }>;
136
+ interface OpenFormatDeps {
137
+ engine: (markdown: string, format: "docx" | "epub", fileName: string) => Promise<{
138
+ bytes: Uint8Array;
139
+ fileName: string;
140
+ }>;
141
+ convert: (docx: Uint8Array, fileName: string, target: "odt" | "rtf") => Promise<Uint8Array>;
142
+ save: (fileName: string, bytes: Uint8Array, mime: string) => void;
143
+ }
144
+ declare function exportOpenFormat(markdown: string, format: OpenFormat, title: string | null | undefined, deps: OpenFormatDeps): Promise<string>;
145
+ declare function downloadOpenFormat(markdown: string, format: OpenFormat, title: string | null | undefined): Promise<string>;
146
+
147
+ export { DOCUMENT_UPLOAD_ACCEPT, type DocToMarkdownOptions, HEADING_LEVEL_BY_FONT_SIZE, OPEN_FORMATS, type OpenFormat, type OpenFormatDeps, deriveDocumentName, documentFileKind, documentFileToMarkdown, documentImageSources, docxXmlToMarkdown, downloadOpenFormat, exportOpenFormat, htmlToDocumentMarkdown, imageMarkdownSource, importDocumentFile, markdownToUniverDoc, univerDocToMarkdown };