Using const rather than var and let, and using ts rather than js examples

This commit is contained in:
Dolan Miu
2019-08-06 17:51:13 +01:00
parent 0f1f398e6d
commit 820e5edc1e
13 changed files with 87 additions and 87 deletions

View File

@ -18,8 +18,8 @@ npm install --save docx
Then you can `require` or `import` as usual:
```js
let docx = require("docx");
```ts
const docx = require("docx");
```
```ts
@ -30,7 +30,7 @@ import { ... } from "docx";
## Basic Usage
```js
```ts
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";

View File

@ -2,7 +2,7 @@
* Include documentation reference(s) at the top of each file:
```js
```ts
// http://officeopenxml.com/WPdocument.php
```
@ -44,7 +44,7 @@ Try to make method parameters of the outside API accept primitives, or `json` ob
This is so that:
1. Imports are much cleaner for the end user, no need for:
```js
```ts
import { ChildComponent } from "./my-feature/sub-component/deeper/.../my-deep.component";
```
@ -55,7 +55,7 @@ This is so that:
`TableFloatProperties` is a class. The outside world would have to `new` up the object, and inject it in like so:
```js
```ts
public float(tableFloatProperties: TableFloatProperties): Table
```
@ -67,7 +67,7 @@ This is so that:
`ITableFloatOptions` is an interface for a JSON of primitives. The end user would need to pass in a json object and not need to worry about the internals:
```js
```ts
public float(tableFloatOptions: ITableFloatOptions): Table
```
@ -81,7 +81,7 @@ This is just a guideline, and the rules can sometimes be broken.
* Use `create` if the method `new`'s up an element inside:
```js
```ts
public createParagraph() {
const paragraph = new Paragraph();
this.root.push(paragraph);
@ -91,7 +91,7 @@ This is just a guideline, and the rules can sometimes be broken.
* Use `add` if you add the element into the method as a parameter.
*Note:* This may look like its breaking the previous guideline, but it has semantically different meanings. The previous one is using data to construct an object, whereas this one is simply adding elements into the document:
```js
```ts
public add(paragraph: Paragraph) {
this.root.push(paragraph);
}
@ -101,7 +101,7 @@ This is just a guideline, and the rules can sometimes be broken.
Getters and Setters are done with a capital letter like so:
```js
```ts
public get Level() {
...
}
@ -111,13 +111,13 @@ There is no performance advantage by doing this. It means we don't need to prefi
**Do not:**
```js
```ts
private get _level: string;
```
**Do**
```js
```ts
private get level: string;
```
@ -158,13 +158,13 @@ Do not use `type`, but rather use `Interfaces`. `type` cannot be extended, and a
**Do not:**
```js
```ts
type RelationshipFileInfo = { id: number, target: string };
```
**Do:**
```js
```ts
interface IRelationshipFileInfo {
id: number;
target: string;
@ -177,13 +177,13 @@ To take full advantage of TypeScript's typing system, its best to use `string en
**Do not:**
```js
```ts
type WeaponType = "bow" | "sword" | "wand";
```
**Do:**
```js
```ts
enum WeaponType = {
BOW = "bow",
SWORD = "sword",
@ -196,7 +196,7 @@ enum WeaponType = {
I am not sure where these habits in software development come from, but I do not believe it is beneficial:
**Do not:**
```js
```ts
readdy // misspelling
perm // abbreviation
conf // abbreviation
@ -206,7 +206,7 @@ colour // U.K. English
```
**Do:**
```js
```ts
ready
permission
config
@ -231,7 +231,7 @@ Please write a test of every file you make and suffix it with `.spec.ts`.
Here is a template of a test:
```js
```ts
import { assert } from "chai";
describe("ClassName", () => {

View File

@ -4,12 +4,12 @@
To make a bullet point, simply make a paragraph into a bullet point:
```js
var text = new docx.TextRun("Bullet points");
var paragraph = new docx.Paragraph(text).bullet();
```ts
const text = new docx.TextRun("Bullet points");
const paragraph = new docx.Paragraph(text).bullet();
var text2 = new docx.TextRun("Are awesome");
var paragraph2 = new docx.Paragraph(text2).bullet();
const text2 = new docx.TextRun("Are awesome");
const paragraph2 = new docx.Paragraph(text2).bullet();
doc.add(paragraph);
doc.add(paragraph2);

View File

@ -4,7 +4,7 @@
To create a new document, it is very easy:
```js
```ts
const doc = new docx.Document();
```
@ -12,7 +12,7 @@ const doc = new docx.Document();
You can add properties to the Word document by specifying options, for example:
```js
```ts
const doc = new docx.Document({
creator: "Dolan Miu",
description: "My extremely interesting document",

View File

@ -6,21 +6,21 @@
Creating Headers and footers is simple. Access the `Header` and `Footer` by doing so like this:
```js
```ts
doc.Header;
doc.Footer;
```
You can call the same methods as you would with a `File`:
```js
```ts
doc.Header.createParagraph("Header text");
doc.Footer.createParagraph("Footer text");
```
Even add images:
```js
```ts
doc.Header.createImage([BUFFER_OF_YOUR_IMAGE]);
doc.Footer.createImage([BUFFER_OF_YOUR_IMAGE]);
```
@ -33,7 +33,7 @@ Also all the supported section properties are implemented according to: http://o
### Example
```js
```ts
const header = this.document.createHeader();
const footer = this.document.createFooter();

View File

@ -67,7 +67,7 @@ First you need to create a new numbering container class and use it to
create your abstract numbering style, define your levels, and create
your concrete numbering style:
```js
```ts
const numbering = new docx.Numbering();
const abstractNum = numbering.createAbstractNumbering();
@ -81,7 +81,7 @@ const concrete = numbering.createConcreteNumbering(abstractNum);
You can then apply your concrete style to paragraphs using the
`setNumbering` method:
```js
```ts
topLevelP.setNumbering(concrete, 0);
subP.setNumbering(concrete, 1);
subSubP.setNumbering(concrete, 2);
@ -90,7 +90,7 @@ subSubP.setNumbering(concrete, 2);
Finally, you need to let your exporter know about your numbering
styles when you're ready to render the document:
```js
```ts
const packer = new Packer(doc, undefined, undefined, numbering);
packer.pack(myOutput);
```

View File

@ -10,7 +10,7 @@ Packers in `version 4` and above are now one single `Packer`. It works in both a
This will return a NodeJS `Buffer`. If this is used in the browser, it will return a `UInt8Array` instead.
```js
```ts
const packer = new docx.Packer();
packer.toBuffer(doc).then((buffer) => {
@ -20,7 +20,7 @@ packer.toBuffer(doc).then((buffer) => {
### Export as a `base64` string
```js
```ts
const packer = new docx.Packer();
packer.toBase64String(doc).then((string) => {
@ -32,7 +32,7 @@ packer.toBase64String(doc).then((string) => {
This is useful if you want to send it as an downloadable in a browser environment.
```js
```ts
const packer = new docx.Packer();
packer.toBlob(doc).then((blob) => {
@ -45,7 +45,7 @@ packer.toBlob(doc).then((blob) => {
### File System Packer
```js
```ts
const docx = require("docx");
const doc = new docx.Document();
@ -56,7 +56,7 @@ exporter.pack("My Document");
### Buffer Packer
```js
```ts
const docx = require("docx");
const doc = new docx.Document();
@ -68,7 +68,7 @@ const buffer = exporter.pack();
Creates a `node` `Readable` stream
```js
```ts
const docx = require("docx");
const doc = new docx.Document();
@ -88,7 +88,7 @@ I used the express exporter in my [website](http://www.dolan.bio).
The recommended way is to use the `StreamPacker` and handle the `express` magic outside of the library:
```js
```ts
const docx = require("docx");
const doc = new docx.Document();
@ -107,7 +107,7 @@ where `res` is the response object obtained through the Express router. It is th
You can export your word document as a PDF file like so:
```js
```ts
const exporter = new docx.LocalPacker(doc);
exporter.packPdf("My Document");

View File

@ -8,33 +8,33 @@ You can create `Paragraphs` in the following ways:
### Shorthand
```js
```ts
import { Paragraph } from "docx";
var paragraph = new Paragraph("Short hand Hello World");
const paragraph = new Paragraph("Short hand Hello World");
```
### Children Method
This method is useful for adding different `text` with different styles or adding `images` inline.
```js
var paragraph = new Paragraph({
```ts
const paragraph = new Paragraph({
children: [new TextRun("Lorem Ipsum Foo Bar"), new TextRun("Hello World")],
});
```
### Explicit
```js
var paragraph = new Paragraph({
```ts
const paragraph = new Paragraph({
text: "Short hand notation for adding text.",
});
```
After you create the paragraph, you must add the paragraph into the `document's section`. Learn more about `sections` here:
```js
```ts
doc.addSection({
children: [paragraph],
});
@ -42,7 +42,7 @@ doc.addSection({
Or the preferred convension, define the paragraph inside the section and remove the usage of variables:
```js
```ts
doc.addSection({
children: [
new Paragraph({
@ -186,7 +186,7 @@ To create styles, please refer to the styling Wiki: https://github.com/dolanmiu/
### Headings and titles
```js
```ts
import { HeadingLevel, Paragraph } from "docx";
const paragraph = new Paragraph({
@ -201,7 +201,7 @@ To change the text alignment of a paragraph, add an `AlignmentType` option on th
**Example:**
```js
```ts
const paragraph = new Paragraph({
text: "Hello World",
heading: HeadingLevel.HEADING_1,
@ -231,8 +231,8 @@ The result is:
To add a thematic break in the `Paragraph`:
```js
var paragraph = new docx.Paragraph("Amazing Heading");
```ts
const paragraph = new docx.Paragraph("Amazing Heading");
const paragraph = new Paragraph({
text: "Amazing Heading",
heading: HeadingLevel.HEADING_1,

View File

@ -2,7 +2,7 @@
## Example
```js
```ts
const para = new Paragraph("To whom it may concern:").heading2().center();
const name = new TextRun("Name:")
@ -56,7 +56,7 @@ Unlike CSS, less specific rules don't _necessarily_ override parent rules. The r
This is the type of formatting that your uncle uses when he types out documents: _N ... a ... m ... e ... :_ Then he grabs the mouse, highlights _Name:_ and moves over to the **B** for bold. This manner of formatting results in markup that is similar to writing `<span style="bold: true">Name:</span>` if you were typing out HTML. DOCX (the format) allows you to specify this for any of the four types of items. `docx` (the library) only supports this type of formatting for paragraphs and characters, using a _fluent_ api. Thus you could do:
```js
```ts
const name = new TextRun("Name:")
.bold()
.font("Calibri")
@ -65,7 +65,7 @@ const name = new TextRun("Name:")
Or for paragraph formatting:
```js
```ts
const para = new Paragraph("To whom it may concern:").heading2().center();
```
@ -76,12 +76,12 @@ DOCX files contain a styles section separate from the main content, much like ho
There are three parts to using custom styles with `docx`:
1. Create a container object for the style definitions:
```js
```ts
const myStyles = new docx.Styles();
```
2. Define your custom styles, similar to the way you would format a paragraph or run
```js
```ts
// The first argument is an ID you use to apply the style to paragraphs
// The second argument is a human-friendly name to show in the UI
myStyles
@ -106,7 +106,7 @@ There are three parts to using custom styles with `docx`:
3. When you generate your document, make sure to pass the `styles` container to the `Packer`:
```js
```ts
const packer = new Packer(doc, myStyles);
packer.pack(myOutStream);
```

View File

@ -24,7 +24,7 @@
Read the styles using `fs`, and put it into the `Document` object in the constructor:
```js
```ts
const styles = fs.readFileSync("./styles.xml", "utf-8");
const doc = new docx.Document({
title: "Title",
@ -34,10 +34,10 @@ const doc = new docx.Document({
You can use paragraphs, `heading1()`, `heading2()` etc and it will be styled according to your `styles.xml` created earlier. You can even use your new style you made by calling the `style` method:
```js
```ts
doc.createParagraph("Cool Heading Text").heading1();
let paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
const paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
paragraph.style("Cool New Style");
doc.add(paragraph);

View File

@ -10,45 +10,45 @@ Simply call the relevant methods on the paragraph listed below. Then just add a
## Example
```js
var paragraph = new docx.Paragraph().maxRightTabStop();
var leftText = new docx.TextRun("Hey everyone").bold();
var rightText = new docx.TextRun("11th November 2015").tab();
```ts
const paragraph = new docx.Paragraph().maxRightTabStop();
const leftText = new docx.TextRun("Hey everyone").bold();
const rightText = new docx.TextRun("11th November 2015").tab();
paragraph.addRun(leftText);
paragraph.addRun(rightText);
```
The example above will create a left aligned text, and a right aligned text on the same line. The laymans approach to this problem would be to either use text boxes or tables. YUK!
```js
var paragraph = new docx.Paragraph();
```ts
const paragraph = new docx.Paragraph();
paragraph.maxRightTabStop();
paragraph.leftTabStop(1000);
var text = new docx.TextRun("Second tab stop here I come!").tab().tab();
const text = new docx.TextRun("Second tab stop here I come!").tab().tab();
paragraph.addRun(text);
```
The above shows the use of two tab stops, and how to select/use it.
## Left Tab Stop
```js
```ts
paragraph.leftTabStop(2268);
```
2268 is the distance from the left side.
## Center Tab Stop
```js
```ts
paragraph.centerTabStop(2268);
```
2268 is the distance from the left side.
## Right Tab Stop
```js
```ts
paragraph.rightTabStop(2268);
```
2268 is the distance from the left side.
## Max Right Tab Stop
```js
```ts
paragraph.maxRightTabStop();
```
This will create a tab stop on the very edge of the right hand side. Handy for right aligning and left aligning text on the same line.

View File

@ -12,7 +12,7 @@ The complete documentation can be found [here](https://www.ecma-international.or
All you need to do is create a `TableOfContents` object and assign it to the document.
```js
```ts
const toc = new TableOfContents("Summary", {
hyperlink: true,
headingStyleRange: "1-5",
@ -47,7 +47,7 @@ Here is the list of all options that you can use to generate your tables of cont
## Examples
```js
```ts
// Let's define the options for generate a TOC for heading 1-5 and MySpectacularStyle,
// making the entries be hyperlinks for the paragraph
const toc = new TableOfContents("Summary", {

View File

@ -2,7 +2,7 @@
You can add multiple `text runs` in `Paragraphs`. This is the most verbose way of writing a `Paragraph` but it is also the most flexible:
```js
```ts
import { Paragraph, Text } from "docx";
const paragraph = new Paragraph({
@ -20,55 +20,55 @@ More info [here](https://english.stackexchange.com/questions/97081/what-is-the-t
### Bold
```js
```ts
text.bold();
```
### Italics
```js
```ts
text.italics();
```
### Underline
```js
```ts
text.underline();
```
### Strike through
```js
```ts
text.strike();
```
### Double strike through
```js
```ts
text.doubleStrike();
```
### Superscript
```js
```ts
text.superScript();
```
### Subscript
```js
```ts
text.subScript();
```
### All Capitals
```js
```ts
text.allCaps();
```
### Small Capitals
```js
```ts
text.smallCaps();
```
@ -76,7 +76,7 @@ text.smallCaps();
Sometimes you would want to put text underneath another line of text but inside the same paragraph.
```js
```ts
text.break();
```
@ -84,6 +84,6 @@ text.break();
What if you want to create a paragraph which is **_bold_** and **_italic_**?
```js
```ts
paragraph.bold().italics();
```