wordpress建博客网站,济南建设集团招聘信息网站,免费制作网站,响应式网站建设有哪些好处#1 写在前面
开始写这篇文章时#xff0c;标题怎么定困扰我良久#xff0c;缘于不晓得如何给接下来要做的事定个简单明了的标题#xff1a;在#x1f4f1;终端只能纯文本交互的前提下#xff0c;优雅展示 markdown 文档中的图片。这也许比问题本身还要棘手#x1f604;。…#1 写在前面
开始写这篇文章时标题怎么定困扰我良久缘于不晓得如何给接下来要做的事定个简单明了的标题在终端只能纯文本交互的前提下优雅展示 markdown 文档中的图片。这也许比问题本身还要棘手。
#2 背景说明
公司内网有一套基于 markdown 的文档系统方便同事查阅资料现希望能够在移动端进行浏览。
目前我们已在集团移动办公 APP 发布有 H5 小程序实现了互联网与内网的数据通信但存在以下限制
请求方式为 POST后端返回内容限定为纯文本每次发起请求终端都有 loading 弹窗无法加载互联网资源
#3 思路阐述
**方案一将图片编码进 markdown 文本 ** 识别出 markdown 内的图片转换为 BASE64 编码并替换原文本终端解析后渲染。本文采用此方案✅。 方案二延迟加载图片 终端渲染后监听页面滚动按需加载图片传递 url 或图片编号后端返回 BASE64 编码。此方案可通过自定义指令实现前后端均需要代码改造。 #3.1 处理流程
用户请求指定 ID 的 MARKDOWN 资源从数据库读取原始文本调用 MarkdownFunc.embedImages 方法若该 ID 的缓存文件存在则直接使用跳转到⑥用正则表达式匹配全部图片标签对符合后缀规范的本地文件进行以下操作 a. 原始图片宽度超出阈值则先缩放 b. 转换为 WEBP 格式节流 c. 进一步转换为 BASE64 编码 d. 替换到原标签文本将处理完成的文本写入缓存文件返回内容到客户端
同时当文档被修改后监听事件删除对应的缓存文件。
#3.2 代码实现
Configuration
ConfigurationProperties(prefix page.markdown)
class MarkdownConfig {var maxWidth 900 //图片宽度超出此值则进行压缩var quality 0.1F //转换为 webp 时质量阈值var resizeQuality 0.8f //裁剪图片的质量阈值var exts listOf(jpg,jpeg,bmp,png)var dir markdown
}Component
class MarkdownFunc(private val fileStore: FileStore,private val config: MarkdownConfig) {Value(\${server.servlet.context-path})private val contextPath private val logger LoggerFactory.getLogger(javaClass)/*** 转换为 Base64 编码*/private fun base64(bytes:ByteArray) .encodeToString(bytes)})private fun txtFile(id: Long) fileStore.buildPathWithoutDate(${id}.txt, config.dir)/**** param id 文档唯一编号* param text markdown 源文本*/fun embedImages(id:Long, text:String):String txtFile(id).let { file-if(file.exists()) returnlet Files.readString(file)Regex(!\\[.*?\\]\\((.*?)\\)).replace(text) { match-val fileUrl match.groupValues.last().let {if(it.startsWith(contextPath))it.replaceFirst(contextPath, )elseit}//暂不支持互联网资源if(fileUrl.startsWith(http)) returnreplace match.valueval imgPath Paths.get(., fileUrl)val ext imgPath.extension.lowercase()logger.info(${imgPath.toAbsolutePath() } ${imgPath.isRegularFile()})if(imgPath.exists() imgPath.isRegularFile()){if(config.exts.contains(ext)){var img ImageIO.read(imgPath.toFile()).let {if(it.width config.maxWidth){if(logger.isDebugEnabled) logger.debug(图片 $imgPath 宽度超出阈值 ${config.maxWidth} 即将裁剪...)//对图片进行缩放如需水印可以调用 watermark 方法Thumbnails.of(it).width(config.maxWidth).outputQuality(config.resizeQuality).asBufferedImage()}elseit}val out ByteArrayOutputStream()val mout MemoryCacheImageOutputStream(out)ImageIO.getImageWritersByMIMEType(image/webp).next().let { writer-writer.output moutwriter.write(null,IIOImage(img, null, null),WebPWriteParam(writer.locale).also {it.compressionMode ImageWriteParam.MODE_EXPLICITit.compressionType it.compressionTypes[WebPWriteParam.LOSSY_COMPRESSION]it.compressionQuality config.quality})if(logger.isDebugEnabled) logger.debug(图片 $imgPath 转 webp 完成...)}mout.flush()base64(out.toByteArray())}//对于 webp 格式不作缩放处理直接编码else if(ext webp){base64(Files.readAllBytes(imgPath))}else{if(logger.isDebugEnabled) logger.debug(图片 $imgPath 不是支持的格式...)match.value}}else {logger.error(图片 $imgPath 不存在或不是一个有效文件...)match.value}}.also {file.parent.also { p-if(!p.exists())Files.createDirectories(p)}Files.writeString(file, it, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)logger.info(缓存 $file 写入成功SIZE ${file.fileSize()} B)}}AsyncEventListener(PageContentUpdateEvent::class)fun onPageUpdate(event: PageContentUpdateEvent) {event.page.also {if(it.template Page.MARKDOWN){logger.info(检测到 #${it.id} 的内容变更即将删除其缓存文件若存在...)txtFile(it.id).deleteIfExists()}}}
}
文章转载自: http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.pnbls.cn.gov.cn.pnbls.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.ityi666.cn.gov.cn.ityi666.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.zkjqj.cn.gov.cn.zkjqj.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.rwyd.cn.gov.cn.rwyd.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.rwzmz.cn.gov.cn.rwzmz.cn http://www.morning.hzqjgas.com.gov.cn.hzqjgas.com http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.csnch.cn.gov.cn.csnch.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.nfpct.cn.gov.cn.nfpct.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.ryxbz.cn.gov.cn.ryxbz.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn