Skip to content

Commit

Permalink
fix: use html-minifier-terser instead of regex. feat: add option to s…
Browse files Browse the repository at this point in the history
…kip pre-processing (#51)

* feat: add option to skip pre-processing

* fix: remove readme issue

* feat: initial implementation of html-minifier-terser

* chore: terser improvements
  • Loading branch information
nicolasiscoding authored Oct 4, 2024
1 parent 52dd6bd commit ce2e9d8
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 143 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ full fledged examples can be found under `example/`
- `defaultOrderedListStyleType` <?[String]> default ordered list style type. Defaults to `decimal`.
- `decodeUnicode` <?[Boolean]> flag to enable unicode decoding of header, body and footer. Defaults to `false`.
- `lang` <?[String]> language localization code for spell checker to work properly. Defaults to `en-US`.
- `preProcessing` <?[Object]>
- `skipHTMLMinify` <?[Boolean]> flag to skip minification of HTML. Defaults to `false`.
- `footerHTMLString` <[String]> clean html string equivalent of footer. Defaults to `<p></p>` if footer flag is `true`.

### Returns
Expand Down
1 change: 1 addition & 0 deletions example/example-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ const htmlString = `<!DOCTYPE html>
table: { row: { cantSplit: true } },
footer: true,
pageNumber: true,
preprocessing: { skipHTMLMinify: false }
});

fs.writeFile(filePath, fileBuffer, (error) => {
Expand Down
1 change: 1 addition & 0 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ const htmlString = `<!DOCTYPE html>
table: { row: { cantSplit: true } },
footer: true,
pageNumber: true,
preprocessing: { skipHTMLMinify: false }
});

fs.writeFile(filePath, fileBuffer, (error) => {
Expand Down
1 change: 1 addition & 0 deletions example/react-example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,7 @@ function App() {
table: { row: { cantSplit: true } },
footer: true,
pageNumber: true,
preprocessing: { skipHTMLMinify: false }
});

saveAs(fileBuffer, 'html-to-docx.docx');
Expand Down
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/* eslint-disable no-useless-escape */
import JSZip from 'jszip';
import { minify } from 'html-minifier-terser';

import createDocumentOptionsAndMergeWithDefaults from './src/utils/options-utils';
import addFilesToContainer from './src/html-to-docx';

const minifyHTMLString = (htmlString) => {
const minifyHTMLString = async (htmlString) => {
try {
if (typeof htmlString === 'string' || htmlString instanceof String) {
const minifiedHTMLString = htmlString
.replace(/\n/g, ' ')
.replace(/\r/g, ' ')
.replace(/\r\n/g, ' ')
.replace(/[\t]+\</g, '<')
.replace(/\>[\t ]+\</g, '><')
.replace(/\>[\t ]+$/g, '>');

const minifiedHTMLString = await minify(htmlString, {
collapseWhitespace: true,
removeComments: true,
});
return minifiedHTMLString;
}

Expand All @@ -30,20 +29,22 @@ async function generateContainer(
) {
const zip = new JSZip();

const normalizedDocumentOptions = createDocumentOptionsAndMergeWithDefaults(documentOptions);

let contentHTML = htmlString;
let headerHTML = headerHTMLString;
let footerHTML = footerHTMLString;
if (htmlString) {
contentHTML = minifyHTMLString(contentHTML);
if (htmlString && !normalizedDocumentOptions['preprocessing']['skipHTMLMinify']) {
contentHTML = await minifyHTMLString(contentHTML);
}
if (headerHTMLString) {
headerHTML = minifyHTMLString(headerHTML);
if (headerHTMLString && !normalizedDocumentOptions['preprocessing']['skipHTMLMinify']) {
headerHTML = await minifyHTMLString(headerHTML);
}
if (footerHTMLString) {
footerHTML = minifyHTMLString(footerHTML);
if (footerHTMLString && !normalizedDocumentOptions['preprocessing']['skipHTMLMinify']) {
footerHTML = await minifyHTMLString(footerHTML);
}

await addFilesToContainer(zip, contentHTML, documentOptions, headerHTML, footerHTML);
await addFilesToContainer(zip, contentHTML, normalizedDocumentOptions, headerHTML, footerHTML);

const buffer = await zip.generateAsync({ type: 'arraybuffer' });
if (Object.prototype.hasOwnProperty.call(global, 'Buffer')) {
Expand Down
Loading

0 comments on commit ce2e9d8

Please sign in to comment.