@oh-my-pi/pi-utils 18.7.0 → 18.8.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/CHANGELOG.md +23 -0
- package/dist/types/ar/index.d.ts +1 -0
- package/dist/types/ar/zip-package.d.ts +21 -0
- package/dist/types/dirs.d.ts +2 -2
- package/dist/types/docx/converter.d.ts +2 -0
- package/dist/types/dom/core.d.ts +15 -4
- package/dist/types/dom/parser.d.ts +5 -3
- package/dist/types/dom/selector.d.ts +7 -1
- package/dist/types/logger/rotating-file.d.ts +4 -2
- package/dist/types/marked/core.d.ts +7 -6
- package/dist/types/marked-list.d.ts +38 -0
- package/dist/types/math-delimiters.d.ts +10 -0
- package/dist/types/readability/types.d.ts +2 -0
- package/dist/types/turndown/types.d.ts +7 -0
- package/dist/types/vterm/buffer.d.ts +16 -5
- package/package.json +2 -2
- package/src/ar/index.ts +1 -0
- package/src/ar/zip-package.ts +57 -0
- package/src/chalk.ts +66 -23
- package/src/dirs.ts +12 -10
- package/src/docx/converter.ts +26 -12
- package/src/dom/core.ts +354 -111
- package/src/dom/parser.ts +42 -19
- package/src/dom/selector.ts +328 -95
- package/src/fetch-retry.ts +2 -0
- package/src/json-parse.ts +21 -6
- package/src/logger/rotating-file.ts +5 -3
- package/src/logger.ts +39 -23
- package/src/marked/core.ts +42 -18
- package/src/marked-list.ts +73 -0
- package/src/math-delimiters.ts +16 -0
- package/src/prompt.ts +3 -1
- package/src/ptree.ts +28 -35
- package/src/readability/readability.ts +349 -39
- package/src/readability/types.ts +2 -0
- package/src/stream.ts +34 -13
- package/src/temp.ts +1 -10
- package/src/template.ts +7 -4
- package/src/turndown/gfm.ts +16 -5
- package/src/turndown/html.ts +28 -8
- package/src/turndown/service.ts +43 -15
- package/src/turndown/types.ts +7 -0
- package/src/vterm/buffer.ts +31 -11
- package/src/vterm/terminal.ts +185 -92
package/src/docx/converter.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fs from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { ZipPackage } from "../ar";
|
|
4
4
|
import { attribute, childElements, descendants, firstChild, localName, parseXml, type XmlElement } from "./xml";
|
|
5
5
|
|
|
6
6
|
/** A mammoth-compatible diagnostic emitted while converting a document. */
|
|
@@ -26,6 +26,8 @@ export interface DocxImage {
|
|
|
26
26
|
readonly altText: string;
|
|
27
27
|
/** Read the image payload using mammoth's used encoding surface. */
|
|
28
28
|
read(encoding: "base64"): Promise<string>;
|
|
29
|
+
/** Read the raw image bytes without a base64 round trip. */
|
|
30
|
+
readBytes(): Promise<Uint8Array>;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/** HTML attributes returned by a custom image converter. */
|
|
@@ -119,7 +121,8 @@ interface NumberingLevel {
|
|
|
119
121
|
}
|
|
120
122
|
|
|
121
123
|
interface ConversionContext {
|
|
122
|
-
|
|
124
|
+
/** Lazily inflated DOCX members, capped on total inflated bytes. */
|
|
125
|
+
readonly package: ZipPackage;
|
|
123
126
|
readonly relationships: ReadonlyMap<string, Relationship>;
|
|
124
127
|
readonly contentTypes: ReadonlyMap<string, string>;
|
|
125
128
|
readonly styles: ReadonlyMap<string, Style>;
|
|
@@ -373,8 +376,8 @@ async function renderImage(element: XmlElement, context: ConversionContext): Pro
|
|
|
373
376
|
const relationship = relationshipId ? context.relationships.get(relationshipId) : undefined;
|
|
374
377
|
if (!relationship || relationship.external) return "";
|
|
375
378
|
const memberPath = relationshipPath(relationship.target);
|
|
376
|
-
const
|
|
377
|
-
if (!
|
|
379
|
+
const docx = context.package;
|
|
380
|
+
if (!docx.members.has(memberPath)) {
|
|
378
381
|
context.messages.push({ type: "warning", message: `Could not find image ${memberPath}` });
|
|
379
382
|
return "";
|
|
380
383
|
}
|
|
@@ -391,8 +394,12 @@ async function renderImage(element: XmlElement, context: ConversionContext): Pro
|
|
|
391
394
|
altText,
|
|
392
395
|
async read(encoding: "base64"): Promise<string> {
|
|
393
396
|
if (encoding !== "base64") throw new Error(`Unsupported image encoding: ${encoding}`);
|
|
397
|
+
const bytes = await readImageMember(docx, memberPath);
|
|
394
398
|
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("base64");
|
|
395
399
|
},
|
|
400
|
+
readBytes(): Promise<Uint8Array> {
|
|
401
|
+
return readImageMember(docx, memberPath);
|
|
402
|
+
},
|
|
396
403
|
};
|
|
397
404
|
const converted = await context.convertImage.convert(image);
|
|
398
405
|
const attributes = Object.entries(converted)
|
|
@@ -403,6 +410,13 @@ async function renderImage(element: XmlElement, context: ConversionContext): Pro
|
|
|
403
410
|
return attributes ? `<img ${attributes} />` : "<img />";
|
|
404
411
|
}
|
|
405
412
|
|
|
413
|
+
/** Bytes of an image member already known to exist. */
|
|
414
|
+
async function readImageMember(docx: ZipPackage, memberPath: string): Promise<Uint8Array> {
|
|
415
|
+
const bytes = await docx.readBytes(memberPath);
|
|
416
|
+
if (!bytes) throw new Error(`Could not find image ${memberPath}`);
|
|
417
|
+
return bytes;
|
|
418
|
+
}
|
|
419
|
+
|
|
406
420
|
function renderFootnoteReference(element: XmlElement, context: ConversionContext): string {
|
|
407
421
|
const id = attribute(element, "w:id");
|
|
408
422
|
if (!id || !context.footnotes.has(id)) return "";
|
|
@@ -655,21 +669,21 @@ function defaultImageConverter(): ImageConverter {
|
|
|
655
669
|
/** Convert a DOCX buffer or path to mammoth-compatible HTML. */
|
|
656
670
|
export async function convertToHtml(input: DocxInput, options: ConvertToHtmlOptions = {}): Promise<DocxResult> {
|
|
657
671
|
const bytes = "buffer" in input && input.buffer ? input.buffer : await fs.readFile(input.path);
|
|
658
|
-
const
|
|
659
|
-
const documentXml =
|
|
672
|
+
const docx = await ZipPackage.open(bytes);
|
|
673
|
+
const documentXml = await docx.readText("word/document.xml");
|
|
660
674
|
if (!documentXml) throw new Error("Invalid DOCX: missing word/document.xml");
|
|
661
675
|
const context: ConversionContext = {
|
|
662
|
-
|
|
663
|
-
relationships: parseRelationships(
|
|
664
|
-
contentTypes: parseContentTypes(
|
|
665
|
-
styles: parseStyles(
|
|
666
|
-
numbering: parseNumbering(
|
|
676
|
+
package: docx,
|
|
677
|
+
relationships: parseRelationships(await docx.readText("word/_rels/document.xml.rels")),
|
|
678
|
+
contentTypes: parseContentTypes(await docx.readText("[Content_Types].xml")),
|
|
679
|
+
styles: parseStyles(await docx.readText("word/styles.xml")),
|
|
680
|
+
numbering: parseNumbering(await docx.readText("word/numbering.xml")),
|
|
667
681
|
messages: [],
|
|
668
682
|
warnedStyles: new Set(),
|
|
669
683
|
customStyles: parseCustomStyles(options.styleMap),
|
|
670
684
|
includeDefaultStyleMap: options.includeDefaultStyleMap !== false,
|
|
671
685
|
convertImage: options.convertImage ?? defaultImageConverter(),
|
|
672
|
-
footnotes: parseFootnotes(
|
|
686
|
+
footnotes: parseFootnotes(await docx.readText("word/footnotes.xml")),
|
|
673
687
|
usedFootnotes: [],
|
|
674
688
|
footnoteOrdinals: new Map(),
|
|
675
689
|
};
|