html-to-word-js
Version:
一个将HTML转换为DOCX文档的TypeScript库,支持标题、段落、文本格式化、列表等常见HTML元素
2 lines (1 loc) • 8.99 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("docx"),require("cheerio")):"function"==typeof define&&define.amd?define(["exports","docx","cheerio"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).HtmlToDocxConverter={},e.docx,e.cheerio)}(this,(function(e,n,t){"use strict";function r(e){var n=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})}})),n.default=e,Object.freeze(n)}var i=r(t);function o(e,n,t,r){return new(t||(t=Promise))((function(i,o){function a(e){try{l(r.next(e))}catch(e){o(e)}}function c(e){try{l(r.throw(e))}catch(e){o(e)}}function l(e){var n;e.done?i(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(a,c)}l((r=r.apply(e,n||[])).next())}))}function a(e,n){var t,r,i,o,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(l){return function(c){if(t)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(a=0)),a;)try{if(t=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,r=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){a.label=c[1];break}if(6===c[0]&&a.label<i[1]){a.label=i[1],i=c;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(c);break}i[2]&&a.ops.pop(),a.trys.pop();continue}c=n.call(e,a)}catch(e){c=[6,e],r=0}finally{t=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,l])}}}function c(){return"undefined"!=typeof window&&"undefined"!=typeof document}function l(e){var n={},t=e.attr("style");t&&t.split(";").map((function(e){return e.trim()})).filter((function(e){return e})).forEach((function(e){var t=e.split(":").map((function(e){return e.trim()})),r=t[0],i=t[1];if(r&&i)switch(r.toLowerCase()){case"color":n.color=function(e){if(e){var n=e.trim().toLowerCase();if(n.startsWith("#")){var t=n.substring(1);if(3===t.length&&(t=t.split("").map((function(e){return e+e})).join("")),6===t.length&&/^[0-9a-f]{6}$/i.test(t))return t.toUpperCase()}var r=n.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*[\d.]+)?\s*\)/);return r?(parseInt(r[1],10).toString(16).padStart(2,"0")+parseInt(r[2],10).toString(16).padStart(2,"0")+parseInt(r[3],10).toString(16).padStart(2,"0")).toUpperCase():{black:"000000",white:"FFFFFF",red:"FF0000",green:"008000",blue:"0000FF",yellow:"FFFF00",cyan:"00FFFF",magenta:"FF00FF",orange:"FFA500",purple:"800080",gray:"808080",grey:"808080",brown:"A52A2A",pink:"FFC0CB",navy:"000080",lime:"00FF00",maroon:"800000",teal:"008080",olive:"808000",silver:"C0C0C0"}[n]}}(i);break;case"font-size":n.fontSize=function(e){if(e){var n=e.trim().toLowerCase().match(/^(\d*\.?\d+)(px|pt|em|rem|%)?$/);if(n){var t=parseFloat(n[1]);switch(n[2]||"px"){case"pt":return Math.round(2*t);case"px":default:return Math.round(1.5*t);case"em":case"rem":return Math.round(16*t*1.5);case"%":return Math.round(t/100*16*1.5)}}}}(i);break;case"font-family":n.fontFamily=i.replace(/['"]/g,"").split(",")[0].trim();break;case"font-weight":n.bold="bold"===i||"bolder"===i||parseInt(i,10)>=600;break;case"font-style":n.italic="italic"===i||"oblique"===i;break;case"text-decoration":n.underline=i.includes("underline");break;case"text-indent":case"margin-left":case"padding-left":n.indent||(n.indent=function(e){if(e){var n=e.trim().toLowerCase().match(/^(\d*\.?\d+)(px|pt|em|rem|%)?$/);if(n){var t=parseFloat(n[1]);switch(n[2]||"px"){case"pt":return Math.round(20*t);case"px":default:return Math.round(15*t);case"em":case"rem":return Math.round(16*t*15)}}}}(i))}}));var r=e.attr("data-scale");return r&&(n.scale=function(e){if(e){var n=e.trim(),t=parseFloat(n.replace("%",""));if(!(Number.isNaN(t)||t<=0))return t/100}}(r)),n}function u(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var t={};return e.forEach((function(e){e.color&&(t.color=e.color),e.fontFamily&&(t.fontFamily=e.fontFamily),void 0!==e.bold&&(t.bold=e.bold),void 0!==e.italic&&(t.italic=e.italic),void 0!==e.underline&&(t.underline=e.underline),void 0!==e.indent&&(t.indent=e.indent),e.fontSize&&(t.fontSize=e.fontSize),void 0!==e.scale&&(t.scale=e.scale)})),t.scale&&t.fontSize?t.fontSize=Math.round(t.fontSize*t.scale):t.scale&&!t.fontSize&&(t.fontSize=Math.round(24*t.scale)),t}function s(e,t){var r={h1:n.HeadingLevel.HEADING_1,h2:n.HeadingLevel.HEADING_2,h3:n.HeadingLevel.HEADING_3,h4:n.HeadingLevel.HEADING_4,h5:n.HeadingLevel.HEADING_5,h6:n.HeadingLevel.HEADING_6},i=l(e),o=new n.TextRun({text:e.text(),color:i.color,size:i.fontSize,font:i.fontFamily,bold:i.bold,italics:i.italic,underline:i.underline?{}:void 0});return new n.Paragraph({heading:r[t],children:[o],indent:i.indent?{left:i.indent}:void 0})}function d(e,t,r){void 0===r&&(r={});var i=[];return t.contents().each((function(t,o){var a;if(3===o.nodeType)(c=e(o).text()).trim()&&i.push(new n.TextRun({text:c,color:r.color,size:r.fontSize,font:r.fontFamily,bold:r.bold,italics:r.italic,underline:r.underline?{}:void 0}));else if(1===o.nodeType){var c,s=e(o),f=null===(a=s.prop("tagName"))||void 0===a?void 0:a.toLowerCase();if(!(c=s.text()).trim())return;var h=l(s),p={};switch(f){case"b":case"strong":p.bold=!0;break;case"i":case"em":p.italic=!0;break;case"u":p.underline=!0;break;case"a":p.color="0000FF",p.underline=!0;break;case"small":p.fontSize=Math.round(.8*(r.fontSize||24));break;case"big":p.fontSize=Math.round(1.2*(r.fontSize||24));break;case"sub":case"sup":p.fontSize=Math.round(.7*(r.fontSize||24))}var v=u(r,p,h);if(s.children().length>0){var b=d(e,s,v);i.push.apply(i,b)}else{var m=new n.TextRun({text:c,color:v.color,size:v.fontSize,font:v.fontFamily,bold:v.bold,italics:v.italic,underline:v.underline?{}:void 0});i.push(m)}}})),i}function f(e,t){var r=l(t),i=d(e,t,r);return new n.Paragraph({children:i.length>0?i:[new n.TextRun("")],indent:r.indent?{left:r.indent}:void 0})}function h(e,t,r){var i,o=null===(i=t.prop("tagName"))||void 0===i?void 0:i.toLowerCase();if(!o)return[];switch(o){case"h1":case"h2":case"h3":case"h4":case"h5":case"h6":return[s(t,o)];case"p":case"div":case"a":case"span":case"strong":case"b":case"em":case"i":case"u":case"small":case"big":case"sub":case"sup":return[f(e,t)];case"ul":case"ol":return function(e,t,r,i){var o=[],a=l(t);return t.find("li").each((function(t,i){var c=e(i),s=c.text().trim();if(s){var d=u(a,l(c)),f="ol"===r?"".concat(t+1,". "):"• ",h=new n.TextRun({text:f+s,color:d.color,size:d.fontSize,font:d.fontFamily,bold:d.bold,italics:d.italic,underline:d.underline?{}:void 0});o.push(new n.Paragraph({children:[h],indent:{left:(d.indent||0)+360}}))}})),0===o.length&&(i.push("空的".concat(r,"列表")),o.push(new n.Paragraph({children:[new n.TextRun("")]}))),o}(e,t,o,r);case"br":return[new n.Paragraph({children:[new n.TextRun("\n")]})];default:var a=t.text().trim();if(a){r.push("不支持的标签: ".concat(o,", 已提取文本内容和样式"));var c=l(t),d=new n.TextRun({text:a,color:c.color,size:c.fontSize,font:c.fontFamily,bold:c.bold,italics:c.italic,underline:c.underline?{}:void 0});return[new n.Paragraph({children:[d],indent:c.indent?{left:c.indent}:void 0})]}return[]}}function p(e,t){var r,c,l,u;return void 0===t&&(t={}),o(this,void 0,void 0,(function(){var o,s,d,f;return a(this,(function(a){return o=[],s=i.load(e),d=[],f=s("body").children(),(f.length>0?f:s.root().children()).each((function(e,n){var t=h(s,s(n),o);d.push.apply(d,t)})),[2,{document:new n.Document({sections:[{children:d.length>0?d:[new n.Paragraph({children:[new n.TextRun("转换的文档为空")]})],properties:{page:{margin:{top:20*((null===(r=t.margins)||void 0===r?void 0:r.top)||72),right:20*((null===(c=t.margins)||void 0===c?void 0:c.right)||72),bottom:20*((null===(l=t.margins)||void 0===l?void 0:l.bottom)||72),left:20*((null===(u=t.margins)||void 0===u?void 0:u.left)||72)}}}}]}),warnings:o}]}))}))}function v(e){return o(this,void 0,void 0,(function(){var n,t;return a(this,(function(r){switch(r.label){case 0:return[4,import("docx")];case 1:return n=r.sent().Packer,c()?[4,n.toBlob(e)]:[3,4];case 2:return[4,r.sent().arrayBuffer()];case 3:return t=r.sent(),[2,new Uint8Array(t)];case 4:return[2,n.toBuffer(e)]}}))}))}"function"==typeof SuppressedError&&SuppressedError,e.convertHtmlToDocxBuffer=function(e,n){return void 0===n&&(n={}),o(this,void 0,void 0,(function(){var t;return a(this,(function(r){switch(r.label){case 0:return[4,p(e,n)];case 1:return[4,v((t=r.sent()).document)];case 2:return[2,{buffer:r.sent(),warnings:t.warnings}]}}))}))},e.docxToBuffer=v,e.htmlToDocx=p}));