diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts index 3ed1f4ea91..a41f84a3c8 100644 --- a/demo/17-footnotes.ts +++ b/demo/17-footnotes.ts @@ -3,7 +3,16 @@ import * as fs from "fs"; import { Document, FootnoteReferenceRun, Packer, Paragraph, TextRun } from "../build"; -const doc = new Document(); +const doc = new Document({ + footnotes: [ + new Paragraph("Foo"), + new Paragraph("Test"), + new Paragraph("My amazing reference"), + new Paragraph("Foo1"), + new Paragraph("Test1"), + new Paragraph("My amazing reference1"), + ], +}); doc.addSection({ children: [ @@ -23,9 +32,23 @@ doc.addSection({ ], }); -doc.createFootnote(new Paragraph("Foo")); -doc.createFootnote(new Paragraph("Test")); -doc.createFootnote(new Paragraph("My amazing reference")); +doc.addSection({ + children: [ + new Paragraph({ + children: [ + new TextRun({ + children: ["Hello", new FootnoteReferenceRun(4)], + }), + new TextRun({ + children: [" World!", new FootnoteReferenceRun(5)], + }), + ], + }), + new Paragraph({ + children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)], + }), + ], +}); Packer.toBuffer(doc).then((buffer) => { fs.writeFileSync("My Document.docx", buffer); diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index 8ac2130310..e807f0fd5f 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -2,6 +2,7 @@ import { XmlComponent } from "file/xml-components"; import { DocumentAttributes } from "../document/document-attributes"; import { INumberingOptions } from "../numbering"; +import { Paragraph } from "../paragraph"; import { IStylesOptions } from "../styles"; import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components"; @@ -16,6 +17,7 @@ export interface IPropertiesOptions { readonly externalStyles?: string; readonly styles?: IStylesOptions; readonly numbering?: INumberingOptions; + readonly footnotes?: Paragraph[]; } export class CoreProperties extends XmlComponent { diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index b82748ca54..e34feea1b6 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -149,12 +149,187 @@ describe("File", () => { }); describe("#createFootnote", () => { - it("should call the underlying document's createFootnote", () => { - const wrapper = new File(); - const spy = sinon.spy(wrapper.FootNotes, "createFootNote"); - wrapper.createFootnote(new Paragraph("")); + it("should create footnote", () => { + const wrapper = new File({ + footnotes: [new Paragraph("hello")], + }); - expect(spy.called).to.equal(true); + const tree = new Formatter().format(wrapper.FootNotes); + + expect(tree).to.deep.equal({ + "w:footnotes": [ + { + _attr: { + "mc:Ignorable": "w14 w15 wp14", + "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math", + "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006", + "xmlns:o": "urn:schemas-microsoft-com:office:office", + "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + "xmlns:v": "urn:schemas-microsoft-com:vml", + "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "xmlns:w10": "urn:schemas-microsoft-com:office:word", + "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml", + "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml", + "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml", + "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + }, + }, + { + "w:footnote": [ + { + _attr: { + "w:id": -1, + "w:type": "separator", + }, + }, + { + "w:p": [ + { + "w:pPr": [ + { + "w:spacing": { + _attr: { + "w:after": 0, + "w:line": 240, + "w:lineRule": "auto", + }, + }, + }, + ], + }, + { + "w:r": [ + { + "w:rPr": [ + { + "w:rStyle": { + _attr: { + "w:val": "FootnoteReference", + }, + }, + }, + ], + }, + { + "w:footnoteRef": {}, + }, + ], + }, + { + "w:r": [ + { + "w:separator": {}, + }, + ], + }, + ], + }, + ], + }, + { + "w:footnote": [ + { + _attr: { + "w:id": 0, + "w:type": "continuationSeparator", + }, + }, + { + "w:p": [ + { + "w:pPr": [ + { + "w:spacing": { + _attr: { + "w:after": 0, + "w:line": 240, + "w:lineRule": "auto", + }, + }, + }, + ], + }, + { + "w:r": [ + { + "w:rPr": [ + { + "w:rStyle": { + _attr: { + "w:val": "FootnoteReference", + }, + }, + }, + ], + }, + { + "w:footnoteRef": {}, + }, + ], + }, + { + "w:r": [ + { + "w:continuationSeparator": {}, + }, + ], + }, + ], + }, + ], + }, + { + "w:footnote": [ + { + _attr: { + "w:id": 1, + }, + }, + { + "w:p": [ + { + "w:r": [ + { + "w:rPr": [ + { + "w:rStyle": { + _attr: { + "w:val": "FootnoteReference", + }, + }, + }, + ], + }, + { + "w:footnoteRef": {}, + }, + ], + }, + { + "w:r": [ + { + "w:t": [ + { + _attr: { + "xml:space": "preserve", + }, + }, + "hello", + ], + }, + ], + }, + ], + }, + ], + }, + ], + }); }); }); }); diff --git a/src/file/file.ts b/src/file/file.ts index 4cb14d3e3d..f2bcad3610 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -67,6 +67,7 @@ export class File { creator: "Un-named", revision: "1", lastModifiedBy: "Un-named", + footnotes: [], }, fileProperties: IFileProperties = {}, sections: ISectionOptions[] = [], @@ -136,6 +137,12 @@ export class File { this.document.add(child); } } + + if (options.footnotes) { + for (const paragraph of options.footnotes) { + this.footNotes.createFootNote(paragraph); + } + } } public createHyperlink(link: string, text?: string): Hyperlink { @@ -191,10 +198,6 @@ export class File { } } - public createFootnote(paragraph: Paragraph): void { - this.footNotes.createFootNote(paragraph); - } - public verifyUpdateFields(): void { if (this.document.getTablesOfContents().length) { this.settings.addUpdateFields();