@stll/docx-utils 0.1.0 → 0.1.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.d.ts +0 -1
- package/dist/index.js +14 -6
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -51,6 +51,12 @@ const extractBinary = async (zip, path) => {
|
|
|
51
51
|
const repackZip = async (zip) => await zip.generateAsync(DOCX_COMPRESSION);
|
|
52
52
|
//#endregion
|
|
53
53
|
//#region src/relationships.ts
|
|
54
|
+
/**
|
|
55
|
+
* Escape a string for safe use inside a double-quoted XML attribute value.
|
|
56
|
+
* Order matters: `&` must be escaped first so it does not double-escape the
|
|
57
|
+
* entities produced for the other characters.
|
|
58
|
+
*/
|
|
59
|
+
const escapeXmlAttribute = (value) => value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """);
|
|
54
60
|
/** Find the next available rId in a relationships XML string */
|
|
55
61
|
const findNextRId = (relsXml) => {
|
|
56
62
|
const matches = relsXml.matchAll(/Id="rId(?<num>\d+)"/gu);
|
|
@@ -63,15 +69,17 @@ const findNextRId = (relsXml) => {
|
|
|
63
69
|
};
|
|
64
70
|
/** Ensure a content type entry exists in [Content_Types].xml */
|
|
65
71
|
const ensureContentType = (contentTypesXml, partName, contentType) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
const escapedPartName = escapeXmlAttribute(partName);
|
|
73
|
+
if (contentTypesXml.includes(`PartName="${escapedPartName}"`)) return contentTypesXml;
|
|
74
|
+
const override = `<Override PartName="${escapedPartName}" ContentType="${escapeXmlAttribute(contentType)}"/>`;
|
|
75
|
+
return contentTypesXml.replace("</Types>", () => `${override}\n</Types>`);
|
|
69
76
|
};
|
|
70
77
|
/** Ensure a relationship entry exists */
|
|
71
78
|
const ensureRelationship = (relsXml, rId, type, target) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
79
|
+
const escapedRId = escapeXmlAttribute(rId);
|
|
80
|
+
if (relsXml.includes(`Id="${escapedRId}"`)) return relsXml;
|
|
81
|
+
const rel = `<Relationship Id="${escapedRId}" Type="${escapeXmlAttribute(type)}" Target="${escapeXmlAttribute(target)}"/>`;
|
|
82
|
+
return relsXml.replace("</Relationships>", () => `${rel}\n</Relationships>`);
|
|
75
83
|
};
|
|
76
84
|
//#endregion
|
|
77
85
|
export { DOCX_COMPRESSION, OOXML_NS, ensureContentType, ensureRelationship, extractBinary, extractText, findNextRId, loadDocx, repackZip };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/docx-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Low-level helpers for reading and writing DOCX/OOXML zip packages: text and binary extraction, relationship and content-type management, and namespace constants.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"docx",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"build": "tsdown",
|
|
41
41
|
"pack:dry-run": "bun pm pack --dry-run",
|
|
42
42
|
"test": "bun test src",
|
|
43
|
-
"typecheck": "
|
|
43
|
+
"typecheck": "bun ../../packages/scripts/src/tsc-native.ts --noEmit",
|
|
44
44
|
"lint": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --report-unused-disable-directives-severity=error --deny-warnings --type-aware packages/docx-utils",
|
|
45
45
|
"lint:fix": "cd ../.. && bun --bun oxlint -c oxlint.config.ts --type-aware --fix packages/docx-utils",
|
|
46
46
|
"format": "oxfmt .",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@stll/typescript-config": "0.0.0",
|
|
54
54
|
"@types/bun": "1.3.14",
|
|
55
|
-
"tsdown": "0.22.
|
|
55
|
+
"tsdown": "0.22.14"
|
|
56
56
|
},
|
|
57
57
|
"main": "./dist/index.js",
|
|
58
58
|
"types": "./dist/index.d.ts"
|