* feat: Migrate to new component authoring # Conflicts: # src/file/table-of-contents/table-of-contents.ts # Conflicts: # src/file/content-types/content-types.ts # src/file/paragraph/properties.ts # src/file/table/table-properties/table-properties.ts # Conflicts: # src/file/document/body/section-properties/section-properties.ts # src/file/paragraph/run/run.ts # src/file/table-of-contents/table-of-contents.ts # src/file/table/grid.ts # src/file/table/table-cell/table-cell-properties.ts # src/file/table/table.ts * Clarify public api * Add docs * Fix tests and linting * Fix readonly array * Fix spelling * Update ToC docs * Update WPS to use BuilderElement * Migrate components to use Builder element Add ESLint rules * Comment out the template demos * Ignore demo 90 until we fix schema validation * Disable demo validation * Disable doc * Disable validation for now --------- Co-authored-by: Dolan Miu <>
8.9 KiB
Contribution Guidelines
File Documentation
Every source file must include structured JSDoc documentation. This ensures discoverability and traceability back to the OOXML specification.
Module-Level JSDoc
Each file should begin with a module-level JSDoc block containing:
- A brief description of the module's purpose.
- A
Reference:link to the relevant documentation (e.g., officeopenxml.com, datypic.com, or the ECMA-376 standard). - The
@moduletag.
/**
* Header module for WordprocessingML documents.
*
* Headers are repeated at the top of each page in a section.
*
* Reference: http://officeopenxml.com/WPheaders.php
*
* @module
*/
XSD Schema References
Include the relevant XSD schema fragment in a ## XSD Schema section within the JSDoc of classes and factory functions. The ooxml-schemas/ directory in this repository contains the official ISO-IEC29500 OOXML XSD schemas and serves as the golden source of truth.
Key schemas:
| Schema | Purpose |
|---|---|
ooxml-schemas/ISO-IEC29500-4_2016/wml.xsd |
WordprocessingML (main document structure) |
ooxml-schemas/ISO-IEC29500-4_2016/dml-main.xsd |
DrawingML (images, shapes) |
ooxml-schemas/ISO-IEC29500-4_2016/shared-math.xsd |
Math equations |
ooxml-schemas/ISO-IEC29500-4_2016/vml-main.xsd |
VML (legacy shapes, textboxes) |
Always cross-reference these schemas when implementing or modifying XML generation:
/**
* Represents a table in a WordprocessingML document.
*
* Reference: http://officeopenxml.com/WPtable.php
*
* @publicApi
*
* ## XSD Schema
* ```xml
* <xsd:complexType name="CT_Tbl">
* <xsd:sequence>
* <xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
* <xsd:element name="tblPr" type="CT_TblPr"/>
* <xsd:element name="tblGrid" type="CT_TblGrid"/>
* <xsd:group ref="EG_ContentRowContent" minOccurs="0" maxOccurs="unbounded"/>
* </xsd:sequence>
* </xsd:complexType>
* ```
*/
Code Style and Linting
- Follow Prettier formatting standards. Consider using the Prettier VS Code extension.
- Follow all
ESLintrules configured in the project.
API Design Principles
Every feature should be designed from the consumer's perspective. Before submitting a contribution, verify that your API meets the following criteria:
- Ease of use — Can a developer use this feature with minimal friction?
- Documentation — Are the options, behavior, and examples clearly documented in JSDoc?
- Intuitiveness — Does the API behave as a developer would reasonably expect?
- Declarative style — Does it follow the project's declarative construction pattern?
- Developer experience — Is it pleasant to read and write?
Good Commit Names
Please write good commit messages when making a commit: https://chris.beams.io/posts/git-commit/
Do not:
c // What?
rtl // Adding acronyms without explaining anything else is not helpful
works! // Glad its working, but the message is not helpful
demo updated // Getting better, but capitalize the first letter
Unesesary coment removed // Make sure to use correct spelling
Do
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:
public float(tableFloatOptions: ITableFloatOptions): Table
Declarative API
All APIs must be declarative — no method chaining, mutation, or imperative construction patterns. This is a core design principle of the project. See: https://dzone.com/articles/why-declarative-coding-makes-you-a-better-programm
Do not:
const paragraph = doc.createParagraph();
const text = paragraph.createText();
text.contents = "Hello World";
Do:
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
},
],
});
Public API Elements (@publicApi)
User-facing elements (e.g., Document, Paragraph, Table, TextRun, Textbox) use the new ClassName(options) pattern and must be annotated with @publicApi in their JSDoc. These form the idiomatic surface of the library that end-users interact with:
/**
* @publicApi
*/
export class Textbox extends FileChild {
public constructor({ style, children, ...rest }: ITextboxOptions) {
// ...
}
}
Internal Elements (BuilderElement)
For internal XML construction — elements that are not part of the public API — use BuilderElement or createXyz() factory functions. This is less verbose while keeping the declarative style intact:
export const createVerticalAlign = (value: VerticalAlignValue): XmlComponent =>
new BuilderElement<{ readonly verticalAlign: VerticalAlignValue }>({
name: "w:vAlign",
attributes: {
verticalAlign: { key: "w:val", value },
},
});
BuilderElement provides typed attributes and optional children in a single declarative call, removing the need for boilerplate wrapper classes for simple XML elements.
Getters and Setters
Getters and Setters are done with a capital letter like so:
public get Level() {
...
}
This is the convention of this project. There is no performance advantage by doing this. It means we don't need to prefix all private variables with _:
Do not:
private get _level: string;
Do
private get level: string;
Types over interfaces
Using type aliases in TypeScript offers several advantages over interfaces:
-
Flexibility with Complex Types:
typesupports defining unions, intersections, and other complex type constructs thatinterfacescannot handle. For example:type StringOrNumber = string | number; type Combined = TypeA & TypeB; -
Support for Primitive Types:
typecan alias primitive types (e.g.,type ID = string), whileinterfacesare limited to object shapes. -
Tuple and Array Types:
typeallows defining tuples and specific array types easily (e.g.,type Point = [number, number]), whichinterfacescannot represent. -
Utility Types Compatibility:
typeworks seamlessly with TypeScript's utility types (e.g.,Partial<T>,Pick<T, K>), enabling more expressive type transformations. -
Functional Programming:
typeis ideal for functional programming patterns, such as defining function signatures or mapped types, due to its versatility. -
No Declaration Merging: Unlike
interfaces, type does not support declaration merging, which can prevent accidental type extensions and ensure predictable type definitions. -
Consistent Pattern: This project uses
typefor all type definitions, so usingtypefor all type definitions maintains consistency and readability across the codebase.
Detailed discussion: https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types
Do not:
interface IRelationshipFileInfo {
id: number;
target: string;
}
Do:
type RelationshipFileInfo = { id: number; target: string };
String enums vs type
To take full advantage of TypeScript's typing system, its best to use string enums:
Do not:
type WeaponType = "bow" | "sword" | "wand";
Do:
enum WeaponType = {
BOW = "bow",
SWORD = "sword",
WAND = "wand",
}
Spell correctly, in full and in American English
Do not:
readdy; // misspelling
perm; // abbreviation
conf; // abbreviation
cnty; // abbreviation
relationFile; // abbreviation
colour; // U.K. English
Do:
ready;
permission;
config;
country;
relationshipFile;
color;
Keep files small (within reason)
To minimize merge conflicts, reduce complexity, and improve readability, keep the files small.
Name files and folders with /foo-bar/kebab-case.ts
To be consistent and in-line with the project, name files like-this.ts.
https://stackoverflow.com/questions/7273316/what-is-the-javascript-filename-naming-convention
Testing
Please write a test of every file you make and suffix it with .spec.ts.
Here is a template of a test:
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
describe("ClassName", () => {
afterEach(() => {
// TODO
});
beforeEach(() => {
// TODO
});
describe("#methodName()", () => {
it("should ", () => {
// TODO
});
});
});