The module is designed to serve an endpoint to which send a .docx document and receive in response the converted HTML version.
The actual conversion is based on mammoth, a npm module that produces simple and clean HTML by using semantic information in the document, such as styles (e.g. Heading 1).
./server.js and ./router.js host the service, receiving and routing the HTTP requests towards the several (future) services, they use express routers.
./converters/docxToHTML.js is the module that takes care of the conversion.
It receives the .docx document from the POST request body and calls mammoth convertToHTML function for conversion, passing an extra options argument with a custom Styling Map, a mammoth feature that allows customizing how the HTML is generated from the docx semantic.
This is the one being used:
var options = {styleMap: ["p[style-name='Section Title'] => h1:fresh","p[style-name='Subsection Title'] => h2:fresh"], includeDefaultStyleMap: false };
It then sends back the generated HTML wrapping it inside a 200 OK status code.
./test.js is a little module created for testing purposes, which reads a .docx local file, places it in a Node Buffer, and sends it to the server via an axios POST ajax call.
It then receives back the corresponding HTML and writes it in a local file for review.
The module comes with a config file (config.json) which allows customizing:
- server:
- host -> localhost in this case
- port -> the port on which the server listens for requests, defaults to 3000
- https -> true/false in order to build the endpoint for the HTTP call, defaults to false for localhost
- docxToHTML
- endpoint -> the path where the endpoint is served at, defaults to docxToHTML
- test:
- docxFilename -> the filename of the .docx document to be read (without extension), defaults to docxIn
- htmlFilename -> same as above but for the HTML output, defaults to htmlOut
In order to build and execute the module, these are the steps to be made:
git pullthis repo on your local machine- Run
npm installinside the directory to install the dependencies - Optional: edit config.json to customize the execution
- Run
nodemon ./server.jsto start the server - Run
node ./test.jsto test the conversion module