Skip to content

Add a map function to Paragraph and similar types #3330

@behrangsa

Description

@behrangsa

Context

This becomes handy when we want to transform a LaTeX, AsciiDoc, etc. document into docx.

Example:

// Parser
function parse(input: string) : Paragraph[] {
  // ...
  return [
    new Paragraph({
      children: "..."
    })
  ]
}

// Later
const paras = parse(`
  Hello

  World!
`);


const mapped = paras.flatMap(p => {
  return p.map({
    indent: { left: 720, right: 720 },
    spacing: { before: 240, after: 120 },
    alignment: AlignmentType.RIGHT,
  })
});

Right now Paragraphs are "immutable" in TypeScript and accomplishing this needs casting to any:

const result = parseToParagraphs(body).map((par) => {
  const runs = (par as any).root;
  return new Paragraph({
    indent: { left: 720, right: 720 },
    spacing: { before: 240, after: 120 },
    children: runs,
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions