Skip to content

Commit

Permalink
fix(plugins/docx-to-txt): add header and footer to response payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed May 11, 2023
1 parent 0d2e0a3 commit e0b8519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/plugins/docx-to-txt/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
const fp = require("fastify-plugin");
const { extractRawText } = require("mammoth");
const WordExtractor = require("word-extractor");

/**
* @author Frazer Smith
* @description Pre-handler plugin that uses Mammoth to convert Buffer containing
* @description Pre-handler plugin that uses Word-Extractor to convert Buffer containing
* DOCX file in `req.body` to TXT.
* `req` object is decorated with `conversionResults.body` holding the converted document.
* @param {object} server - Fastify instance.
*/
async function plugin(server) {
const wordExtractor = new WordExtractor();

server.addHook("onRequest", async (req) => {
req.conversionResults = { body: undefined };
});

server.addHook("preHandler", async (req, res) => {
try {
const { value } = await extractRawText(req.body);
const results = await wordExtractor.extract(req.body);

const value = `${results.getHeaders({
includeFooters: false,
})}\n${results.getTextboxes({
includeHeadersAndFooters: false,
})}\n${results.getBody()}\n${results.getEndnotes()}\n${results.getFootnotes()}\n${results.getFooters()}`.trim();

req.conversionResults.body = value;
res.type("text/plain; charset=utf-8");
} catch {
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/docx-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ describe("DOCX-to-TXT conversion plugin", () => {
await server.close();
});

// TODO: fix docx-to-txt plugin to include header and footer
it.failing("Converts DOCX file to TXT", async () => {
it("Converts DOCX file to TXT", async () => {
const response = await server.inject({
method: "POST",
url: "/",
Expand Down

0 comments on commit e0b8519

Please sign in to comment.