Markdown转docx 保留Latex渲染样式
需求分析Markdown转docx有大量公式。包括行内公式和行间公式Office 自带的数学与 Latex的渲染样式存在差异。本文倾向于使用Latex的渲染样式轻量调整。转换后仅做轻微的调整不再编辑公式。选择方案主要编辑在Markdown使用Latex渲染样式docx只做轻量调整。所以先把Latex公式渲染成图片格式在生成文档中使用渲染的图片而非原始的Latex公式。pandoc 有一个选项--webtex可自动调用API渲染公式。但这个选项只支持 Markdown 和 HTML。为了解决这个问题首先 Markdown 到 HTML。然后 HTML 到 docx。可选自己搭一个简易的webtex服务。使用脚本如下。代码分享Makefile.PHONY: help serve html docx clean # Use : for simple assignment (evaluated once) INPUT : $(if $(INPUT),$(INPUT),main.md) OUTBASE : $(basename $(INPUT)) help: echo Usage: make [target] [INPUTxxx.md] echo echo Targets: echo serve - Start webtex server echo html - Convert markdown to HTML echo docx - Convert markdown to DOCX echo clean - Remove generated files echo echo Default INPUT: main.md echo echo Examples: echo make html INPUTmyfile.md echo make docx INPUTmyfile.md serve: node webtex.js html: pandoc $(INPUT) -o $(OUTBASE).html --webtexhttp://localhost:3000/png?latex sed -i s/classmath display/classmath display styledisplay:block;margin:auto;/g $(OUTBASE).html docx: html pandoc $(OUTBASE).html -o $(OUTBASE).docx clean: rm -f $(OUTBASE).html $(OUTBASE).docxwebtex.js依赖npminstallmathjax-full sharpconsthttprequire(http);consturlrequire(url);constsharprequire(sharp);const{mathjax}require(mathjax-full/js/mathjax.js);const{TeX}require(mathjax-full/js/input/tex.js);const{SVG}require(mathjax-full/js/output/svg.js);const{liteAdaptor}require(mathjax-full/js/adaptors/liteAdaptor.js);const{RegisterHTMLHandler}require(mathjax-full/js/handlers/html.js);const{AllPackages}require(mathjax-full/js/input/tex/AllPackages.js);constadaptorliteAdaptor();RegisterHTMLHandler(adaptor);consttexnewTeX({packages:AllPackages});constsvgnewSVG({fontCache:none,exFactor:1,});consthtmlmathjax.document(,{InputJax:tex,OutputJax:svg});functionrenderToSVG(latex,displayfalse){constnodehtml.convert(latex,{display});returnadaptor.innerHTML(node);}asyncfunctionrenderToPNG(latex,displayfalse,scale2){constsvgStringrenderToSVG(latex,display);returnawaitsharp(Buffer.from(svgString),{density:150*scale,background:white}).png({quality:100}).toBuffer();}constserverhttp.createServer(async(req,res){console.log(connection${req.url});constparsedUrlurl.parse(req.url,true);res.setHeader(Access-Control-Allow-Origin,*);constlatexparsedUrl.query.latex;if(!latex){res.writeHead(400);res.end(Missing latex parameter);return;}constdecodedLatexdecodeURIComponent(latex);constdisplayModeparsedUrl.query.displaytrue;constscaleparseInt(parsedUrl.query.scale)||2;try{if(parsedUrl.pathname/png||parsedUrl.pathname/){constpngBufferawaitrenderToPNG(decodedLatex,displayMode,scale);res.writeHead(200,{Content-Type:image/png});res.end(pngBuffer);}elseif(parsedUrl.pathname/svg){constsvgStringrenderToSVG(decodedLatex,displayMode);res.writeHead(200,{Content-Type:image/svgxml});res.end(svgString);}else{res.writeHead(404);res.end(Use /svg?latex... or /png?latex...);}}catch(err){console.error(Error:,err);res.writeHead(500);res.end(Error rendering LaTeX);}});constPORT3000;server.listen(PORT,(){console.log(WebTeX Server running at http://localhost:${PORT});console.log(SVG: http://localhost:${PORT}/svg?latexEmc%5E2);console.log(PNG: http://localhost:${PORT}/png?latexEmc%5E2);});参考pandocmathjax还有其他互联网资料
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2531714.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!