{"id":19486221,"url":"https://github.com/textbook/docx-jsx","last_synced_at":"2025-04-25T18:31:54.287Z","repository":{"id":39227725,"uuid":"240713263","full_name":"textbook/docx-jsx","owner":"textbook","description":"docx is nice. JSX is nice. Both together is really nice.","archived":false,"fork":false,"pushed_at":"2023-01-07T04:32:51.000Z","size":2148,"stargazers_count":9,"open_issues_count":22,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T01:11:11.593Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/textbook.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-15T13:06:29.000Z","updated_at":"2025-03-27T17:54:53.000Z","dependencies_parsed_at":"2023-02-06T11:45:55.201Z","dependency_job_id":null,"html_url":"https://github.com/textbook/docx-jsx","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textbook%2Fdocx-jsx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textbook%2Fdocx-jsx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textbook%2Fdocx-jsx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/textbook%2Fdocx-jsx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/textbook","download_url":"https://codeload.github.com/textbook/docx-jsx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250872212,"owners_count":21500775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T20:35:42.099Z","updated_at":"2025-04-25T18:31:53.415Z","avatar_url":"https://github.com/textbook.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# docx-jsx\n\n[![License](https://img.shields.io/github/license/textbook/docx-jsx.svg)](https://github.com/textbook/docx-jsx/blob/master/LICENSE)\n[![Build Status](https://github.com/textbook/docx-jsx/actions/workflows/push.yml/badge.svg)](https://github.com/textbook/docx-jsx/actions/workflows/push.yml)\n[![NPM Version](https://img.shields.io/npm/v/docx-jsx.svg)](https://www.npmjs.com/package/docx-jsx)\n\n[docx] is nice. [JSX] is nice. Both together is _really_ nice.\n\n## What is this?\n\n[docx] is a great package for creating .docx files, but the API it exposes for\nbuilding the documents is a little awkward, particularly when you're used to\nputting hierarchical elements together with XML-like syntax.\n\nThis package allows you to write:\n\n```jsx\n/** @jsx createElement */\nimport { createElement, Document, Paragraph, Section, TextRun } from \"docx-jsx\";\n\nconst createDocument = () =\u003e (\n  \u003cDocument\u003e\n    \u003cSection\u003e\n      \u003cParagraph\u003e\n        \u003cTextRun\u003eHello World\u003c/TextRun\u003e\n        \u003cTextRun bold={true}\u003eFoo Bar\u003c/TextRun\u003e\n        \u003cTextRun bold={true} text={\"\\tGithub is the best\"}\u003e\u003c/TextRun\u003e\n      \u003c/Paragraph\u003e\n    \u003c/Section\u003e\n  \u003c/Document\u003e\n);\n```\n\nInstead of the original example:\n\n```javascript\nimport { Document, Paragraph, TextRun } from \"docx\";\n\nconst createDocument = () =\u003e {\n  // Create document\n  const doc = new Document();\n\n  // Documents contain sections, you can have multiple sections per document, go here to learn more about sections\n  // This simple example will only contain one section\n  doc.addSection({\n    properties: {},\n    children: [\n      new Paragraph({\n        children: [\n          new TextRun(\"Hello World\"),\n          new TextRun({\n            text: \"Foo Bar\",\n            bold: true\n          }),\n          new TextRun({\n            text: \"\\tGithub is the best\",\n            bold: true\n          })\n        ]\n      })\n    ]\n  });\n\n  return doc;\n};\n```\n\n## How do I use it?\n\nInstall `docx-jsx` and `docx` (which is a peer dependency):\n\n```sh\nnpm install docx@5 docx-jsx\n```\n\nThe example above uses `/** @jsx createElement */` to get [the Babel JSX plugin]\nto use `docx-jsx`'s `createElement` instead of the default\n`React.createElement`. If you are using some other method to process JSX,\nconsult the appropriate documentation.\n\nYou can import most of the `docx` elements, like `Document` and `TextRun`, from\neither `docx` _or_ `docx-jsx`. **However** note that:\n\n- `Section` does not exist in the `docx` package; and\n- `TabStop` does exist, but is overridden in this package for functionality\n  reasons;\n\nso you _must_ import them from `docx-jsx` for correct behaviour.\n\n### API improvements\n\nIn general, you can translate the `docx` API directly to JSX. However, to make\nthe element structure a bit neater, the following elements can be passed as\nchildren:\n\n- `Section`s can be children of a `Document`, rather than calling `addSection`\n\n- `TableRow` elements can be children of a `Table`, rather than passing them as\n  the `rows` property\n\n- `Header` and `Footer` elements can be children of a `Section`, rather than\n  setting the `default` in the `headers` or `footers` prop\n\n- `TabStop`s can be children of a `Paragraph`, rather than using the `tabStops`\n  prop directly\n\n### Gotchas\n\nSome special characters don't seem to be handled very well by JSX. If you need\ne.g. a tab character in a `TextRun` (see example above), use the `text` prop\nexpression form, rather than the string literal form or passing the text as a\nchild of the element:\n\n```jsx\n\u003cTextRun\u003e\\tDon't do this\u003c/TextRun\u003e\n\u003cTextRun text=\"\\tor this\" /\u003e\n\u003cTextRun text={'\\tdo this instead'} /\u003e\n```\n\n### ESLint\n\nYou will need to disable the rule [`react/style-prop-object`][1] for files\nincluding docx JSX, where the style prop is a string. You can do this by adding\n`/* eslint-disable react/style-prop-object */` to the top of each file, for\nexample.\n\n## How's it going?\n\nThis is still in pre-release phase, I'm working through the examples in the docs\none by one...\n\n- [x] Get initial example working\n- [x] `Document` properties examples\n- [x] `tabStops` example\n- [ ] Work through [demos]\n  - [x] 1. Basic\n  - [x] 2. Declarative styles\n  - [x] 3. Numbering and bullet points\n  - [x] 4. Basic table\n  - [x] 6. Page borders\n  - [x] 7. Landscape\n  - [x] 8. Header \u0026 footer\n  - [x] 14. Page numbers\n- [ ] Handle fragments\n\n[1]:\n  https://github.com/yannickcr/eslint-plugin-react/blob/HEAD/docs/rules/style-prop-object.md\n[demos]: https://github.com/dolanmiu/docx/blob/master/demo\n[docx]: https://docx.js.org/#/\n[jsx]: https://reactjs.org/docs/introducing-jsx.html\n[the babel jsx plugin]:\n  https://babeljs.io/docs/en/babel-plugin-transform-react-jsx\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextbook%2Fdocx-jsx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftextbook%2Fdocx-jsx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftextbook%2Fdocx-jsx/lists"}