@docen/utils 0.0.9
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/LICENSE +21 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +192 -0
- package/dist/index.d.mts +192 -0
- package/dist/index.d.ts +192 -0
- package/dist/index.mjs +1 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Demo Macro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const DOCX_DPI=96;function convertTwipToPixels(r){return Math.round(r*96/1440)}function convertTwipToCssString(r){return`${convertTwipToPixels(r)}px`}function convertPixelsToTwip(r){return Math.round(r*(1440/96))}function convertEmuToPixels(r){return Math.round(r/(914400/96))}function convertPixelsToEmu(r){return Math.round(r*(914400/96))}function convertEmuStringToPixels(r){const n=parseInt(r,10);if(!isNaN(n))return convertEmuToPixels(n)}const i=/^([\d.]+)(px|pt|em|rem|%|)?$/,s={px:1,pt:1.333,em:16,rem:16,"%":.16};function convertCssLengthToPixels(r){if(!r)return 0;r=r.trim();const n=r.match(i);if(!n)return 0;const e=parseFloat(n[1]);if(isNaN(e))return 0;const t=n[2]||"px",o=s[t]??1;return Math.round(e*o)}const c=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,m={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6};function convertMeasureToInches(r){if(typeof r=="number")return r;const n=r.match(c);if(n){const t=parseFloat(n[1]),o=n[2],u=m[o];return u!==void 0?t*u:t}const e=parseFloat(r);return isNaN(e)?6.5:e}function convertMeasureToPixels(r){if(typeof r=="number")return r;const n=convertMeasureToInches(r);return Math.round(n*96)}const COLOR_NAME_TO_HEX={black:"#000000",white:"#FFFFFF",red:"#FF0000",green:"#008000",blue:"#0000FF",yellow:"#FFFF00",orange:"#FFA500",purple:"#800080",pink:"#FFC0CB",brown:"#A52A2A",gray:"#808080",grey:"#808080",cyan:"#00FFFF",magenta:"#FF00FF",lime:"#00FF00",navy:"#000080",teal:"#008080",maroon:"#800000",olive:"#808000",silver:"#C0C0C0",gold:"#FFD700",indigo:"#4B0082",violet:"#EE82EE",aqua:"#00FFFF",fuchsia:"#FF00FF",darkblue:"#00008B",darkcyan:"#008B8B",darkgrey:"#A9A9A9",darkgreen:"#006400",darkkhaki:"#BDB76B",darkmagenta:"#8B008B",darkolivegreen:"#556B2F",darkorange:"#FF8C00",darkorchid:"#9932CC",darkred:"#8B0000",darksalmon:"#E9967A",darkviolet:"#9400D3",lightblue:"#ADD8E6",lightcyan:"#E0FFFF",lightgreen:"#90EE90",lightgrey:"#D3D3D3",lightpink:"#FFB6C1",lightyellow:"#FFFFE0"};function convertColorToHex(r){if(r)return r.startsWith("#")?r:COLOR_NAME_TO_HEX[r.toLowerCase()]||r}function normalizeHexColor(r){return r.startsWith("#")?r:`#${r}`}function findChild(r,n){if(!r.children)return null;for(const e of r.children)if(e.type==="element"&&e.name===n)return e;return null}function findDeepChild(r,n){if(!r.children)return null;for(const e of r.children)if(e.type==="element"){if(e.name===n)return e;const t=findDeepChild(e,n);if(t)return t}return null}function findDeepChildren(r,n){const e=[];if(!r.children)return e;for(const t of r.children)t.type==="element"&&(t.name===n&&e.push(t),e.push(...findDeepChildren(t,n)));return e}function parseTwipAttr(r,n){const e=r[n];if(!e)return;const t=parseInt(e,10);if(!isNaN(t))return e}exports.COLOR_NAME_TO_HEX=COLOR_NAME_TO_HEX,exports.DOCX_DPI=96,exports.convertColorToHex=convertColorToHex,exports.convertCssLengthToPixels=convertCssLengthToPixels,exports.convertEmuStringToPixels=convertEmuStringToPixels,exports.convertEmuToPixels=convertEmuToPixels,exports.convertMeasureToInches=convertMeasureToInches,exports.convertMeasureToPixels=convertMeasureToPixels,exports.convertPixelsToEmu=convertPixelsToEmu,exports.convertPixelsToTwip=convertPixelsToTwip,exports.convertTwipToCssString=convertTwipToCssString,exports.convertTwipToPixels=convertTwipToPixels,exports.findChild=findChild,exports.findDeepChild=findDeepChild,exports.findDeepChildren=findDeepChildren,exports.normalizeHexColor=normalizeHexColor,exports.parseTwipAttr=parseTwipAttr;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Root, Element } from 'E:\\tiptap-extension\\node_modules\\.pnpm\\@types+xast@2.0.4\\node_modules\\@types\\xast\\index.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unit conversion utilities for DOCX processing
|
|
5
|
+
* Handles conversions between TWIPs, EMUs, pixels, and other units
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* DOCX DPI (dots per inch) for pixel conversions
|
|
9
|
+
* Word uses 96 DPI internally
|
|
10
|
+
*/
|
|
11
|
+
declare const DOCX_DPI = 96;
|
|
12
|
+
/**
|
|
13
|
+
* Convert TWIPs to CSS pixels (returns number)
|
|
14
|
+
* @param twip - Value in TWIPs (1 inch = 1440 TWIPs)
|
|
15
|
+
* @returns Number value in pixels
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* convertTwipToPixels(1440) // returns 96
|
|
19
|
+
*/
|
|
20
|
+
declare function convertTwipToPixels(twip: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Convert TWIPs to CSS string (returns "px" string)
|
|
23
|
+
* @param twip - Value in TWIPs
|
|
24
|
+
* @returns CSS value string in pixels (e.g., "20px")
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* convertTwipToCssString(1440) // returns "96px"
|
|
28
|
+
*/
|
|
29
|
+
declare function convertTwipToCssString(twip: number): string;
|
|
30
|
+
/**
|
|
31
|
+
* Convert pixels to TWIPs
|
|
32
|
+
* @param px - Value in pixels
|
|
33
|
+
* @returns Value in TWIPs
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* convertPixelsToTwip(96) // returns 1440
|
|
37
|
+
*/
|
|
38
|
+
declare function convertPixelsToTwip(px: number): number;
|
|
39
|
+
/**
|
|
40
|
+
* Convert EMUs to pixels
|
|
41
|
+
* EMU = English Metric Unit (1 inch = 914400 EMUs)
|
|
42
|
+
* @param emu - Value in EMUs
|
|
43
|
+
* @returns Value in pixels
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* convertEmuToPixels(914400) // returns 96
|
|
47
|
+
*/
|
|
48
|
+
declare function convertEmuToPixels(emu: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* Convert pixels to EMUs
|
|
51
|
+
* @param px - Value in pixels
|
|
52
|
+
* @returns Value in EMUs
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* convertPixelsToEmu(96) // returns 914400
|
|
56
|
+
*/
|
|
57
|
+
declare function convertPixelsToEmu(px: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* Convert EMU string to pixels
|
|
60
|
+
* @param emuStr - EMU value as string
|
|
61
|
+
* @returns Pixel value or undefined if invalid
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* convertEmuStringToPixels("914400") // returns 96
|
|
65
|
+
* convertEmuStringToPixels("invalid") // returns undefined
|
|
66
|
+
*/
|
|
67
|
+
declare function convertEmuStringToPixels(emuStr: string): number | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Parse CSS length value to pixels
|
|
70
|
+
* Supports: px, pt, em, rem, %, and unitless values
|
|
71
|
+
* @param value - CSS length value (e.g., "20px", "1.5em", "100%")
|
|
72
|
+
* @returns Value in pixels
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* convertCssLengthToPixels("20px") // returns 20
|
|
76
|
+
* convertCssLengthToPixels("1.5em") // returns 24
|
|
77
|
+
* convertCssLengthToPixels("100%") // returns 16
|
|
78
|
+
*/
|
|
79
|
+
declare function convertCssLengthToPixels(value: string): number;
|
|
80
|
+
/**
|
|
81
|
+
* Type for universal measure values in DOCX
|
|
82
|
+
* Can be a number (already in inches) or a string with unit suffix
|
|
83
|
+
*/
|
|
84
|
+
type PositiveUniversalMeasure = `${number}${"in" | "mm" | "cm" | "pt" | "pc" | "pi"}`;
|
|
85
|
+
/**
|
|
86
|
+
* Convert universal measure to inches
|
|
87
|
+
* Compatible with docx.js UniversalMeasure type
|
|
88
|
+
* @param value - Value in various units (number or string)
|
|
89
|
+
* @returns Value in inches
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* convertMeasureToInches(6.5) // returns 6.5
|
|
93
|
+
* convertMeasureToInches("1in") // returns 1
|
|
94
|
+
* convertMeasureToInches("25.4mm") // returns ~1
|
|
95
|
+
*/
|
|
96
|
+
declare function convertMeasureToInches(value: number | PositiveUniversalMeasure): number;
|
|
97
|
+
/**
|
|
98
|
+
* Convert universal measure to pixels
|
|
99
|
+
* Compatible with docx.js UniversalMeasure type
|
|
100
|
+
* @param value - Value in various units (number or string)
|
|
101
|
+
* @returns Value in pixels
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* convertMeasureToPixels("1in") // returns 96
|
|
105
|
+
* convertMeasureToPixels(6.5) // returns 624
|
|
106
|
+
*/
|
|
107
|
+
declare function convertMeasureToPixels(value: number | PositiveUniversalMeasure): number;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Color conversion utilities for DOCX processing
|
|
111
|
+
* Handles conversion between color names and hex values
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Color name to hex value mapping
|
|
115
|
+
* Includes common HTML/CSS color names
|
|
116
|
+
*/
|
|
117
|
+
declare const COLOR_NAME_TO_HEX: Record<string, string>;
|
|
118
|
+
/**
|
|
119
|
+
* Convert color name or hex to normalized hex value
|
|
120
|
+
* @param color - Color as name (e.g., "red") or hex (e.g., "#FF0000" or "FF0000")
|
|
121
|
+
* @returns Normalized hex color string (e.g., "#FF0000") or undefined if invalid
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* convertColorToHex("red") // returns "#FF0000"
|
|
125
|
+
* convertColorToHex("#FF0000") // returns "#FF0000"
|
|
126
|
+
* convertColorToHex("FF0000") // returns "#FF0000"
|
|
127
|
+
* convertColorToHex("invalid") // returns undefined
|
|
128
|
+
*/
|
|
129
|
+
declare function convertColorToHex(color?: string): string | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Normalize hex color to ensure it has a # prefix
|
|
132
|
+
* @param color - Color value with or without # prefix
|
|
133
|
+
* @returns Hex color with # prefix
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* normalizeHexColor("FF0000") // returns "#FF0000"
|
|
137
|
+
* normalizeHexColor("#FF0000") // returns "#FF0000"
|
|
138
|
+
*/
|
|
139
|
+
declare function normalizeHexColor(color: string): string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* XML utility functions for DOCX processing
|
|
143
|
+
* Provides helper functions for traversing and parsing XML trees (xast)
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Find direct child element with specified name
|
|
148
|
+
* @param node - Parent XML element or root node
|
|
149
|
+
* @param name - Child element name to find (can include namespace prefix, e.g., "w:p")
|
|
150
|
+
* @returns Child element if found, null otherwise
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const paragraph = findChild(document, "w:p");
|
|
154
|
+
*/
|
|
155
|
+
declare function findChild(node: Root | Element, name: string): Element | null;
|
|
156
|
+
/**
|
|
157
|
+
* Find deep descendant element with specified name (recursive)
|
|
158
|
+
* Searches through all descendants, not just direct children
|
|
159
|
+
* @param node - Root XML element
|
|
160
|
+
* @param name - Descendant element name to find
|
|
161
|
+
* @returns Descendant element if found, null otherwise
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* const textElement = findDeepChild(run, "w:t");
|
|
165
|
+
*/
|
|
166
|
+
declare function findDeepChild(node: Root | Element, name: string): Element | null;
|
|
167
|
+
/**
|
|
168
|
+
* Find all deep descendant elements with specified name (recursive)
|
|
169
|
+
* @param node - Root XML element
|
|
170
|
+
* @param name - Descendant element name to find
|
|
171
|
+
* @returns Array of matching descendant elements
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const allTextRuns = findDeepChildren(paragraph, "w:r");
|
|
175
|
+
*/
|
|
176
|
+
declare function findDeepChildren(node: Root | Element, name: string): Element[];
|
|
177
|
+
/**
|
|
178
|
+
* Parse TWIP attribute value from element attributes
|
|
179
|
+
* TWIP = Twentieth of a Point (1 inch = 1440 TWIPs)
|
|
180
|
+
* @param attributes - Element attributes object
|
|
181
|
+
* @param name - Attribute name to parse
|
|
182
|
+
* @returns TWIP value as string, or undefined if not found
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* const leftIndent = parseTwipAttr(pPr.attributes, "w:left");
|
|
186
|
+
*/
|
|
187
|
+
declare function parseTwipAttr(attributes: Record<string, any> | {
|
|
188
|
+
[key: string]: string | undefined;
|
|
189
|
+
}, name: string): string | undefined;
|
|
190
|
+
|
|
191
|
+
export { COLOR_NAME_TO_HEX, DOCX_DPI, convertColorToHex, convertCssLengthToPixels, convertEmuStringToPixels, convertEmuToPixels, convertMeasureToInches, convertMeasureToPixels, convertPixelsToEmu, convertPixelsToTwip, convertTwipToCssString, convertTwipToPixels, findChild, findDeepChild, findDeepChildren, normalizeHexColor, parseTwipAttr };
|
|
192
|
+
export type { PositiveUniversalMeasure };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Root, Element } from 'E:\\tiptap-extension\\node_modules\\.pnpm\\@types+xast@2.0.4\\node_modules\\@types\\xast\\index.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unit conversion utilities for DOCX processing
|
|
5
|
+
* Handles conversions between TWIPs, EMUs, pixels, and other units
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* DOCX DPI (dots per inch) for pixel conversions
|
|
9
|
+
* Word uses 96 DPI internally
|
|
10
|
+
*/
|
|
11
|
+
declare const DOCX_DPI = 96;
|
|
12
|
+
/**
|
|
13
|
+
* Convert TWIPs to CSS pixels (returns number)
|
|
14
|
+
* @param twip - Value in TWIPs (1 inch = 1440 TWIPs)
|
|
15
|
+
* @returns Number value in pixels
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* convertTwipToPixels(1440) // returns 96
|
|
19
|
+
*/
|
|
20
|
+
declare function convertTwipToPixels(twip: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Convert TWIPs to CSS string (returns "px" string)
|
|
23
|
+
* @param twip - Value in TWIPs
|
|
24
|
+
* @returns CSS value string in pixels (e.g., "20px")
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* convertTwipToCssString(1440) // returns "96px"
|
|
28
|
+
*/
|
|
29
|
+
declare function convertTwipToCssString(twip: number): string;
|
|
30
|
+
/**
|
|
31
|
+
* Convert pixels to TWIPs
|
|
32
|
+
* @param px - Value in pixels
|
|
33
|
+
* @returns Value in TWIPs
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* convertPixelsToTwip(96) // returns 1440
|
|
37
|
+
*/
|
|
38
|
+
declare function convertPixelsToTwip(px: number): number;
|
|
39
|
+
/**
|
|
40
|
+
* Convert EMUs to pixels
|
|
41
|
+
* EMU = English Metric Unit (1 inch = 914400 EMUs)
|
|
42
|
+
* @param emu - Value in EMUs
|
|
43
|
+
* @returns Value in pixels
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* convertEmuToPixels(914400) // returns 96
|
|
47
|
+
*/
|
|
48
|
+
declare function convertEmuToPixels(emu: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* Convert pixels to EMUs
|
|
51
|
+
* @param px - Value in pixels
|
|
52
|
+
* @returns Value in EMUs
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* convertPixelsToEmu(96) // returns 914400
|
|
56
|
+
*/
|
|
57
|
+
declare function convertPixelsToEmu(px: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* Convert EMU string to pixels
|
|
60
|
+
* @param emuStr - EMU value as string
|
|
61
|
+
* @returns Pixel value or undefined if invalid
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* convertEmuStringToPixels("914400") // returns 96
|
|
65
|
+
* convertEmuStringToPixels("invalid") // returns undefined
|
|
66
|
+
*/
|
|
67
|
+
declare function convertEmuStringToPixels(emuStr: string): number | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Parse CSS length value to pixels
|
|
70
|
+
* Supports: px, pt, em, rem, %, and unitless values
|
|
71
|
+
* @param value - CSS length value (e.g., "20px", "1.5em", "100%")
|
|
72
|
+
* @returns Value in pixels
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* convertCssLengthToPixels("20px") // returns 20
|
|
76
|
+
* convertCssLengthToPixels("1.5em") // returns 24
|
|
77
|
+
* convertCssLengthToPixels("100%") // returns 16
|
|
78
|
+
*/
|
|
79
|
+
declare function convertCssLengthToPixels(value: string): number;
|
|
80
|
+
/**
|
|
81
|
+
* Type for universal measure values in DOCX
|
|
82
|
+
* Can be a number (already in inches) or a string with unit suffix
|
|
83
|
+
*/
|
|
84
|
+
type PositiveUniversalMeasure = `${number}${"in" | "mm" | "cm" | "pt" | "pc" | "pi"}`;
|
|
85
|
+
/**
|
|
86
|
+
* Convert universal measure to inches
|
|
87
|
+
* Compatible with docx.js UniversalMeasure type
|
|
88
|
+
* @param value - Value in various units (number or string)
|
|
89
|
+
* @returns Value in inches
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* convertMeasureToInches(6.5) // returns 6.5
|
|
93
|
+
* convertMeasureToInches("1in") // returns 1
|
|
94
|
+
* convertMeasureToInches("25.4mm") // returns ~1
|
|
95
|
+
*/
|
|
96
|
+
declare function convertMeasureToInches(value: number | PositiveUniversalMeasure): number;
|
|
97
|
+
/**
|
|
98
|
+
* Convert universal measure to pixels
|
|
99
|
+
* Compatible with docx.js UniversalMeasure type
|
|
100
|
+
* @param value - Value in various units (number or string)
|
|
101
|
+
* @returns Value in pixels
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* convertMeasureToPixels("1in") // returns 96
|
|
105
|
+
* convertMeasureToPixels(6.5) // returns 624
|
|
106
|
+
*/
|
|
107
|
+
declare function convertMeasureToPixels(value: number | PositiveUniversalMeasure): number;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Color conversion utilities for DOCX processing
|
|
111
|
+
* Handles conversion between color names and hex values
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Color name to hex value mapping
|
|
115
|
+
* Includes common HTML/CSS color names
|
|
116
|
+
*/
|
|
117
|
+
declare const COLOR_NAME_TO_HEX: Record<string, string>;
|
|
118
|
+
/**
|
|
119
|
+
* Convert color name or hex to normalized hex value
|
|
120
|
+
* @param color - Color as name (e.g., "red") or hex (e.g., "#FF0000" or "FF0000")
|
|
121
|
+
* @returns Normalized hex color string (e.g., "#FF0000") or undefined if invalid
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* convertColorToHex("red") // returns "#FF0000"
|
|
125
|
+
* convertColorToHex("#FF0000") // returns "#FF0000"
|
|
126
|
+
* convertColorToHex("FF0000") // returns "#FF0000"
|
|
127
|
+
* convertColorToHex("invalid") // returns undefined
|
|
128
|
+
*/
|
|
129
|
+
declare function convertColorToHex(color?: string): string | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Normalize hex color to ensure it has a # prefix
|
|
132
|
+
* @param color - Color value with or without # prefix
|
|
133
|
+
* @returns Hex color with # prefix
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* normalizeHexColor("FF0000") // returns "#FF0000"
|
|
137
|
+
* normalizeHexColor("#FF0000") // returns "#FF0000"
|
|
138
|
+
*/
|
|
139
|
+
declare function normalizeHexColor(color: string): string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* XML utility functions for DOCX processing
|
|
143
|
+
* Provides helper functions for traversing and parsing XML trees (xast)
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Find direct child element with specified name
|
|
148
|
+
* @param node - Parent XML element or root node
|
|
149
|
+
* @param name - Child element name to find (can include namespace prefix, e.g., "w:p")
|
|
150
|
+
* @returns Child element if found, null otherwise
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const paragraph = findChild(document, "w:p");
|
|
154
|
+
*/
|
|
155
|
+
declare function findChild(node: Root | Element, name: string): Element | null;
|
|
156
|
+
/**
|
|
157
|
+
* Find deep descendant element with specified name (recursive)
|
|
158
|
+
* Searches through all descendants, not just direct children
|
|
159
|
+
* @param node - Root XML element
|
|
160
|
+
* @param name - Descendant element name to find
|
|
161
|
+
* @returns Descendant element if found, null otherwise
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* const textElement = findDeepChild(run, "w:t");
|
|
165
|
+
*/
|
|
166
|
+
declare function findDeepChild(node: Root | Element, name: string): Element | null;
|
|
167
|
+
/**
|
|
168
|
+
* Find all deep descendant elements with specified name (recursive)
|
|
169
|
+
* @param node - Root XML element
|
|
170
|
+
* @param name - Descendant element name to find
|
|
171
|
+
* @returns Array of matching descendant elements
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const allTextRuns = findDeepChildren(paragraph, "w:r");
|
|
175
|
+
*/
|
|
176
|
+
declare function findDeepChildren(node: Root | Element, name: string): Element[];
|
|
177
|
+
/**
|
|
178
|
+
* Parse TWIP attribute value from element attributes
|
|
179
|
+
* TWIP = Twentieth of a Point (1 inch = 1440 TWIPs)
|
|
180
|
+
* @param attributes - Element attributes object
|
|
181
|
+
* @param name - Attribute name to parse
|
|
182
|
+
* @returns TWIP value as string, or undefined if not found
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* const leftIndent = parseTwipAttr(pPr.attributes, "w:left");
|
|
186
|
+
*/
|
|
187
|
+
declare function parseTwipAttr(attributes: Record<string, any> | {
|
|
188
|
+
[key: string]: string | undefined;
|
|
189
|
+
}, name: string): string | undefined;
|
|
190
|
+
|
|
191
|
+
export { COLOR_NAME_TO_HEX, DOCX_DPI, convertColorToHex, convertCssLengthToPixels, convertEmuStringToPixels, convertEmuToPixels, convertMeasureToInches, convertMeasureToPixels, convertPixelsToEmu, convertPixelsToTwip, convertTwipToCssString, convertTwipToPixels, findChild, findDeepChild, findDeepChildren, normalizeHexColor, parseTwipAttr };
|
|
192
|
+
export type { PositiveUniversalMeasure };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Root, Element } from 'E:\\tiptap-extension\\node_modules\\.pnpm\\@types+xast@2.0.4\\node_modules\\@types\\xast\\index.d.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unit conversion utilities for DOCX processing
|
|
5
|
+
* Handles conversions between TWIPs, EMUs, pixels, and other units
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* DOCX DPI (dots per inch) for pixel conversions
|
|
9
|
+
* Word uses 96 DPI internally
|
|
10
|
+
*/
|
|
11
|
+
declare const DOCX_DPI = 96;
|
|
12
|
+
/**
|
|
13
|
+
* Convert TWIPs to CSS pixels (returns number)
|
|
14
|
+
* @param twip - Value in TWIPs (1 inch = 1440 TWIPs)
|
|
15
|
+
* @returns Number value in pixels
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* convertTwipToPixels(1440) // returns 96
|
|
19
|
+
*/
|
|
20
|
+
declare function convertTwipToPixels(twip: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Convert TWIPs to CSS string (returns "px" string)
|
|
23
|
+
* @param twip - Value in TWIPs
|
|
24
|
+
* @returns CSS value string in pixels (e.g., "20px")
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* convertTwipToCssString(1440) // returns "96px"
|
|
28
|
+
*/
|
|
29
|
+
declare function convertTwipToCssString(twip: number): string;
|
|
30
|
+
/**
|
|
31
|
+
* Convert pixels to TWIPs
|
|
32
|
+
* @param px - Value in pixels
|
|
33
|
+
* @returns Value in TWIPs
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* convertPixelsToTwip(96) // returns 1440
|
|
37
|
+
*/
|
|
38
|
+
declare function convertPixelsToTwip(px: number): number;
|
|
39
|
+
/**
|
|
40
|
+
* Convert EMUs to pixels
|
|
41
|
+
* EMU = English Metric Unit (1 inch = 914400 EMUs)
|
|
42
|
+
* @param emu - Value in EMUs
|
|
43
|
+
* @returns Value in pixels
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* convertEmuToPixels(914400) // returns 96
|
|
47
|
+
*/
|
|
48
|
+
declare function convertEmuToPixels(emu: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* Convert pixels to EMUs
|
|
51
|
+
* @param px - Value in pixels
|
|
52
|
+
* @returns Value in EMUs
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* convertPixelsToEmu(96) // returns 914400
|
|
56
|
+
*/
|
|
57
|
+
declare function convertPixelsToEmu(px: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* Convert EMU string to pixels
|
|
60
|
+
* @param emuStr - EMU value as string
|
|
61
|
+
* @returns Pixel value or undefined if invalid
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* convertEmuStringToPixels("914400") // returns 96
|
|
65
|
+
* convertEmuStringToPixels("invalid") // returns undefined
|
|
66
|
+
*/
|
|
67
|
+
declare function convertEmuStringToPixels(emuStr: string): number | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Parse CSS length value to pixels
|
|
70
|
+
* Supports: px, pt, em, rem, %, and unitless values
|
|
71
|
+
* @param value - CSS length value (e.g., "20px", "1.5em", "100%")
|
|
72
|
+
* @returns Value in pixels
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* convertCssLengthToPixels("20px") // returns 20
|
|
76
|
+
* convertCssLengthToPixels("1.5em") // returns 24
|
|
77
|
+
* convertCssLengthToPixels("100%") // returns 16
|
|
78
|
+
*/
|
|
79
|
+
declare function convertCssLengthToPixels(value: string): number;
|
|
80
|
+
/**
|
|
81
|
+
* Type for universal measure values in DOCX
|
|
82
|
+
* Can be a number (already in inches) or a string with unit suffix
|
|
83
|
+
*/
|
|
84
|
+
type PositiveUniversalMeasure = `${number}${"in" | "mm" | "cm" | "pt" | "pc" | "pi"}`;
|
|
85
|
+
/**
|
|
86
|
+
* Convert universal measure to inches
|
|
87
|
+
* Compatible with docx.js UniversalMeasure type
|
|
88
|
+
* @param value - Value in various units (number or string)
|
|
89
|
+
* @returns Value in inches
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* convertMeasureToInches(6.5) // returns 6.5
|
|
93
|
+
* convertMeasureToInches("1in") // returns 1
|
|
94
|
+
* convertMeasureToInches("25.4mm") // returns ~1
|
|
95
|
+
*/
|
|
96
|
+
declare function convertMeasureToInches(value: number | PositiveUniversalMeasure): number;
|
|
97
|
+
/**
|
|
98
|
+
* Convert universal measure to pixels
|
|
99
|
+
* Compatible with docx.js UniversalMeasure type
|
|
100
|
+
* @param value - Value in various units (number or string)
|
|
101
|
+
* @returns Value in pixels
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* convertMeasureToPixels("1in") // returns 96
|
|
105
|
+
* convertMeasureToPixels(6.5) // returns 624
|
|
106
|
+
*/
|
|
107
|
+
declare function convertMeasureToPixels(value: number | PositiveUniversalMeasure): number;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Color conversion utilities for DOCX processing
|
|
111
|
+
* Handles conversion between color names and hex values
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Color name to hex value mapping
|
|
115
|
+
* Includes common HTML/CSS color names
|
|
116
|
+
*/
|
|
117
|
+
declare const COLOR_NAME_TO_HEX: Record<string, string>;
|
|
118
|
+
/**
|
|
119
|
+
* Convert color name or hex to normalized hex value
|
|
120
|
+
* @param color - Color as name (e.g., "red") or hex (e.g., "#FF0000" or "FF0000")
|
|
121
|
+
* @returns Normalized hex color string (e.g., "#FF0000") or undefined if invalid
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* convertColorToHex("red") // returns "#FF0000"
|
|
125
|
+
* convertColorToHex("#FF0000") // returns "#FF0000"
|
|
126
|
+
* convertColorToHex("FF0000") // returns "#FF0000"
|
|
127
|
+
* convertColorToHex("invalid") // returns undefined
|
|
128
|
+
*/
|
|
129
|
+
declare function convertColorToHex(color?: string): string | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Normalize hex color to ensure it has a # prefix
|
|
132
|
+
* @param color - Color value with or without # prefix
|
|
133
|
+
* @returns Hex color with # prefix
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* normalizeHexColor("FF0000") // returns "#FF0000"
|
|
137
|
+
* normalizeHexColor("#FF0000") // returns "#FF0000"
|
|
138
|
+
*/
|
|
139
|
+
declare function normalizeHexColor(color: string): string;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* XML utility functions for DOCX processing
|
|
143
|
+
* Provides helper functions for traversing and parsing XML trees (xast)
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Find direct child element with specified name
|
|
148
|
+
* @param node - Parent XML element or root node
|
|
149
|
+
* @param name - Child element name to find (can include namespace prefix, e.g., "w:p")
|
|
150
|
+
* @returns Child element if found, null otherwise
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const paragraph = findChild(document, "w:p");
|
|
154
|
+
*/
|
|
155
|
+
declare function findChild(node: Root | Element, name: string): Element | null;
|
|
156
|
+
/**
|
|
157
|
+
* Find deep descendant element with specified name (recursive)
|
|
158
|
+
* Searches through all descendants, not just direct children
|
|
159
|
+
* @param node - Root XML element
|
|
160
|
+
* @param name - Descendant element name to find
|
|
161
|
+
* @returns Descendant element if found, null otherwise
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* const textElement = findDeepChild(run, "w:t");
|
|
165
|
+
*/
|
|
166
|
+
declare function findDeepChild(node: Root | Element, name: string): Element | null;
|
|
167
|
+
/**
|
|
168
|
+
* Find all deep descendant elements with specified name (recursive)
|
|
169
|
+
* @param node - Root XML element
|
|
170
|
+
* @param name - Descendant element name to find
|
|
171
|
+
* @returns Array of matching descendant elements
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const allTextRuns = findDeepChildren(paragraph, "w:r");
|
|
175
|
+
*/
|
|
176
|
+
declare function findDeepChildren(node: Root | Element, name: string): Element[];
|
|
177
|
+
/**
|
|
178
|
+
* Parse TWIP attribute value from element attributes
|
|
179
|
+
* TWIP = Twentieth of a Point (1 inch = 1440 TWIPs)
|
|
180
|
+
* @param attributes - Element attributes object
|
|
181
|
+
* @param name - Attribute name to parse
|
|
182
|
+
* @returns TWIP value as string, or undefined if not found
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* const leftIndent = parseTwipAttr(pPr.attributes, "w:left");
|
|
186
|
+
*/
|
|
187
|
+
declare function parseTwipAttr(attributes: Record<string, any> | {
|
|
188
|
+
[key: string]: string | undefined;
|
|
189
|
+
}, name: string): string | undefined;
|
|
190
|
+
|
|
191
|
+
export { COLOR_NAME_TO_HEX, DOCX_DPI, convertColorToHex, convertCssLengthToPixels, convertEmuStringToPixels, convertEmuToPixels, convertMeasureToInches, convertMeasureToPixels, convertPixelsToEmu, convertPixelsToTwip, convertTwipToCssString, convertTwipToPixels, findChild, findDeepChild, findDeepChildren, normalizeHexColor, parseTwipAttr };
|
|
192
|
+
export type { PositiveUniversalMeasure };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const f=96;function u(r){return Math.round(r*96/1440)}function d(r){return`${u(r)}px`}function p(r){return Math.round(r*(1440/96))}function c(r){return Math.round(r/(914400/96))}function h(r){return Math.round(r*(914400/96))}function m(r){const e=parseInt(r,10);if(!isNaN(e))return c(e)}const g=/^([\d.]+)(px|pt|em|rem|%|)?$/,C={px:1,pt:1.333,em:16,rem:16,"%":.16};function k(r){if(!r)return 0;r=r.trim();const e=r.match(g);if(!e)return 0;const n=parseFloat(e[1]);if(isNaN(n))return 0;const t=e[2]||"px",o=C[t]??1;return Math.round(n*o)}const v=/^([\d.]+)(in|mm|cm|pt|pc|pi)$/,y={in:1,mm:1/25.4,cm:1/2.54,pt:1/72,pc:1/6,pi:1/6};function a(r){if(typeof r=="number")return r;const e=r.match(v);if(e){const t=parseFloat(e[1]),o=e[2],i=y[o];return i!==void 0?t*i:t}const n=parseFloat(r);return isNaN(n)?6.5:n}function E(r){if(typeof r=="number")return r;const e=a(r);return Math.round(e*96)}const l={black:"#000000",white:"#FFFFFF",red:"#FF0000",green:"#008000",blue:"#0000FF",yellow:"#FFFF00",orange:"#FFA500",purple:"#800080",pink:"#FFC0CB",brown:"#A52A2A",gray:"#808080",grey:"#808080",cyan:"#00FFFF",magenta:"#FF00FF",lime:"#00FF00",navy:"#000080",teal:"#008080",maroon:"#800000",olive:"#808000",silver:"#C0C0C0",gold:"#FFD700",indigo:"#4B0082",violet:"#EE82EE",aqua:"#00FFFF",fuchsia:"#FF00FF",darkblue:"#00008B",darkcyan:"#008B8B",darkgrey:"#A9A9A9",darkgreen:"#006400",darkkhaki:"#BDB76B",darkmagenta:"#8B008B",darkolivegreen:"#556B2F",darkorange:"#FF8C00",darkorchid:"#9932CC",darkred:"#8B0000",darksalmon:"#E9967A",darkviolet:"#9400D3",lightblue:"#ADD8E6",lightcyan:"#E0FFFF",lightgreen:"#90EE90",lightgrey:"#D3D3D3",lightpink:"#FFB6C1",lightyellow:"#FFFFE0"};function T(r){if(r)return r.startsWith("#")?r:l[r.toLowerCase()]||r}function x(r){return r.startsWith("#")?r:`#${r}`}function B(r,e){if(!r.children)return null;for(const n of r.children)if(n.type==="element"&&n.name===e)return n;return null}function s(r,e){if(!r.children)return null;for(const n of r.children)if(n.type==="element"){if(n.name===e)return n;const t=s(n,e);if(t)return t}return null}function F(r,e){const n=[];if(!r.children)return n;for(const t of r.children)t.type==="element"&&(t.name===e&&n.push(t),n.push(...F(t,e)));return n}function D(r,e){const n=r[e];if(!n)return;const t=parseInt(n,10);if(!isNaN(t))return n}export{l as COLOR_NAME_TO_HEX,f as DOCX_DPI,T as convertColorToHex,k as convertCssLengthToPixels,m as convertEmuStringToPixels,c as convertEmuToPixels,a as convertMeasureToInches,E as convertMeasureToPixels,h as convertPixelsToEmu,p as convertPixelsToTwip,d as convertTwipToCssString,u as convertTwipToPixels,B as findChild,s as findDeepChild,F as findDeepChildren,x as normalizeHexColor,D as parseTwipAttr};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@docen/utils",
|
|
3
|
+
"version": "0.0.9",
|
|
4
|
+
"description": "Shared utility functions for DOCX processing, including unit conversions, color transformations, and XML helpers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"color",
|
|
7
|
+
"conversion",
|
|
8
|
+
"docen",
|
|
9
|
+
"docx",
|
|
10
|
+
"unit-conversion",
|
|
11
|
+
"utilities",
|
|
12
|
+
"xml"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/DemoMacro/docen#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/DemoMacro/docen/issues"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "Demo Macro",
|
|
21
|
+
"email": "abc@imst.xyz",
|
|
22
|
+
"url": "https://imst.xyz/"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/DemoMacro/docen.git"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"main": "dist/index.mjs",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/index.mjs",
|
|
36
|
+
"require": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/xast": "2.0.4"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"dev": "unbuild --stub",
|
|
44
|
+
"build": "unbuild"
|
|
45
|
+
}
|
|
46
|
+
}
|