fix: export to html render abnormal (#468)
All checks were successful
Build and Deploy / build-and-deploy (push) Has been skipped

This commit is contained in:
dribble-njr 2024-12-12 10:38:07 +08:00 committed by GitHub
parent 2ae75ab76e
commit a62fa1f29b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 12 deletions

View File

@ -78,19 +78,19 @@ Markdown 文档自动即时渲染为微信图文,让你不再为微信文章
示例代码:
```js
const { file, util, okCb, errCb } = CUSTOM_ARG;
const param = new FormData();
param.append(`file`, file);
const { file, util, okCb, errCb } = CUSTOM_ARG
const param = new FormData()
param.append(`file`, file)
util.axios
.post(`http://127.0.0.1:9000/upload`, param, {
headers: { "Content-Type": `multipart/form-data` },
headers: { 'Content-Type': `multipart/form-data` },
})
.then((res) => {
okCb(res.url);
okCb(res.url)
})
.catch((err) => {
errCb(err);
});
errCb(err)
})
// 提供的可用参数:
// CUSTOM_ARG = {

View File

@ -335,7 +335,7 @@ export const useStore = defineStore(`store`, () => {
// 导出编辑器内容为 HTML并且下载到本地
const exportEditorContent2HTML = () => {
exportHTML()
exportHTML(primaryColor.value)
document.querySelector(`#output`)!.innerHTML = output.value
}

View File

@ -180,10 +180,16 @@ export function downloadMD(doc: string) {
/**
* HTML
*/
export function exportHTML() {
export function exportHTML(primaryColor: string) {
const element = document.querySelector(`#output`)!
setStyles(element)
const htmlStr = element.innerHTML
.replaceAll(`var(--el-text-color-regular)`, `#3f3f3f`)
.replaceAll(`var(--blockquote-background)`, `#f7f7f7`)
.replaceAll(`var(--md-primary-color)`, primaryColor)
.replaceAll(/--md-primary-color:.+?;/g, ``)
const downLink = document.createElement(`a`)
@ -205,13 +211,21 @@ export function exportHTML() {
* @param {} excludes 使
* @returns
*/
function getElementStyles(element: Element, excludes = [`width`, `height`]) {
function getElementStyles(element: Element, excludes = [`width`, `height`, `inlineSize`, `webkitLogicalWidth`, `webkitLogicalHeight`]) {
const styles = getComputedStyle(element, null)
return Object.entries(styles)
.filter(
([key]) => styles.getPropertyValue(key) && !excludes.includes(key),
([key]) => {
// 将驼峰转换为短横线格式
const kebabKey = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)
return styles.getPropertyValue(kebabKey) && !excludes.includes(key)
},
)
.map(([key, value]) => `${key}:${value};`)
.map(([key, value]) => {
// 将驼峰转换为短横线格式
const kebabKey = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)
return `${kebabKey}:${value};`
})
.join(``)
}