From 069fb9a4c1abccf810fe81df386a0d2c69a9b509 Mon Sep 17 00:00:00 2001 From: Oyasuminasai <61616918+nfnfgo@users.noreply.github.com> Date: Sun, 20 Oct 2024 22:15:03 +0800 Subject: [PATCH] fix: Fix compatibility issue with litetools. --- src/render/msgpiece_processor.tsx | 27 +++++++++++++++++++++++---- src/style/markdown.css | 6 +++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/render/msgpiece_processor.tsx b/src/render/msgpiece_processor.tsx index 69e1320..3e647a0 100644 --- a/src/render/msgpiece_processor.tsx +++ b/src/render/msgpiece_processor.tsx @@ -98,11 +98,11 @@ const textElementProcessor: FragmentProcessFunc = (parent, element, index) => { let settings = useSettingsStore.getState(); // generate rendered HTML processor based on user config. - function renderedHtmlPostProcessor(x: string) { + function renderedHtmlPostProcessor(x: string): string { // text processor if ((settings.forceEnableHtmlPurify() ?? settings.enableHtmlPurify) === true) { mditLogger('debug', `Purify`, 'Input:', `${x}`); - return purifyHtml(x); + return purifyHtml(x) as string; } return x; @@ -138,11 +138,30 @@ const textElementProcessor: FragmentProcessFunc = (parent, element, index) => { // render let renderedTextElement = element; - renderedTextElement.innerHTML = ( + let renderedMarkdownInnerHtml = ( // first use markdownit to render the html text // then passed to post processor (post processor also accept text) - renderedHtmlPostProcessor(getMarkdownIns().render(originalText)) + renderedHtmlPostProcessor(getMarkdownIns().render(originalText)).trim() ); + mditLogger('debug', 'Rendered/post-processed HTML innterText:', renderedMarkdownInnerHtml); + + // remove unnecessary wrapping

if there is only one element + let renderedHtmlElement = (new DOMParser).parseFromString(renderedMarkdownInnerHtml, 'text/html'); + mditLogger('debug', 'renderedHtmlElement.children.length==1', renderedHtmlElement.children.length == 1); + mditLogger('debug', 'renderedMarkdownInnerHtml.startsWith(p)', renderedMarkdownInnerHtml.startsWith('

')); + mditLogger('debug', 'renderedMarkdownInnerHtml.endsWith(p)', renderedMarkdownInnerHtml.endsWith('

')); + if ((renderedHtmlElement.children.length == 1) + && renderedMarkdownInnerHtml.startsWith('

') + && renderedMarkdownInnerHtml.endsWith('

')) { + renderedMarkdownInnerHtml = + renderedMarkdownInnerHtml + .substring(3, renderedMarkdownInnerHtml.length - 4) + .trim(); + mditLogger('debug', 'Striped innerHTML:', renderedMarkdownInnerHtml); + } + + renderedTextElement.innerHTML = renderedMarkdownInnerHtml; + return { original: element, diff --git a/src/style/markdown.css b/src/style/markdown.css index 3db0d5b..6ee338d 100644 --- a/src/style/markdown.css +++ b/src/style/markdown.css @@ -172,10 +172,10 @@ pre.hl-code-block > button.lang_copy:not(:hover) > p.copy { } /* 修复轻量工具箱等插件时间显示多出换行的问题 */ -.mix-message__inner { +/* .mix-message__inner { display: flex; flex-direction: row; -} +} */ .lite-tools-slot.embed-slot { flex: none; @@ -186,6 +186,6 @@ button.mdit-show-origin-button { color: inherit; opacity: 0.5; padding-inline: 2px; - text-underline: auto; + /* text-underline: auto; */ border-width: 0px; } \ No newline at end of file