From ddb34e6a4610ed80f12c202dfb4a73d422d44543 Mon Sep 17 00:00:00 2001 From: Maximilian Zauner Date: Fri, 18 Oct 2019 13:33:47 +0200 Subject: [PATCH 1/2] feat(textRun): implemented footnote references on the text run element --- demo/17-footnotes.ts | 10 ++++++++-- src/file/paragraph/run/text-run.ts | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/demo/17-footnotes.ts b/demo/17-footnotes.ts index 3920d0dce8..bf3f11eb91 100644 --- a/demo/17-footnotes.ts +++ b/demo/17-footnotes.ts @@ -1,14 +1,20 @@ // Footnotes // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Document, Packer, Paragraph } from "../build"; +import { Document, Packer, Paragraph, TextRun } from "../build"; const doc = new Document(); doc.addSection({ - children: [new Paragraph("Hello World").referenceFootnote(1), new Paragraph("Hello World").referenceFootnote(2)], + children: [ + new Paragraph({ + children: [new TextRun("Hello").referenceFootnote(1), new TextRun(" World!").referenceFootnote(2)], + }), + new Paragraph("Hello World").referenceFootnote(3), + ], }); +doc.createFootnote(new Paragraph("Foo")); doc.createFootnote(new Paragraph("Test")); doc.createFootnote(new Paragraph("My amazing reference")); diff --git a/src/file/paragraph/run/text-run.ts b/src/file/paragraph/run/text-run.ts index eeacf2dfd5..2bbf1bae75 100644 --- a/src/file/paragraph/run/text-run.ts +++ b/src/file/paragraph/run/text-run.ts @@ -1,3 +1,4 @@ +import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; import { IRunOptions, Run } from "./run"; import { Text } from "./run-components/text"; @@ -16,4 +17,9 @@ export class TextRun extends Run { super(options); this.root.push(new Text(options.text)); } + + public referenceFootnote(id: number): TextRun { + this.root.push(new FootnoteReferenceRun(id)); + return this; + } } From c3c29bb119f0d5ac3cc3569ef511ca8ca1e4cb6b Mon Sep 17 00:00:00 2001 From: Maximilian Zauner Date: Fri, 18 Oct 2019 14:09:33 +0200 Subject: [PATCH 2/2] fix(test): added unit test for TextRun --- src/file/paragraph/run/text-run.spec.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/file/paragraph/run/text-run.spec.ts b/src/file/paragraph/run/text-run.spec.ts index 077d6300f0..ae8e5bd9b4 100644 --- a/src/file/paragraph/run/text-run.spec.ts +++ b/src/file/paragraph/run/text-run.spec.ts @@ -16,4 +16,23 @@ describe("TextRun", () => { }); }); }); + + describe("#referenceFootnote()", () => { + it("should add a valid footnote reference", () => { + run = new TextRun("test"); + run.referenceFootnote(1); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { "w:t": [{ _attr: { "xml:space": "preserve" } }, "test"] }, + { + "w:r": [ + { "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "FootnoteReference" } } }] }, + { "w:footnoteReference": { _attr: { "w:id": 1 } } }, + ], + }, + ], + }); + }); + }); });