mammoth.js
mammoth.js copied to clipboard
Amend extractRawText to display plaintext checkboxes?
Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used patch-package to patch [email protected] for the project I'm working on.
I wanted the extractRawText function to display checkboxes (eg, [ ] and [X]).
Included below is the diff that solved my problem. An alternative (and probably better/more flexible) approach would be to allow extractRawText to make use of document transformations via an options parameter, but I'm sharing my hacky solution here in case it's useful to the project or to others with the same or similar requirements.
diff --git a/node_modules/mammoth/lib/docx/body-reader.js b/node_modules/mammoth/lib/docx/body-reader.js
index 28bd2ef..cae35d0 100644
--- a/node_modules/mammoth/lib/docx/body-reader.js
+++ b/node_modules/mammoth/lib/docx/body-reader.js
@@ -164,6 +164,29 @@ function BodyReader(options) {
if (type === "begin") {
complexFieldStack.push(unknownComplexField);
currentInstrText = [];
+
+ // check if it's a checkbox, and if so, output whether it's checked or not.
+ var children = element.children || [];
+ var ffData = children.find((c) => c.name === "w:ffData");
+ if (ffData) {
+ var ffChildren = ffData.children || [];
+ var checkbox = ffChildren.find((c) => c.name === "w:checkBox");
+ if (checkbox) {
+ var cbChildren = checkbox.children || [];
+ var isChecked = cbChildren.find((c) => c.name === "w:checked");
+ if (isChecked) {
+ if (isChecked.attributes["w:val"] === "0") {
+ return new elementResult(new documents.text("[ ] "));
+ }
+ return new elementResult(new documents.text("[X] "));
+ }
+ var defaultChecked = cbChildren.find((c) => c.name === "w:default");
+ if (defaultChecked && defaultChecked.attributes && defaultChecked.attributes["w:val"] === "1") {
+ return new elementResult(new documents.text("[X] "));
+ }
+ return new elementResult(new documents.text("[ ] "));
+ }
+ }
} else if (type === "end") {
complexFieldStack.pop();
} else if (type === "separate") {
This issue body was partially generated by patch-package.