├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── ci.yml │ └── static.yml ├── .gitignore ├── .prettierrc ├── .tool-versions ├── CONTRIBUTING.md ├── Dockerfile ├── LICENSE ├── README.md ├── build ├── __tests__ │ ├── main.test.js │ └── main.test.js.map ├── cli.js ├── cli.js.map ├── index.js ├── index.js.map ├── main.js ├── main.js.map ├── server.js ├── server.js.map ├── web.js └── web.js.map ├── dist ├── index.html ├── main.js ├── main.js.LICENSE.txt ├── privacy │ └── index.html └── terms │ └── index.html ├── jest.config.js ├── package-lock.json ├── package.json ├── src ├── __fixtures__ │ ├── comma after bold.docx │ ├── em.docx │ ├── file with space.docx │ ├── h1.docx │ ├── h2.docx │ ├── list-with-links.docx │ ├── multiple-headings.docx │ ├── nested-ol.docx │ ├── nested-ul.docx │ ├── ol.docx │ ├── p.docx │ ├── small-medium-large.docx │ ├── strong.docx │ ├── table.docx │ ├── text after bold.docx │ └── ul.docx ├── __tests__ │ └── main.test.ts ├── cli.ts ├── index.ts ├── main.ts └── server.ts ├── tsconfig.json ├── tsconfig.release.json └── webpack.config.cjs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | 9 | [*.md] 10 | insert_final_newline = false 11 | trim_trailing_whitespace = false 12 | 13 | [*.{js,json,ts,mts,yml,yaml}] 14 | indent_size = 2 15 | indent_style = space 16 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | /**/*.js 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "es6": true, 5 | "node": true 6 | }, 7 | "parser": "@typescript-eslint/parser", 8 | "parserOptions": { 9 | "project": "tsconfig.json", 10 | "sourceType": "module", 11 | "ecmaVersion": 2020 12 | }, 13 | "plugins": ["@typescript-eslint", "jest"], 14 | "extends": [ 15 | "eslint:recommended", 16 | "plugin:@typescript-eslint/recommended", 17 | "plugin:jest/recommended", 18 | "prettier" 19 | ], 20 | "rules": { 21 | "@typescript-eslint/prefer-ts-expect-error": "warn", 22 | "@typescript-eslint/ban-ts-comment": "warn" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [jsynowiec] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: 'npm' 9 | directory: '/' 10 | schedule: 11 | interval: 'weekly' 12 | - package-ecosystem: 'github-actions' 13 | directory: '/' 14 | schedule: 15 | interval: 'weekly' 16 | - package-ecosystem: 'docker' 17 | directory: '/' 18 | schedule: 19 | interval: 'weekly' 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: actions/setup-node@v4 12 | with: 13 | node-version: '20.x' 14 | cache: 'npm' 15 | - run: npm install 16 | - run: npm run test 17 | - run: npm run lint 18 | - run: npm run build 19 | - run: npm run build:web 20 | -------------------------------------------------------------------------------- /.github/workflows/static.yml: -------------------------------------------------------------------------------- 1 | # Simple workflow for deploying static content to GitHub Pages 2 | name: Deploy static content to Pages 3 | 4 | on: 5 | # Runs on pushes targeting the default branch 6 | push: 7 | branches: ['main'] 8 | 9 | # Allows you to run this workflow manually from the Actions tab 10 | workflow_dispatch: 11 | 12 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages 13 | permissions: 14 | contents: read 15 | pages: write 16 | id-token: write 17 | 18 | # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. 19 | # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. 20 | concurrency: 21 | group: 'pages' 22 | cancel-in-progress: false 23 | 24 | jobs: 25 | # Single deploy job since we're just deploying 26 | deploy: 27 | environment: 28 | name: github-pages 29 | url: ${{ steps.deployment.outputs.page_url }} 30 | runs-on: ubuntu-latest 31 | steps: 32 | - name: Checkout 33 | uses: actions/checkout@v4 34 | - name: Setup Pages 35 | uses: actions/configure-pages@v5 36 | - name: Upload artifact 37 | uses: actions/upload-pages-artifact@v3 38 | with: 39 | # Upload entire repository 40 | path: './dist' 41 | - name: Deploy to GitHub Pages 42 | id: deployment 43 | uses: actions/deploy-pages@v4 44 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Dependencies 7 | node_modules/ 8 | 9 | # Coverage 10 | coverage 11 | 12 | # VS Code 13 | .vscode 14 | !.vscode/tasks.js 15 | 16 | # JetBrains IDEs 17 | .idea/ 18 | 19 | # Optional npm cache directory 20 | .npm 21 | 22 | # Optional eslint cache 23 | .eslintcache 24 | 25 | # Misc 26 | .DS_Store -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all", 4 | "overrides": [ 5 | { 6 | "files": ["*.ts", "*.mts"], 7 | "options": { 8 | "parser": "typescript" 9 | } 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | nodejs 20.10.0 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Word To Markdown 2 | 3 | Hi there! We're thrilled that you'd like to contribute to Word To Markdown. Your help is essential for keeping it great. 4 | 5 | Word To Markdown is an open source project supported by the efforts of an entire community and built one contribution at a time by users like you. We'd love for you to get involved. Whatever your level of skill or however much time you can give, your contribution is greatly appreciated. There are many ways to contribute, from writing tutorials or blog posts, improving the documentation, submitting bug reports and feature requests, helping other users by commenting on issues, or writing code which can be incorporated into Word To Markdown itself. 6 | 7 | Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue, assessing changes, and helping you finalize your pull requests. 8 | 9 | 10 | 11 | ## How to report a bug 12 | 13 | Think you found a bug? Please check [the list of open issues](https://github.com/benbalter/word-to-markdown-js/issues) to see if your bug has already been reported. If it hasn't please [submit a new issue](https://github.com/benbalter/word-to-markdown-js/issues/new). 14 | 15 | Here are a few tips for writing *great* bug reports: 16 | 17 | * Describe the specific problem (e.g., "widget doesn't turn clockwise" versus "getting an error") 18 | * Include the steps to reproduce the bug, what you expected to happen, and what happened instead 19 | * Check that you are using the latest version of the project and its dependencies 20 | * Include what version of the project your using, as well as any relevant dependencies 21 | * Only include one bug per issue. If you have discovered two bugs, please file two issues 22 | * Include screenshots or screencasts whenever possible 23 | * Even if you don't know how to fix the bug, including a failing test may help others track it down 24 | 25 | **If you find a security vulnerability, do not open an issue. Please email ben@balter.com instead.** 26 | 27 | ## How to suggest a feature or enhancement 28 | 29 | If you find yourself wishing for a feature that doesn't exist in Word To Markdown, you are probably not alone. There are bound to be others out there with similar needs. Many of the features that Word To Markdown has today have been added because our users saw the need. 30 | 31 | Feature requests are welcome. But take a moment to find out whether your idea fits with the scope and goals of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Please provide as much detail and context as possible, including describing the problem you're trying to solve. 32 | 33 | [Open an issue](https://github.com/benbalter/word-to-markdown-js/issues/new) which describes the feature you would like to see, why you want it, how it should work, etc. 34 | 35 | ## What should I do if my document doesn't convert correctly 36 | 37 | 1. Strip the Word Document down to isolate the troublesome part (e.g., remove the parts that are converting correctly) 38 | 2. Upload the `.doc` or `.docx` file to someplace like DropBox 39 | 3. [Open a new issue](https://github.com/benbalter/word-to-markdown-js/issues/new) describing the particular problem. Be sure to include a link to the uploaded file. 40 | 41 | A few tips for reporting conversion issues: 42 | 43 | * Explain the expected output, actual output, and steps to reproduce 44 | * Include screenshots of the expected and actual output 45 | * Issues should be limited to one conversation error and describe it in detail 46 | * Issues that report "My document won't convert" or "I have these three problems" are less likely to be resolved quickly 47 | * Be sure to include what version of Microsoft Word was used to create the document 48 | 49 | 50 | ## Your first contribution 51 | 52 | We'd love for you to contribute to the project. Unsure where to begin contributing to Word To Markdown? You can start by looking through these "good first issue" and "help wanted" issues: 53 | 54 | * [Good first issues](https://github.com/benbalter/word-to-markdown-js/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) - issues which should only require a few lines of code and a test or two 55 | * [Help wanted issues](https://github.com/benbalter/word-to-markdown-js/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) - issues which may be a bit more involved, but are specifically seeking community contributions 56 | 57 | *p.s. Feel free to ask for help; everyone is a beginner at first* :smiley_cat: 58 | 59 | ## How to propose changes 60 | 61 | Here's a few general guidelines for proposing changes: 62 | 63 | * If you are changing any user-facing functionality, please be sure to update the documentation 64 | * If you are adding a new behavior or changing an existing behavior, please be sure to update the corresponding test(s) 65 | * Each pull request should implement **one** feature or bug fix. If you want to add or fix more than one thing, submit more than one pull request 66 | * Do not commit changes to files that are irrelevant to your feature or bug fix 67 | * Don't bump the version number in your pull request (it will be bumped prior to release) 68 | * Write [a good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) 69 | 70 | At a high level, [the process for proposing changes](https://guides.github.com/introduction/flow/) is: 71 | 72 | 1. [Fork](https://github.com/benbalter/word-to-markdown-js/fork) and clone the project 73 | 2. Configure and install the dependencies: `script/bootstrap` 74 | 3. Make sure the tests pass on your machine: `script/cibuild` 75 | 4. Create a descriptively named branch: `git checkout -b my-branch-name` 76 | 5. Make your change, add tests and documentation, and make sure the tests still pass 77 | 6. Push to your fork and [submit a pull request](https://github.com/benbalter/word-to-markdown-js/compare) describing your change 78 | 7. Pat your self on the back and wait for your pull request to be reviewed and merged 79 | 80 | **Interesting in submitting your first Pull Request?** It's easy! You can learn how from this *free* series [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github) 81 | 82 | ## Bootstrapping your local development environment 83 | 84 | `script/bootstrap` 85 | 86 | ## Running tests 87 | 88 | `script/cibuild` 89 | 90 | ## Code of conduct 91 | 92 | This project is governed by [the Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. 93 | 94 | ## Additional Resources 95 | 96 | * [Contributing to Open Source on GitHub](https://guides.github.com/activities/contributing-to-open-source/) 97 | * [Using Pull Requests](https://help.github.com/articles/using-pull-requests/) 98 | * [GitHub Help](https://help.github.com) -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:20 2 | 3 | ENV PORT=80 4 | ENV NODE_ENV=production 5 | 6 | EXPOSE ${PORT} 7 | 8 | WORKDIR /app 9 | 10 | COPY package.json package-lock.json /app/ 11 | 12 | RUN npm install 13 | 14 | COPY build /app/build/ 15 | 16 | CMD ["node", "build/src/server.js"] 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Word to Markdown 2 | 3 | Convert Word documents to beautiful Markdown. Via command line or in your browser. An even better version of the original [`word-to-markdown`](https://github.com/benbalter/word-to-markdown). 4 | 5 | ## Supports 6 | 7 | - Paragraphs 8 | - Numbered lists 9 | - Bullet lists 10 | - Nested Lists 11 | - Headings 12 | - Lists 13 | - Tables 14 | - Footnotes and endnotes 15 | - Images 16 | - Bold, italics, underlines, strikethrough, superscript and subscript. 17 | - Links 18 | - Line breaks 19 | - Text boxes 20 | - Comments 21 | 22 | ## How is this different from the original? 23 | 24 | _TL;DR: This project is a complete rewrite, using modern tools and libraries, and is much faster and more reliable. The output should be the same or better. [Feedback welcome!](https://github.com/benbalter/word-to-markdown-js/issues/new)_ 25 | 26 | ## A note on privacy 27 | 28 | Word to Markdown can be run locally or in your browser. In either event, the conversion happens locally, and no information ever leaves your browser. 29 | 30 | ## Running Locally 31 | 32 | ## Get Setup 33 | 34 | 1. Clone the repo 35 | 2. Run `npm install` 36 | 37 | ## Command line 38 | 39 | Run `w2m path/to/your/file.docx` 40 | 41 | ## Web server (static HTML) 42 | 43 | `npm run server:web` 44 | 45 | ## Web server (HTTP API) 46 | 47 | You can also run Word to Markdown as an HTTP API server, where you can make requests from elsewhere. 48 | 49 | `npm run server` 50 | 51 | The server exposes a `POST /raw` endpoint, which returns the converted Markdown. 52 | 53 | ## More context 54 | 55 | See the README of [the original Word to Markdown](https://github.com/benbalter/word-to-markdown?tab=readme-ov-file#the-problem) for the project's motivation. 56 | 57 | ### The old way 58 | 59 | [The Original](https://github.com/benbalter/word-to-markdown) Word to Markdown is 10 years old. The conversion process was as follows: 60 | 61 | 1. Use [LibreOffice](https://www.libreoffice.org/) to convert the Word document to HTML. 62 | 2. Use a bunch of RegEx to clean up the HTML 63 | 3. User [Premailer](https://github.com/premailer/premailer) to inline the CSS 64 | 4. Use [Nokogiri](https://nokogiri.org) to manipulate the HTML further 65 | 5. Use [Reverse Markdown](https://github.com/xijo/reverse_markdown) to convert the HTML to Markdown 66 | 6. Use a bunch of RegEx to clean up the Markdown 67 | 68 | Not only did this process require installing and shelling out to a huge binary (LibreOffice), but it was very fragile, and key projects like Reverse Markdown are no longer maintained. I tried experimenting with Pandoc, but it had many of the same limitation. 69 | 70 | ### The new way 71 | 72 | 1. Use [Mammoth.js](https://github.com/mwilliamson/mammoth.js/) to convert the Word document to HTML. 73 | 2. Use [Turndown](https://github.com/mixmark-io/turndown) to convert the HTML to Markdown. 74 | 3. Use [Markdownlint](https://github.com/DavidAnson/markdownlint) to clean up the Markdown. 75 | 76 | All three of these projects are actively maintained and heavily used, and allows us to convert the document faster, and entirely in JavaScript. Heck, I think theoretically, this could run in the browser for added privacy. 77 | 78 | It's still in beta, but so far, I've found the output to be better, with much less manual cleanup required. Notice something is off? Please [open an issue](https://github.com/benbalter/word-to-markdown-js/issues/new). 79 | 80 | One note: This project does not yet attempt to guess heading levels based on font size. It could, but it's not yet implemented. 81 | -------------------------------------------------------------------------------- /build/__tests__/main.test.js: -------------------------------------------------------------------------------- 1 | import { __awaiter } from "tslib"; 2 | import convert from '../main.js'; 3 | // Map of fixtures and expected Markdown output 4 | const expectations = { 5 | em: 'This word is _italic_.', 6 | strong: 'This word is **bold**.', 7 | h1: '# Heading 1\n\nParagraph text', 8 | h2: '## Heading 2\n\nParagraph text', 9 | p: 'This is paragraph text.', 10 | 'multiple-headings': '# H1\n\nParagraph\n\n## H2\n\nParagraph\n\n### H3\n\nParagraph', 11 | table: '| **Foo** | **Bar** |\n| --- | --- |\n| One | Two |\n| Three | Four |', 12 | ul: '- One\n- Two\n- Three', 13 | ol: '1. One\n2. Two\n3. Three', 14 | 'nested-ol': '1. One\n 1. Sub one\n 2. Sub two\n2. Two\n 1. Sub one\n 1. Sub sub one\n 2. Sub sub two\n 2. Sub two\n3. Three', 15 | 'nested-ul': '- One\n - Sub one\n - Sub sub one\n - Sub sub two\n - Sub two\n- Two', 16 | 'list-with-links': '[word-to-markdown](https://github.com/benbalter/word-to-markdown)\n\n- [word-to-markdown](https://github.com/benbalter/word-to-markdown)', 17 | 'comma after bold': 'This is **bolded**, and text.', 18 | 'text after bold': '**This** is **bolded** _and_ text.', 19 | 'file with space': 'This is paragraph text.', 20 | }; 21 | describe('main', () => { 22 | for (const [fixture, expected] of Object.entries(expectations)) { 23 | it(`should convert the "${fixture}" fixture to Markdown`, () => __awaiter(void 0, void 0, void 0, function* () { 24 | const path = `src/__fixtures__/${fixture}.docx`; 25 | const md = yield convert(path); 26 | expect(md).toEqual(expected); 27 | })); 28 | } 29 | }); 30 | //# sourceMappingURL=main.test.js.map -------------------------------------------------------------------------------- /build/__tests__/main.test.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"main.test.js","sourceRoot":"","sources":["../../src/__tests__/main.test.ts"],"names":[],"mappings":";AAAA,OAAO,OAAO,MAAM,YAAY,CAAC;AAEjC,+CAA+C;AAC/C,MAAM,YAAY,GAAG;IACnB,EAAE,EAAE,wBAAwB;IAC5B,MAAM,EAAE,wBAAwB;IAChC,EAAE,EAAE,+BAA+B;IACnC,EAAE,EAAE,gCAAgC;IACpC,CAAC,EAAE,yBAAyB;IAC5B,mBAAmB,EACjB,gEAAgE;IAClE,KAAK,EACH,uEAAuE;IACzE,EAAE,EAAE,uBAAuB;IAC3B,EAAE,EAAE,0BAA0B;IAC9B,WAAW,EACT,0IAA0I;IAC5I,WAAW,EACT,8EAA8E;IAChF,iBAAiB,EACf,0IAA0I;IAC5I,kBAAkB,EAAE,+BAA+B;IACnD,iBAAiB,EAAE,oCAAoC;IACvD,iBAAiB,EAAE,yBAAyB;CAC7C,CAAC;AAEF,QAAQ,CAAC,MAAM,EAAE,GAAG,EAAE;IACpB,KAAK,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;QAC/D,EAAE,CAAC,uBAAuB,OAAO,uBAAuB,EAAE,GAAS,EAAE;YACnE,MAAM,IAAI,GAAG,oBAAoB,OAAO,OAAO,CAAC;YAChD,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;YAC/B,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC/B,CAAC,CAAA,CAAC,CAAC;IACL,CAAC;AACH,CAAC,CAAC,CAAC"} -------------------------------------------------------------------------------- /build/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import { __awaiter } from "tslib"; 3 | import { Command } from 'commander'; 4 | import convert from './main.js'; 5 | const program = new Command(); 6 | program.name('w2m'); 7 | program.description('Convert Word documents to beautiful Markdown'); 8 | program 9 | .command('convert', { isDefault: true }) 10 | .argument('', 'The Word document to convert') 11 | .action((file) => __awaiter(void 0, void 0, void 0, function* () { 12 | const md = yield convert(file); 13 | console.log(md); 14 | })); 15 | program.parse(); 16 | //# sourceMappingURL=cli.js.map -------------------------------------------------------------------------------- /build/cli.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,OAAO,MAAM,WAAW,CAAC;AAEhC,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACpB,OAAO,CAAC,WAAW,CAAC,8CAA8C,CAAC,CAAC;AACpE,OAAO;KACJ,OAAO,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;KACvC,QAAQ,CAAC,QAAQ,EAAE,8BAA8B,CAAC;KAClD,MAAM,CAAC,CAAO,IAAI,EAAE,EAAE;IACrB,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/B,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;AAClB,CAAC,CAAA,CAAC,CAAC;AAEL,OAAO,CAAC,KAAK,EAAE,CAAC"} -------------------------------------------------------------------------------- /build/index.js: -------------------------------------------------------------------------------- 1 | import { __awaiter } from "tslib"; 2 | import convert from './main.js'; 3 | import rehypeSanitize from 'rehype-sanitize'; 4 | import rehypeStringify from 'rehype-stringify'; 5 | import remarkParse from 'remark-parse'; 6 | import remarkRehype from 'remark-rehype'; 7 | import { unified } from 'unified'; 8 | import remarkGfm from 'remark-gfm'; 9 | import ClipboardJS from 'clipboard'; 10 | import 'bootstrap/dist/css/bootstrap.min.css'; 11 | function handleFile() { 12 | return __awaiter(this, void 0, void 0, function* () { 13 | const reader = new FileReader(); 14 | const file = this.files[0]; 15 | reader.readAsArrayBuffer(file); 16 | reader.onload = () => __awaiter(this, void 0, void 0, function* () { 17 | new ClipboardJS('#copy-button'); 18 | const md = yield convert(reader.result); 19 | const outputElement = document.getElementById('output'); 20 | outputElement.innerText = md; 21 | const html = yield unified() 22 | .use(remarkParse) 23 | .use(remarkGfm) 24 | .use(remarkRehype) 25 | .use(rehypeSanitize) 26 | .use(rehypeStringify) 27 | .process(md); 28 | const renderedElement = document.getElementById('rendered'); 29 | renderedElement.innerHTML = String(html); 30 | const filenameElement = document.getElementById('filename'); 31 | filenameElement.innerText = file.name; 32 | const inputElement = document.getElementById('input'); 33 | inputElement.classList.add('d-none'); 34 | const resultsElement = document.getElementById('results'); 35 | resultsElement.classList.remove('d-none'); 36 | }); 37 | }); 38 | } 39 | document.addEventListener('DOMContentLoaded', () => { 40 | const inputElement = document.getElementById('file'); 41 | inputElement.addEventListener('change', handleFile, false); 42 | }); 43 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /build/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,cAAc,MAAM,iBAAiB,CAAC;AAC7C,OAAO,eAAe,MAAM,kBAAkB,CAAC;AAC/C,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,WAAW,CAAC;AACpC,OAAO,sCAAsC,CAAC;AAE9C,SAAe,UAAU;;QACvB,MAAM,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAChC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC3B,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;QAC/B,MAAM,CAAC,MAAM,GAAG,GAAwB,EAAE;YACxC,IAAI,WAAW,CAAC,cAAc,CAAC,CAAC;YAEhC,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAExC,MAAM,aAAa,GAAG,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;YACxD,aAAa,CAAC,SAAS,GAAG,EAAE,CAAC;YAE7B,MAAM,IAAI,GAAG,MAAM,OAAO,EAAE;iBACzB,GAAG,CAAC,WAAW,CAAC;iBAChB,GAAG,CAAC,SAAS,CAAC;iBACd,GAAG,CAAC,YAAY,CAAC;iBACjB,GAAG,CAAC,cAAc,CAAC;iBACnB,GAAG,CAAC,eAAe,CAAC;iBACpB,OAAO,CAAC,EAAE,CAAC,CAAC;YAEf,MAAM,eAAe,GAAG,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;YAC5D,eAAe,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAEzC,MAAM,eAAe,GAAG,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;YAC5D,eAAe,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC;YAEtC,MAAM,YAAY,GAAG,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YACtD,YAAY,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAErC,MAAM,cAAc,GAAG,QAAQ,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;YAC1D,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CAAA,CAAC;IACJ,CAAC;CAAA;AAED,QAAQ,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,GAAG,EAAE;IACjD,MAAM,YAAY,GAAG,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IACrD,YAAY,CAAC,gBAAgB,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;AAC7D,CAAC,CAAC,CAAC"} -------------------------------------------------------------------------------- /build/main.js: -------------------------------------------------------------------------------- 1 | import { __awaiter } from "tslib"; 2 | import TurndownService from '@joplin/turndown'; 3 | import * as turndownPluginGfm from '@joplin/turndown-plugin-gfm'; 4 | import * as mammoth from 'mammoth'; 5 | import markdownlint from 'markdownlint'; 6 | import markdownlintRuleHelpers from 'markdownlint-rule-helpers'; 7 | import { parse } from 'node-html-parser'; 8 | const defaultTurndownOptions = { 9 | headingStyle: 'atx', 10 | codeBlockStyle: 'fenced', 11 | bulletListMarker: '-', 12 | }; 13 | // Turndown will add an empty header if the first row 14 | // of the table isn't `` elements. This function 15 | // converts the first row of a table to `` elements 16 | // so that it renders correctly in Markdown. 17 | function autoTableHeaders(html) { 18 | const root = parse(html); 19 | root.querySelectorAll('table').forEach((table) => { 20 | const firstRow = table.querySelector('tr'); 21 | firstRow.querySelectorAll('td').forEach((cell) => { 22 | cell.tagName = 'th'; 23 | }); 24 | }); 25 | return root.toString(); 26 | } 27 | // Convert HTML to GitHub-flavored Markdown 28 | function htmlToMd(html, options = {}) { 29 | const turndownService = new TurndownService(Object.assign(Object.assign({}, options), defaultTurndownOptions)); 30 | turndownService.use(turndownPluginGfm.gfm); 31 | return turndownService.turndown(html).trim(); 32 | } 33 | // Lint the Markdown and correct any issues 34 | function lint(md) { 35 | const lintResult = markdownlint.sync({ strings: { md } }); 36 | return markdownlintRuleHelpers.applyFixes(md, lintResult['md']).trim(); 37 | } 38 | // Converts a Word document to crisp, clean Markdown 39 | export default function convert(input, options = {}) { 40 | return __awaiter(this, void 0, void 0, function* () { 41 | let inputObj; 42 | if (typeof input === 'string') { 43 | inputObj = { path: input }; 44 | } 45 | else { 46 | inputObj = { arrayBuffer: input }; 47 | } 48 | const mammothResult = yield mammoth.convertToHtml(inputObj, options.mammoth); 49 | const html = autoTableHeaders(mammothResult.value); 50 | const md = htmlToMd(html, options.turndown); 51 | const cleanedMd = lint(md); 52 | return cleanedMd; 53 | }); 54 | } 55 | //# sourceMappingURL=main.js.map -------------------------------------------------------------------------------- /build/main.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AAAA,OAAO,eAAe,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,iBAAiB,MAAM,6BAA6B,CAAC;AACjE,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,YAAY,MAAM,cAAc,CAAC;AACxC,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAazC,MAAM,sBAAsB,GAAoB;IAC9C,YAAY,EAAE,KAAK;IACnB,cAAc,EAAE,QAAQ;IACxB,gBAAgB,EAAE,GAAG;CACtB,CAAC;AAEF,qDAAqD;AACrD,oDAAoD;AACpD,uDAAuD;AACvD,4CAA4C;AAC5C,SAAS,gBAAgB,CAAC,IAAY;IACpC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;IACzB,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QAC/C,MAAM,QAAQ,GAAG,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC3C,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAC/C,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACtB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;AACzB,CAAC;AAED,2CAA2C;AAC3C,SAAS,QAAQ,CAAC,IAAY,EAAE,UAAkB,EAAE;IAClD,MAAM,eAAe,GAAG,IAAI,eAAe,iCACtC,OAAO,GACP,sBAAsB,EACzB,CAAC;IACH,eAAe,CAAC,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;IAC3C,OAAO,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;AAC/C,CAAC;AAED,2CAA2C;AAC3C,SAAS,IAAI,CAAC,EAAU;IACtB,MAAM,UAAU,GAAG,YAAY,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IAC1D,OAAO,uBAAuB,CAAC,UAAU,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACzE,CAAC;AAED,oDAAoD;AACpD,MAAM,CAAC,OAAO,UAAgB,OAAO,CACnC,KAA2B,EAC3B,UAA0B,EAAE;;QAE5B,IAAI,QAAyD,CAAC;QAC9D,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,QAAQ,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,QAAQ,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC;QACpC,CAAC;QACD,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,aAAa,CAAC,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAC7E,MAAM,IAAI,GAAG,gBAAgB,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QACnD,MAAM,EAAE,GAAG,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC5C,MAAM,SAAS,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC;QAC3B,OAAO,SAAS,CAAC;IACnB,CAAC;CAAA"} -------------------------------------------------------------------------------- /build/server.js: -------------------------------------------------------------------------------- 1 | import { __awaiter } from "tslib"; 2 | import express from 'express'; 3 | import multer from 'multer'; 4 | import os from 'os'; 5 | import convert from './main.js'; 6 | import helmet from 'helmet'; 7 | import morgan from 'morgan'; 8 | const app = express(); 9 | const port = process.env.PORT || 3000; 10 | const upload = multer({ dest: os.tmpdir() }); 11 | app.use(morgan('combined')); 12 | app.use(helmet()); 13 | app.post('/raw', upload.single('doc'), (req, res) => __awaiter(void 0, void 0, void 0, function* () { 14 | if (!(req.file instanceof multer.File)) { 15 | res.status(400).send('You must upload a document to convert.'); 16 | return; 17 | } 18 | const md = yield convert(req.file.path); 19 | res.status(200).send(md); 20 | return; 21 | })); 22 | app.get('/_healthcheck', (_req, res) => { 23 | res.status(200).send('OK'); 24 | return; 25 | }); 26 | app.listen(port, () => { 27 | console.log(`Example app listening on port ${port}`); 28 | }); 29 | //# sourceMappingURL=server.js.map -------------------------------------------------------------------------------- /build/server.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,OAAO,MAAM,WAAW,CAAC;AAChC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AACtB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC;AACtC,MAAM,MAAM,GAAG,MAAM,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7C,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;AAE5B,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;AAElB,GAAG,CAAC,IAAI,CACN,MAAM,EACN,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EACpB,CAAO,GAAoC,EAAE,GAAG,EAAE,EAAE;IAClD,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,YAAY,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,wCAAwC,CAAC,CAAC;QAC/D,OAAO;IACT,CAAC;IAED,MAAM,EAAE,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAExC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzB,OAAO;AACT,CAAC,CAAA,CACF,CAAC;AAEF,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;IACrC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO;AACT,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IACpB,OAAO,CAAC,GAAG,CAAC,iCAAiC,IAAI,EAAE,CAAC,CAAC;AACvD,CAAC,CAAC,CAAC"} -------------------------------------------------------------------------------- /build/web.js: -------------------------------------------------------------------------------- 1 | export {}; 2 | //# sourceMappingURL=web.js.map 3 | -------------------------------------------------------------------------------- /build/web.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"web.js","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":""} -------------------------------------------------------------------------------- /dist/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | Word to Markdown 11 | 12 | 13 | 14 | 15 |
16 |

Word to Markdown

17 |
18 |
19 |
20 |

Step 1

21 |

Upload a .docx or .doc file

22 |

Drag-and-drop the file below to upload.

23 |

24 | Google doc? File → Download as → .docx. 25 |

26 |
27 |
28 |

Step 2

29 |

Get back crisp, clean Markdown

30 |
# H1
 31 | 
 32 | Paragraph
 33 | 
 34 | ## H2
 35 | 
 36 | Paragraph
 37 | 
 38 | ### H3
 39 | 
 40 | Paragraph
 41 |         
42 |
43 | 44 |
45 |

Step 3

46 |

???

47 |
48 |
49 |
50 |
51 |
52 | 57 | 60 |
61 |
62 |
63 |
64 |
65 |

Results of converting

66 | 67 |
68 | 76 |
77 | 78 |
79 |
80 |

Markdown

81 |
84 |
85 |
86 |

Rendered

87 |
91 |
92 |
93 |
94 | 115 |
116 | 117 | 118 | -------------------------------------------------------------------------------- /dist/main.js.LICENSE.txt: -------------------------------------------------------------------------------- 1 | /*! 2 | 3 | JSZip v3.10.1 - A JavaScript class for generating and reading zip files 4 | 5 | 6 | (c) 2009-2016 Stuart Knightley 7 | Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/main/LICENSE.markdown. 8 | 9 | JSZip uses the library pako released under the MIT license : 10 | https://github.com/nodeca/pako/blob/main/LICENSE 11 | */ 12 | 13 | /*! 14 | * clipboard.js v2.0.11 15 | * https://clipboardjs.com/ 16 | * 17 | * Licensed MIT © Zeno Rocha 18 | */ 19 | 20 | /*! https://mths.be/he v1.2.0 by @mathias | MIT license */ 21 | 22 | /*! https://mths.be/punycode v1.4.1 by @mathias */ 23 | 24 | /*! markdownlint-micromark 0.1.8 https://github.com/DavidAnson/markdownlint */ 25 | 26 | //!! Deliberately using an API that's deprecated in node.js because 27 | 28 | //!! Discussion: github.com/node-browser-compat/atob/pull/9 29 | 30 | //!! this file is for browsers and we expect them to cope with it. 31 | -------------------------------------------------------------------------------- /dist/privacy/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | Word to Markdown Converter 12 | 13 | 14 | 18 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |

29 | Word to Markdown Converter 32 |

33 | 34 |

Your privacy

35 | 36 |
37 |

Information collected

38 |

39 | TL;DR: This website is provided as a free service to convert 40 | Word documents to Markdown, and only collects and stores the 41 | bare-minimum information necessary to do just that. 42 |

43 |

Information you provide

44 |

45 | If you upload a document, it is processed within your browser, and the 46 | contents are not sent to any server or third party. 47 |

48 |

Information collected automatically

49 |
    50 |
  • 51 | This site may use Google Analytics to track anonymous usage 52 | information. Your information may be subject to 53 | their third-party terms. 56 |
  • 57 |
  • 58 | This site's hosting provider (GitHub) logs basic diagnostic 59 | information, such as the time and date of your request, your IP 60 | address, etc. 61 |
  • 62 |
63 |

How information is used

64 |
    65 |
  • 66 | Documents you upload are used to generate a Markdown representation 67 | of the document's contents. That's why you uploaded them to this 68 | site. 69 |
  • 70 |
  • 71 | Usage information is used to gauge interest in this site. If 72 | nobody's using it, why maintain it? 73 |
  • 74 |
  • 75 | Diagnostic information is used to diagnose conversion errors, most 76 | likely in response to an issue submitted 77 | via the issue tracker. 80 |
  • 81 |
82 |

How information is shared

83 |

84 | It's not, beyond relying on third-parties for tracking usage 85 | information. 86 |

87 |

A note on security

88 |

89 | This website is provided for your convenience. All traffic is 90 | encrypted and documents are not transmitted off your device. If you'd 91 | like to better understand what's happening under the hood you can 92 | view the source code. Of course, if you don't want to trust a stranger on the internet 95 | with your super-important work document, you can always 96 | convert the file on your own machine. 99 |

100 |
101 | 102 | 123 |
124 | 125 | 126 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /dist/terms/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | Word to Markdown Converter 12 | 13 | 14 | 18 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |

29 | Word to Markdown Converter 32 |

33 | 34 |

Terms of Use

35 | 36 |
37 |

38 | TL;DR: This service is provided for your convenience, without 39 | any warranty of any kind. In an ideal world, if you use common sense, 40 | it will convert your document securely and privately, and this site 41 | will try it's best, but make no guarantees whatsoever. 42 |

43 |

44 | Like any other website, your use of this service is subject to these 45 | terms, meaning if you use the service, you agree to be bound by these 46 | terms. That also means, that if you do not agree with all of 47 | the terms here, you may not use the service. 48 |

49 |

50 | Assuming, "You" are the person uploading a document to be 51 | converted (your "Content"), and "the Service" is 52 | the website you are currently on to convert your Content, 53 | specifically: 54 |

55 |
    56 |
  • 57 | Don't use this Service for anything illegal. Don't try to hack or 58 | overburden the Service. Don't use it to harm others. 59 |
  • 60 |
  • 61 | You may not use this Service if you are under 13 years old. Explain 62 | to your parents what Markdown is and have them use the Service on 63 | your behalf. 64 |
  • 65 |
  • 66 | The Content you upload to be converted is your Content and you are 67 | responsible for it and any harm that it may cause to yourself or 68 | others. 69 |
  • 70 |
  • 71 | The Service doesn't claim any ownership in the original Content or 72 | Markdown conversion. You, however, do claim that you have a right to 73 | possess and convert the Content. If you don't, don't use this 74 | service to convert it. If Content you upload infringes on the rights 75 | of others, that's on you. 76 |
  • 77 |
  • 78 | The Service does not review your Content nor the Content of others, 79 | nor does it endorse any Content uploaded or converted. You're 80 | responsible for ensuring the Content or converted Content doesn't 81 | harm your computer (or anyone else's). 82 |
  • 83 |
  • 84 | You use this Service at your own risk and it might break or cease to 85 | exist at any time. 86 |
  • 87 |
  • 88 | If you'd like to know how your information is used and stored, read 89 | more about your privacy. 90 |
  • 91 |
92 |

93 | This Service is provided "as is", without warranty of any 95 | kind, express or implied, including but not limited to the 96 | warranties of merchantability, fitness for a particular purpose, or 97 | non-infringement. In no event shall the owners, maintainers, or 98 | copyright holders be liable for any claim, damages, or other 99 | liability, whether in action of tort, contract, or otherwise, 100 | arising from, out of, or in connection with the service or its 101 | use. 103 |

104 |
105 | 106 | 127 |
128 | 129 | 130 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | testEnvironment: 'node', 3 | preset: 'ts-jest/presets/default-esm', 4 | transform: { 5 | '^.+\\.m?[tj]s?$': ['ts-jest', { useESM: true }], 6 | }, 7 | moduleNameMapper: { 8 | '^(\\.{1,2}/.*)\\.(m)?js$': '$1', 9 | }, 10 | testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(m)?ts$', 11 | coverageDirectory: 'coverage', 12 | collectCoverageFrom: [ 13 | 'src/**/*.ts', 14 | 'src/**/*.mts', 15 | '!src/**/*.d.ts', 16 | '!src/**/*.d.mts', 17 | ], 18 | }; 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "word-to-markdown", 3 | "version": "0.0.0", 4 | "description": "Convert Word documents to beautiful Markdown.", 5 | "type": "module", 6 | "engines": { 7 | "node": ">= 20.9 < 21" 8 | }, 9 | "devDependencies": { 10 | "@types/jest": "~29.5", 11 | "@types/node": "~20", 12 | "@types/turndown": "^5.0.4", 13 | "@typescript-eslint/eslint-plugin": "~6.15", 14 | "@typescript-eslint/parser": "~6.15", 15 | "css-loader": "^7.1.2", 16 | "eslint": "~8.56", 17 | "eslint-config-prettier": "~9.1", 18 | "eslint-plugin-jest": "~27.6", 19 | "jest": "~29.7", 20 | "prettier": "~3.1", 21 | "style-loader": "^4.0.0", 22 | "ts-api-utils": "~1.0", 23 | "ts-jest": "~29.1", 24 | "ts-loader": "^9.5.1", 25 | "typescript": "~5.3", 26 | "webpack": "^5.91.0", 27 | "webpack-cli": "^5.1.4", 28 | "webpack-dev-server": "^5.0.4" 29 | }, 30 | "scripts": { 31 | "fix": "eslint --fix --ext .ts . && prettier --write .", 32 | "build": "tsc", 33 | "build:web": "webpack build", 34 | "server:web": "webpack serve --open", 35 | "lint": "eslint . --ext .ts --ext .mts", 36 | "test": "NODE_OPTIONS=--experimental-vm-modules jest --coverage", 37 | "all": "npm run lint && npm run test && npm run build", 38 | "server": "node build/server.js" 39 | }, 40 | "author": "Ben Balter ", 41 | "license": "ISC", 42 | "dependencies": { 43 | "@joplin/turndown": "^4.0.72", 44 | "@joplin/turndown-plugin-gfm": "^1.0.54", 45 | "@types/helmet": "^4.0.0", 46 | "bootstrap": "^5.3.3", 47 | "clipboard": "^2.0.11", 48 | "commander": "^12.0.0", 49 | "express": "^4.18.2", 50 | "helmet": "^7.1.0", 51 | "mammoth": "^1.6.0", 52 | "markdownlint": "^0.33.0", 53 | "markdownlint-rule-helpers": "^0.24.0", 54 | "morgan": "^1.10.0", 55 | "multer": "^1.4.5-lts.1", 56 | "node-html-parser": "^6.1.12", 57 | "rehype-sanitize": "^6.0.0", 58 | "rehype-stringify": "^10.0.0", 59 | "remark-gfm": "^4.0.0", 60 | "remark-parse": "^11.0.0", 61 | "remark-rehype": "^11.1.0", 62 | "tslib": "~2.6", 63 | "unified": "^11.0.4", 64 | "url": "^0.11.3" 65 | }, 66 | "volta": { 67 | "node": "20.10.0" 68 | }, 69 | "bin": { 70 | "w2m": "build/cli.js" 71 | }, 72 | "main": "build/main.js" 73 | } 74 | -------------------------------------------------------------------------------- /src/__fixtures__/comma after bold.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/comma after bold.docx -------------------------------------------------------------------------------- /src/__fixtures__/em.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/em.docx -------------------------------------------------------------------------------- /src/__fixtures__/file with space.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/file with space.docx -------------------------------------------------------------------------------- /src/__fixtures__/h1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/h1.docx -------------------------------------------------------------------------------- /src/__fixtures__/h2.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/h2.docx -------------------------------------------------------------------------------- /src/__fixtures__/list-with-links.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/list-with-links.docx -------------------------------------------------------------------------------- /src/__fixtures__/multiple-headings.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/multiple-headings.docx -------------------------------------------------------------------------------- /src/__fixtures__/nested-ol.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/nested-ol.docx -------------------------------------------------------------------------------- /src/__fixtures__/nested-ul.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/nested-ul.docx -------------------------------------------------------------------------------- /src/__fixtures__/ol.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/ol.docx -------------------------------------------------------------------------------- /src/__fixtures__/p.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/p.docx -------------------------------------------------------------------------------- /src/__fixtures__/small-medium-large.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/small-medium-large.docx -------------------------------------------------------------------------------- /src/__fixtures__/strong.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/strong.docx -------------------------------------------------------------------------------- /src/__fixtures__/table.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/table.docx -------------------------------------------------------------------------------- /src/__fixtures__/text after bold.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/text after bold.docx -------------------------------------------------------------------------------- /src/__fixtures__/ul.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benbalter/word-to-markdown-js/47a55587bbc5e13ffb8de8e24d59cffcadf1e4eb/src/__fixtures__/ul.docx -------------------------------------------------------------------------------- /src/__tests__/main.test.ts: -------------------------------------------------------------------------------- 1 | import convert from '../main.js'; 2 | 3 | // Map of fixtures and expected Markdown output 4 | const expectations = { 5 | em: 'This word is _italic_.', 6 | strong: 'This word is **bold**.', 7 | h1: '# Heading 1\n\nParagraph text', 8 | h2: '## Heading 2\n\nParagraph text', 9 | p: 'This is paragraph text.', 10 | 'multiple-headings': 11 | '# H1\n\nParagraph\n\n## H2\n\nParagraph\n\n### H3\n\nParagraph', 12 | table: 13 | '| **Foo** | **Bar** |\n| --- | --- |\n| One | Two |\n| Three | Four |', 14 | ul: '- One\n- Two\n- Three', 15 | ol: '1. One\n2. Two\n3. Three', 16 | 'nested-ol': 17 | '1. One\n 1. Sub one\n 2. Sub two\n2. Two\n 1. Sub one\n 1. Sub sub one\n 2. Sub sub two\n 2. Sub two\n3. Three', 18 | 'nested-ul': 19 | '- One\n - Sub one\n - Sub sub one\n - Sub sub two\n - Sub two\n- Two', 20 | 'list-with-links': 21 | '[word-to-markdown](https://github.com/benbalter/word-to-markdown)\n\n- [word-to-markdown](https://github.com/benbalter/word-to-markdown)', 22 | 'comma after bold': 'This is **bolded**, and text.', 23 | 'text after bold': '**This** is **bolded** _and_ text.', 24 | 'file with space': 'This is paragraph text.', 25 | }; 26 | 27 | describe('main', () => { 28 | for (const [fixture, expected] of Object.entries(expectations)) { 29 | it(`should convert the "${fixture}" fixture to Markdown`, async () => { 30 | const path = `src/__fixtures__/${fixture}.docx`; 31 | const md = await convert(path); 32 | expect(md).toEqual(expected); 33 | }); 34 | } 35 | }); 36 | -------------------------------------------------------------------------------- /src/cli.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import { Command } from 'commander'; 4 | import convert from './main.js'; 5 | 6 | const program = new Command(); 7 | program.name('w2m'); 8 | program.description('Convert Word documents to beautiful Markdown'); 9 | program 10 | .command('convert', { isDefault: true }) 11 | .argument('', 'The Word document to convert') 12 | .action(async (file) => { 13 | const md = await convert(file); 14 | console.log(md); 15 | }); 16 | 17 | program.parse(); 18 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import convert from './main.js'; 2 | import rehypeSanitize from 'rehype-sanitize'; 3 | import rehypeStringify from 'rehype-stringify'; 4 | import remarkParse from 'remark-parse'; 5 | import remarkRehype from 'remark-rehype'; 6 | import { unified } from 'unified'; 7 | import remarkGfm from 'remark-gfm'; 8 | import ClipboardJS from 'clipboard'; 9 | 10 | async function handleFile(): Promise { 11 | const reader = new FileReader(); 12 | const file = this.files[0]; 13 | reader.readAsArrayBuffer(file); 14 | reader.onload = async (): Promise => { 15 | const md = await convert(reader.result); 16 | 17 | const outputElement = document.getElementById('output'); 18 | outputElement.innerText = md; 19 | 20 | const html = await unified() 21 | .use(remarkParse) 22 | .use(remarkGfm) 23 | .use(remarkRehype) 24 | .use(rehypeSanitize) 25 | .use(rehypeStringify) 26 | .process(md); 27 | 28 | const renderedElement = document.getElementById('rendered'); 29 | renderedElement.innerHTML = String(html); 30 | 31 | const filenameElement = document.getElementById('filename'); 32 | filenameElement.innerText = file.name; 33 | 34 | const inputElement = document.getElementById('input'); 35 | inputElement.classList.add('d-none'); 36 | 37 | const resultsElement = document.getElementById('results'); 38 | resultsElement.classList.remove('d-none'); 39 | }; 40 | } 41 | 42 | document.addEventListener('DOMContentLoaded', () => { 43 | const inputElement = document.getElementById('file'); 44 | inputElement.addEventListener('change', handleFile, false); 45 | 46 | const copyButton = document.getElementById('copy-button'); 47 | if (copyButton !== null) { 48 | new ClipboardJS('#copy-button'); 49 | } 50 | }); 51 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import TurndownService from '@joplin/turndown'; 2 | import * as turndownPluginGfm from '@joplin/turndown-plugin-gfm'; 3 | import * as mammoth from 'mammoth'; 4 | import markdownlint from 'markdownlint'; 5 | import markdownlintRuleHelpers from 'markdownlint-rule-helpers'; 6 | import { parse } from 'node-html-parser'; 7 | 8 | interface convertOptions { 9 | mammoth?: object; 10 | turndown?: object; 11 | } 12 | 13 | interface turndownOptions { 14 | headingStyle?: 'setext' | 'atx'; 15 | codeBlockStyle?: 'indented' | 'fenced'; 16 | bulletListMarker?: '*' | '-' | '+'; 17 | } 18 | 19 | const defaultTurndownOptions: turndownOptions = { 20 | headingStyle: 'atx', 21 | codeBlockStyle: 'fenced', 22 | bulletListMarker: '-', 23 | }; 24 | 25 | // Turndown will add an empty header if the first row 26 | // of the table isn't `` elements. This function 27 | // converts the first row of a table to `` elements 28 | // so that it renders correctly in Markdown. 29 | function autoTableHeaders(html: string): string { 30 | const root = parse(html); 31 | root.querySelectorAll('table').forEach((table) => { 32 | const firstRow = table.querySelector('tr'); 33 | firstRow.querySelectorAll('td').forEach((cell) => { 34 | cell.tagName = 'th'; 35 | }); 36 | }); 37 | return root.toString(); 38 | } 39 | 40 | // Convert HTML to GitHub-flavored Markdown 41 | function htmlToMd(html: string, options: object = {}): string { 42 | const turndownService = new TurndownService({ 43 | ...options, 44 | ...defaultTurndownOptions, 45 | }); 46 | turndownService.use(turndownPluginGfm.gfm); 47 | return turndownService.turndown(html).trim(); 48 | } 49 | 50 | // Lint the Markdown and correct any issues 51 | function lint(md: string): string { 52 | const lintResult = markdownlint.sync({ strings: { md } }); 53 | return markdownlintRuleHelpers.applyFixes(md, lintResult['md']).trim(); 54 | } 55 | 56 | // Converts a Word document to crisp, clean Markdown 57 | export default async function convert( 58 | input: string | ArrayBuffer, 59 | options: convertOptions = {}, 60 | ): Promise { 61 | let inputObj: { path: string } | { arrayBuffer: ArrayBuffer }; 62 | if (typeof input === 'string') { 63 | inputObj = { path: input }; 64 | } else { 65 | inputObj = { arrayBuffer: input }; 66 | } 67 | const mammothResult = await mammoth.convertToHtml(inputObj, options.mammoth); 68 | const html = autoTableHeaders(mammothResult.value); 69 | const md = htmlToMd(html, options.turndown); 70 | const cleanedMd = lint(md); 71 | return cleanedMd; 72 | } 73 | -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import multer from 'multer'; 3 | import os from 'os'; 4 | import convert from './main.js'; 5 | import helmet from 'helmet'; 6 | import morgan from 'morgan'; 7 | import { Request } from 'express'; 8 | 9 | const app = express(); 10 | const port = process.env.PORT || 3000; 11 | const upload = multer({ dest: os.tmpdir() }); 12 | app.use(morgan('combined')); 13 | 14 | app.use(helmet()); 15 | 16 | app.post( 17 | '/raw', 18 | upload.single('doc'), 19 | async (req: Request & { file: multer.File }, res) => { 20 | if (!(req.file instanceof multer.File)) { 21 | res.status(400).send('You must upload a document to convert.'); 22 | return; 23 | } 24 | 25 | const md = await convert(req.file.path); 26 | 27 | res.status(200).send(md); 28 | return; 29 | }, 30 | ); 31 | 32 | app.get('/_healthcheck', (_req, res) => { 33 | res.status(200).send('OK'); 34 | return; 35 | }); 36 | 37 | app.listen(port, () => { 38 | console.log(`Example app listening on port ${port}`); 39 | }); 40 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES6", 4 | "module": "node16", 5 | "lib": ["ES2022", "DOM"], 6 | "moduleResolution": "node16", 7 | "rootDir": "./src", 8 | "outDir": "build", 9 | "allowSyntheticDefaultImports": true, 10 | "importHelpers": true, 11 | "alwaysStrict": true, 12 | "sourceMap": true, 13 | "forceConsistentCasingInFileNames": true, 14 | "noFallthroughCasesInSwitch": true, 15 | "noImplicitReturns": true, 16 | "noUnusedLocals": true, 17 | "noUnusedParameters": true, 18 | "noImplicitAny": false, 19 | "noImplicitThis": false, 20 | "strictNullChecks": false, 21 | "esModuleInterop": true 22 | }, 23 | "include": ["src/**/*", "__tests__/**/*"] 24 | } 25 | -------------------------------------------------------------------------------- /tsconfig.release.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "removeComments": true 6 | }, 7 | "include": ["src/**/*"] 8 | } 9 | -------------------------------------------------------------------------------- /webpack.config.cjs: -------------------------------------------------------------------------------- 1 | const webpack = require('webpack'); 2 | const nodeModulePrefixRe = /^node:/u; 3 | 4 | module.exports = { 5 | entry: ['./src/index.ts'], 6 | stats: { warnings: false }, 7 | module: { 8 | unknownContextCritical: false, 9 | rules: [ 10 | { test: /\.ts$/, use: 'ts-loader', exclude: /node_modules/ }, 11 | { 12 | test: /\.css$/, 13 | use: ['style-loader', 'css-loader'], 14 | }, 15 | ], 16 | }, 17 | devServer: { 18 | static: './dist', 19 | client: { 20 | overlay: { 21 | errors: true, 22 | warnings: false, 23 | runtimeErrors: true, 24 | }, 25 | }, 26 | }, 27 | resolve: { 28 | extensions: ['.tsx', '.ts', '.js'], 29 | alias: { 30 | './main.js': './main.ts', 31 | }, 32 | fallback: { 33 | fs: false, 34 | os: false, 35 | path: false, 36 | util: false, 37 | url: require.resolve('url/'), 38 | }, 39 | }, 40 | mode: 'production', 41 | plugins: [ 42 | new webpack.NormalModuleReplacementPlugin( 43 | nodeModulePrefixRe, 44 | (resource) => { 45 | const module = resource.request.replace(nodeModulePrefixRe, ''); 46 | resource.request = module; 47 | }, 48 | ), 49 | ], 50 | }; 51 | --------------------------------------------------------------------------------