Merge pull request #436 from zaunermax/master

Support footnotes on TextRun elements
This commit is contained in:
Dolan
2019-10-19 14:46:23 +01:00
committed by GitHub
3 changed files with 33 additions and 2 deletions

View File

@ -1,14 +1,20 @@
// Footnotes // Footnotes
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Packer, Paragraph } from "../build"; import { Document, Packer, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document();
doc.addSection({ 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("Test"));
doc.createFootnote(new Paragraph("My amazing reference")); doc.createFootnote(new Paragraph("My amazing reference"));

View File

@ -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 } } },
],
},
],
});
});
});
}); });

View File

@ -1,3 +1,4 @@
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { IRunOptions, Run } from "./run"; import { IRunOptions, Run } from "./run";
import { Text } from "./run-components/text"; import { Text } from "./run-components/text";
@ -16,4 +17,9 @@ export class TextRun extends Run {
super(options); super(options);
this.root.push(new Text(options.text)); this.root.push(new Text(options.text));
} }
public referenceFootnote(id: number): TextRun {
this.root.push(new FootnoteReferenceRun(id));
return this;
}
} }