Compare commits
35 Commits
Author | SHA1 | Date | |
---|---|---|---|
3eb98533ae | |||
f5f021834e | |||
35702c3f77 | |||
8fbbd571ad | |||
2dd228be69 | |||
562835cfe7 | |||
6bac06e84d | |||
88436168ee | |||
0c3206d2e2 | |||
033debd339 | |||
d4f160732a | |||
4238fc9ab4 | |||
0d042b8dd1 | |||
afdd5f2d8f | |||
70c7b3d1b3 | |||
7b1cd5fe86 | |||
09ab169ffd | |||
35a82cf12e | |||
8d33cb01dd | |||
438d11dd86 | |||
d23b0d0789 | |||
1fa8c7ac82 | |||
3d6ead0359 | |||
379050dccd | |||
f9d1c197cf | |||
a89ee463e6 | |||
036caaacc8 | |||
80c37afe2b | |||
3a36d912fe | |||
c741d5d8ac | |||
0fa7dd13ad | |||
066aa56f6a | |||
3997b7a5d6 | |||
9577192d41 | |||
f2b1587bff |
3011
package-lock.json
generated
3011
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docx",
|
"name": "docx",
|
||||||
"version": "8.2.3",
|
"version": "8.2.4",
|
||||||
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
|
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "build/index.umd.js",
|
"main": "build/index.umd.js",
|
||||||
@ -67,16 +67,15 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://docx.js.org",
|
"homepage": "https://docx.js.org",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@esbuild/win32-x64": "^0.18.3",
|
|
||||||
"@types/inquirer": "^9.0.3",
|
"@types/inquirer": "^9.0.3",
|
||||||
"@types/prompt": "^1.1.1",
|
"@types/prompt": "^1.1.1",
|
||||||
"@types/unzipper": "^0.10.4",
|
"@types/unzipper": "^0.10.4",
|
||||||
"@types/xml": "^1.0.8",
|
"@types/xml": "^1.0.8",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.36.1",
|
"@typescript-eslint/eslint-plugin": "^6.9.1",
|
||||||
"@typescript-eslint/parser": "^5.36.1",
|
"@typescript-eslint/parser": "^6.9.1",
|
||||||
"@vitest/coverage-v8": "^0.33.0",
|
"@vitest/coverage-v8": "^0.33.0",
|
||||||
"@vitest/ui": "^0.33.0",
|
"@vitest/ui": "^0.33.0",
|
||||||
"cspell": "^6.2.2",
|
"cspell": "^7.3.8",
|
||||||
"docsify-cli": "^4.3.0",
|
"docsify-cli": "^4.3.0",
|
||||||
"eslint": "^8.23.0",
|
"eslint": "^8.23.0",
|
||||||
"eslint-plugin-functional": "^5.0.8",
|
"eslint-plugin-functional": "^5.0.8",
|
||||||
@ -84,8 +83,8 @@
|
|||||||
"eslint-plugin-jsdoc": "^46.2.6",
|
"eslint-plugin-jsdoc": "^46.2.6",
|
||||||
"eslint-plugin-no-null": "^1.0.2",
|
"eslint-plugin-no-null": "^1.0.2",
|
||||||
"eslint-plugin-prefer-arrow": "^1.2.3",
|
"eslint-plugin-prefer-arrow": "^1.2.3",
|
||||||
"eslint-plugin-unicorn": "^47.0.0",
|
"eslint-plugin-unicorn": "^48.0.1",
|
||||||
"execa": "^7.1.1",
|
"execa": "^8.0.1",
|
||||||
"glob": "^10.2.7",
|
"glob": "^10.2.7",
|
||||||
"inquirer": "^9.2.7",
|
"inquirer": "^9.2.7",
|
||||||
"jsdom": "^22.1.0",
|
"jsdom": "^22.1.0",
|
||||||
|
@ -5,7 +5,6 @@ import { FooterWrapper } from "@file/footer-wrapper";
|
|||||||
import { HeaderWrapper } from "@file/header-wrapper";
|
import { HeaderWrapper } from "@file/header-wrapper";
|
||||||
import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align";
|
import { VerticalAlign, VerticalAlignElement } from "@file/vertical-align";
|
||||||
import { OnOffElement, XmlComponent } from "@file/xml-components";
|
import { OnOffElement, XmlComponent } from "@file/xml-components";
|
||||||
import { PositiveUniversalMeasure, UniversalMeasure } from "@util/values";
|
|
||||||
|
|
||||||
import { HeaderFooterReference, HeaderFooterReferenceType, HeaderFooterType } from "./properties/header-footer-reference";
|
import { HeaderFooterReference, HeaderFooterReferenceType, HeaderFooterType } from "./properties/header-footer-reference";
|
||||||
import { Columns, IColumnsAttributes } from "./properties/columns";
|
import { Columns, IColumnsAttributes } from "./properties/columns";
|
||||||
@ -76,10 +75,10 @@ export interface ISectionPropertiesOptions {
|
|||||||
// </xsd:group>
|
// </xsd:group>
|
||||||
|
|
||||||
export const sectionMarginDefaults = {
|
export const sectionMarginDefaults = {
|
||||||
TOP: "1in" as UniversalMeasure,
|
TOP: 1440,
|
||||||
RIGHT: "1in" as PositiveUniversalMeasure,
|
RIGHT: 1440,
|
||||||
BOTTOM: "1in" as UniversalMeasure,
|
BOTTOM: 1440,
|
||||||
LEFT: "1in" as PositiveUniversalMeasure,
|
LEFT: 1440,
|
||||||
HEADER: 708,
|
HEADER: 708,
|
||||||
FOOTER: 708,
|
FOOTER: 708,
|
||||||
GUTTER: 0,
|
GUTTER: 0,
|
||||||
|
@ -80,6 +80,7 @@ export class Document extends XmlComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public add(item: Paragraph | Table | TableOfContents | ConcreteHyperlink): Document {
|
public add(item: Paragraph | Table | TableOfContents | ConcreteHyperlink): Document {
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
this.body.push(item);
|
this.body.push(item);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,7 @@ export class Numbering extends XmlComponent {
|
|||||||
abstractNumId: abstractNumbering.id,
|
abstractNumId: abstractNumbering.id,
|
||||||
reference,
|
reference,
|
||||||
instance,
|
instance,
|
||||||
overrideLevel:
|
overrideLevels: [
|
||||||
firstLevelStartNumber && Number.isInteger(firstLevelStartNumber)
|
firstLevelStartNumber && Number.isInteger(firstLevelStartNumber)
|
||||||
? {
|
? {
|
||||||
num: 0,
|
num: 0,
|
||||||
@ -226,6 +226,7 @@ export class Numbering extends XmlComponent {
|
|||||||
num: 0,
|
num: 0,
|
||||||
start: 1,
|
start: 1,
|
||||||
},
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
this.concreteNumberingMap.set(fullReference, new ConcreteNumbering(concreteNumberingSettings));
|
this.concreteNumberingMap.set(fullReference, new ConcreteNumbering(concreteNumberingSettings));
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// http://officeopenxml.com/WPparagraphProperties.php
|
// http://officeopenxml.com/WPparagraphProperties.php
|
||||||
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_suppressLineNumbers_topic_ID0ECJAO.html
|
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_suppressLineNumbers_topic_ID0ECJAO.html
|
||||||
|
/* eslint-disable functional/immutable-data */
|
||||||
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, OnOffElement, XmlComponent } from "@file/xml-components";
|
import { IContext, IgnoreIfEmptyXmlComponent, IXmlableObject, OnOffElement, XmlComponent } from "@file/xml-components";
|
||||||
import { DocumentWrapper } from "../document-wrapper";
|
import { DocumentWrapper } from "../document-wrapper";
|
||||||
import { IShadingAttributesProperties, Shading } from "../shading";
|
import { IShadingAttributesProperties, Shading } from "../shading";
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// https://www.ecma-international.org/wp-content/uploads/ECMA-376-1_5th_edition_december_2016.zip page 297, section 17.3.2.21
|
// https://www.ecma-international.org/wp-content/uploads/ECMA-376-1_5th_edition_december_2016.zip page 297, section 17.3.2.21
|
||||||
|
/* eslint-disable functional/immutable-data */
|
||||||
import { BorderElement, IBorderOptions } from "@file/border";
|
import { BorderElement, IBorderOptions } from "@file/border";
|
||||||
import { IShadingAttributesProperties, Shading } from "@file/shading";
|
import { IShadingAttributesProperties, Shading } from "@file/shading";
|
||||||
import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision";
|
import { ChangeAttributes, IChangedAttributesProperties } from "@file/track-revision/track-revision";
|
||||||
|
@ -55,6 +55,7 @@ describe("ImportedXmlComponent", () => {
|
|||||||
otherAttr: "2",
|
otherAttr: "2",
|
||||||
};
|
};
|
||||||
importedXmlComponent = new ImportedXmlComponent("w:test", attributes);
|
importedXmlComponent = new ImportedXmlComponent("w:test", attributes);
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
importedXmlComponent.push(new ImportedXmlComponent("w:child"));
|
importedXmlComponent.push(new ImportedXmlComponent("w:child"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ export const convertToXmlComponent = (element: XmlElement): ImportedXmlComponent
|
|||||||
for (const childElm of childElements) {
|
for (const childElm of childElements) {
|
||||||
const child = convertToXmlComponent(childElm);
|
const child = convertToXmlComponent(childElm);
|
||||||
if (child !== undefined) {
|
if (child !== undefined) {
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
xmlComponent.push(child);
|
xmlComponent.push(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,6 +61,7 @@ export class ImportedXmlComponent extends XmlComponent {
|
|||||||
public constructor(rootKey: string, _attr?: any) {
|
public constructor(rootKey: string, _attr?: any) {
|
||||||
super(rootKey);
|
super(rootKey);
|
||||||
if (_attr) {
|
if (_attr) {
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
this.root.push(new ImportedXmlComponentAttributes(_attr));
|
this.root.push(new ImportedXmlComponentAttributes(_attr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,13 @@ describe("XmlComponent", () => {
|
|||||||
});
|
});
|
||||||
it("should handle children elements", () => {
|
it("should handle children elements", () => {
|
||||||
const xmlComponent = new TestComponent("w:test");
|
const xmlComponent = new TestComponent("w:test");
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
xmlComponent.push(
|
xmlComponent.push(
|
||||||
new Attributes({
|
new Attributes({
|
||||||
val: "test",
|
val: "test",
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
xmlComponent.push(new TestComponent("innerTest"));
|
xmlComponent.push(new TestComponent("innerTest"));
|
||||||
|
|
||||||
const tree = new Formatter().format(xmlComponent);
|
const tree = new Formatter().format(xmlComponent);
|
||||||
@ -43,6 +45,7 @@ describe("XmlComponent", () => {
|
|||||||
});
|
});
|
||||||
it("should hoist attrs if only attrs are present", () => {
|
it("should hoist attrs if only attrs are present", () => {
|
||||||
const xmlComponent = new TestComponent("w:test");
|
const xmlComponent = new TestComponent("w:test");
|
||||||
|
// eslint-disable-next-line functional/immutable-data
|
||||||
xmlComponent.push(
|
xmlComponent.push(
|
||||||
new Attributes({
|
new Attributes({
|
||||||
val: "test",
|
val: "test",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
export interface IXmlAttribute {
|
export interface IXmlAttribute {
|
||||||
readonly [key: string]: string | number | boolean;
|
readonly [key: string]: string | number | boolean;
|
||||||
}
|
}
|
||||||
export interface IXmlableObject extends Object {
|
export interface IXmlableObject extends Record<string, unknown> {
|
||||||
// readonly _attr?: IXmlAttribute;
|
// readonly _attr?: IXmlAttribute;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
readonly [key: string]: any;
|
readonly [key: string]: any;
|
||||||
|
@ -49,6 +49,7 @@ export type IPatch = ParagraphPatch | FilePatch;
|
|||||||
|
|
||||||
export interface PatchDocumentOptions {
|
export interface PatchDocumentOptions {
|
||||||
readonly patches: { readonly [key: string]: IPatch };
|
readonly patches: { readonly [key: string]: IPatch };
|
||||||
|
readonly keepOriginalStyles?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageReplacer = new ImageReplacer();
|
const imageReplacer = new ImageReplacer();
|
||||||
@ -128,6 +129,7 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
|
|||||||
patchText,
|
patchText,
|
||||||
renderedParagraphs,
|
renderedParagraphs,
|
||||||
context,
|
context,
|
||||||
|
options.keepOriginalStyles,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ export const replaceTokenInParagraphElement = ({
|
|||||||
patchTextElement(paragraphElement.elements![run.index].elements![index], firstPart);
|
patchTextElement(paragraphElement.elements![run.index].elements![index], firstPart);
|
||||||
replaceMode = ReplaceMode.MIDDLE;
|
replaceMode = ReplaceMode.MIDDLE;
|
||||||
continue;
|
continue;
|
||||||
|
/* c8 ignore next 2 */
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ReplaceMode.MIDDLE:
|
case ReplaceMode.MIDDLE:
|
||||||
@ -59,6 +60,7 @@ export const replaceTokenInParagraphElement = ({
|
|||||||
patchTextElement(paragraphElement.elements![run.index].elements![index], "");
|
patchTextElement(paragraphElement.elements![run.index].elements![index], "");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
/* c8 ignore next */
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,28 @@ const MOCK_JSON = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:p",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:r",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:rPr",
|
||||||
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:t",
|
||||||
|
elements: [{ type: "text", text: "What a {{bold}} text!" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -115,6 +137,93 @@ describe("replacer", () => {
|
|||||||
expect(JSON.stringify(output)).to.contain("Delightful Header");
|
expect(JSON.stringify(output)).to.contain("Delightful Header");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
|
||||||
|
const output = replacer(
|
||||||
|
MOCK_JSON,
|
||||||
|
{
|
||||||
|
type: PatchType.PARAGRAPH,
|
||||||
|
children: [new TextRun("sweet")],
|
||||||
|
},
|
||||||
|
"{{bold}}",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
text: "What a {{bold}} text!",
|
||||||
|
runs: [
|
||||||
|
{
|
||||||
|
text: "What a {{bold}} text!",
|
||||||
|
parts: [{ text: "What a {{bold}} text!", index: 1, start: 0, end: 21 }],
|
||||||
|
index: 0,
|
||||||
|
start: 0,
|
||||||
|
end: 21,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
index: 0,
|
||||||
|
path: [0, 0, 1],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{
|
||||||
|
file: {} as unknown as File,
|
||||||
|
viewWrapper: {
|
||||||
|
Relationships: {},
|
||||||
|
} as unknown as IViewWrapper,
|
||||||
|
stack: [],
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(JSON.stringify(output)).to.contain("sweet");
|
||||||
|
expect(output.elements![0].elements![1].elements).toMatchObject([
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:r",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:rPr",
|
||||||
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:t",
|
||||||
|
elements: [{ type: "text", text: "What a " }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:r",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:rPr",
|
||||||
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:t",
|
||||||
|
elements: [{ type: "text", text: "sweet" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:r",
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:rPr",
|
||||||
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "element",
|
||||||
|
name: "w:t",
|
||||||
|
elements: [{ type: "text", text: " text!" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it("should replace document type", () => {
|
it("should replace document type", () => {
|
||||||
const output = replacer(
|
const output = replacer(
|
||||||
MOCK_JSON,
|
MOCK_JSON,
|
||||||
|
@ -20,6 +20,7 @@ export const replacer = (
|
|||||||
patchText: string,
|
patchText: string,
|
||||||
renderedParagraphs: readonly IRenderedParagraphNode[],
|
renderedParagraphs: readonly IRenderedParagraphNode[],
|
||||||
context: IContext,
|
context: IContext,
|
||||||
|
keepOriginalStyles: boolean = false,
|
||||||
): Element => {
|
): Element => {
|
||||||
for (const renderedParagraph of renderedParagraphs) {
|
for (const renderedParagraph of renderedParagraphs) {
|
||||||
const textJson = patch.children
|
const textJson = patch.children
|
||||||
@ -47,9 +48,30 @@ export const replacer = (
|
|||||||
|
|
||||||
const index = findRunElementIndexWithToken(paragraphElement, SPLIT_TOKEN);
|
const index = findRunElementIndexWithToken(paragraphElement, SPLIT_TOKEN);
|
||||||
|
|
||||||
const { left, right } = splitRunElement(paragraphElement.elements![index], SPLIT_TOKEN);
|
const runElementToBeReplaced = paragraphElement.elements![index];
|
||||||
|
const { left, right } = splitRunElement(runElementToBeReplaced, SPLIT_TOKEN);
|
||||||
|
|
||||||
|
let newRunElements = textJson;
|
||||||
|
let patchedRightElement = right;
|
||||||
|
|
||||||
|
if (keepOriginalStyles) {
|
||||||
|
const runElementNonTextualElements = runElementToBeReplaced.elements!.filter(
|
||||||
|
(e) => e.type === "element" && e.name !== "w:t",
|
||||||
|
);
|
||||||
|
|
||||||
|
newRunElements = textJson.map((e) => ({
|
||||||
|
...e,
|
||||||
|
elements: [...runElementNonTextualElements, ...e.elements!],
|
||||||
|
}));
|
||||||
|
|
||||||
|
patchedRightElement = {
|
||||||
|
...right,
|
||||||
|
elements: [...runElementNonTextualElements, ...right.elements!],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line functional/immutable-data
|
// eslint-disable-next-line functional/immutable-data
|
||||||
paragraphElement.elements!.splice(index, 1, left, ...textJson, right);
|
paragraphElement.elements!.splice(index, 1, left, ...newRunElements, patchedRightElement);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user