Skip to content

Commit

Permalink
perf: 移除模板依赖,整合Word文档生成功能至toDocx
Browse files Browse the repository at this point in the history
  • Loading branch information
lpreterite committed Feb 23, 2023
1 parent 1576136 commit ef8e5ff
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 693 deletions.
579 changes: 144 additions & 435 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
},
"homepage": "https://github.com/lpreterite/md2docx#readme",
"dependencies": {
"art-template": "^4.13.2",
"chalk": "^5.2.0",
"commander": "^10.0.0",
"happy-dom": "^8.7.1",
"html-to-docx": "^1.6.5",
"image-size": "^1.0.2",
"jsdom": "^21.1.0",
"jszip": "^2.7.0",
"lodash.merge": "^4.6.2",
"mime-types": "^2.1.35"
Expand All @@ -56,14 +55,17 @@
"@release-it/conventional-changelog": "^4.2.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@yelo/rollup-node-external": "^1.0.1",
"chai": "^4.2.0",
"core-js": "^3.28.0",
"cross-env": "^7.0.3",
"mocha": "^8.3.2",
"nyc": "^15.1.0",
"release-it": "^14.6.1",
"rollup": "^2.45.2",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-terser": "^7.0.2"
}
}
10 changes: 8 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import babel from "@rollup/plugin-babel";
import { terser } from "rollup-plugin-terser";
import json from '@rollup/plugin-json';
import external from '@yelo/rollup-node-external';
// import nodePolyfills from "rollup-plugin-node-polyfills";

import pkg from "./package.json";

const name = "md2docx";

const plugins = [
resolve(),
// nodePolyfills(),
resolve({ preferBuiltins: true }),
commonjs(),
babel(),
json(),
];
/**
* key为包名称,value为全局引用的值
Expand Down Expand Up @@ -37,6 +43,6 @@ export default [
},
],
plugins,
external: Object.keys(globals),
external: external(),
}
];
73 changes: 64 additions & 9 deletions src/templates/document.tpl → src/XMLTemplates.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
const bootstrapDocumentTemplate = ({
width,
height,
orient,
margins={
top: 1440,
right: 1440,
bottom: 1440,
left: 1440,
header: 720,
footer: 720,
gutter: 0,
},
}={})=>{
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
Expand Down Expand Up @@ -28,14 +42,55 @@
<w:body>
<w:altChunk r:id="htmlChunk" />
<w:sectPr>
<w:pgSz w:w="<%= width %>" w:h="<%= height %>" w:orient="<%= orient %>" />
<w:pgMar w:top="<%= margins.top %>"
w:right="<%= margins.right %>"
w:bottom="<%= margins.bottom %>"
w:left="<%= margins.left %>"
w:header="<%= margins.header %>"
w:footer="<%= margins.footer %>"
w:gutter="<%= margins.gutter %>"/>
<w:pgSz w:w="${ width }" w:h="${ height }" w:orient="${ orient }" />
<w:pgMar w:top="${ margins.top }"
w:right="${ margins.right }"
w:bottom="${ margins.bottom }"
w:left="${ margins.left }"
w:header="${ margins.header }"
w:footer="${ margins.footer }"
w:gutter="${ margins.gutter }"/>
</w:sectPr>
</w:body>
</w:document>
`
}

const mhtDocumentTemplate = ({htmlSource,contentParts}={})=>{
return `MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=mhtDocumentPart"
------=mhtDocumentPart
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: file:///C:/fake/document.html
${ htmlSource }
${ contentParts }
------=mhtDocumentPart--
`
}


const mhtPartTemplate = ({contentType,contentEncoding,contentLocation,encodedContent}={})=>{
return `------=mhtDocumentPart
Content-Type: ${ contentType }
Content-Transfer-Encoding: ${ contentEncoding }
Content-Location: ${ contentLocation }
${ encodedContent }
`
}


module.exports = {
bootstrapDocumentTemplate,
mhtDocumentTemplate,
mhtPartTemplate
}
48 changes: 25 additions & 23 deletions src/example.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
const path = require("path")
const fs = require('fs');
const JSZip = require('jszip');
const internal = require('./internal');
// const internal = require('./internal');

const toDocx = require('./toDocx')

// https://stackoverflow.com/questions/12752622/require-file-as-string
require.extensions['.tpl'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
require.extensions['.html'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
// require.extensions['.html'] = function (module, filename) {
// module.exports = fs.readFileSync(filename, 'utf8');
// };

function asBlob(html, options) {
var zip;
zip = new JSZip();
internal.addFiles(zip, html, options);
return internal.generateDocument(zip);
}
// function asBlob(html, options) {
// var zip;
// zip = new JSZip();
// internal.addFiles(zip, html, options);
// return internal.generateDocument(zip);
// }

const pwd = path.resolve(__dirname, "../assets/")
const filePath = './dist/example.docx';
const html = require("../assets/example.html");
// const pwd = path.resolve(__dirname, "../assets/")
// const filePath = './dist/example.docx';
// const html = require("../assets/example.html");

fs.writeFile(filePath, asBlob(html, { pwd }), (error) => {
if (error) {
console.log('Docx file creation failed');
return;
}
console.log('Docx file created successfully');
});
// fs.writeFile(filePath, asBlob(html, { pwd }), (error) => {
// if (error) {
// console.log('Docx file creation failed');
// return;
// }
// console.log('Docx file created successfully');
// });

const pwd = path.resolve(__dirname, "../assets/")
const html = fs.readFileSync("./assets/example.html",'utf8')
toDocx.generate('./dist/example.docx', html, { pwd })
95 changes: 0 additions & 95 deletions src/internal.js

This file was deleted.

1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./example"
17 changes: 0 additions & 17 deletions src/templates/mht_document.tpl

This file was deleted.

6 changes: 0 additions & 6 deletions src/templates/mht_part.tpl

This file was deleted.

Loading

0 comments on commit ef8e5ff

Please sign in to comment.