Skip to content

Commit 150435e

Browse files
authored
fix: code style lost in downloaded html file (#73)
* fix: color of the html export button in dark mode * fix: code style lost in downloaded html file * fix: adjust for which span includes code * fix #71
1 parent 544483c commit 150435e

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/assets/less/theme.less

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
::v-deep .el-icon-upload,
122122
.el-icon-download,
123123
.el-icon-refresh,
124-
.el-icon-s-grid {
124+
.el-icon-s-grid,
125+
.el-icon-document {
125126
color: @nightWhiteColor;
126127
}
127128

src/assets/scripts/util.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,12 @@ export function downloadMD(doc) {
242242

243243
/**
244244
* 导出 HTML 生成内容
245-
* @param {HTML生成内容} htmlStr
246245
*/
247-
export function exportHTML(htmlStr) {
246+
export function exportHTML() {
247+
const element = document.querySelector("#output");
248+
setStyles(element);
249+
const htmlStr = element.innerHTML;
250+
248251
const downLink = document.createElement("a");
249252

250253
downLink.download = "content.html";
@@ -257,6 +260,50 @@ export function exportHTML(htmlStr) {
257260
document.body.appendChild(downLink);
258261
downLink.click();
259262
document.body.removeChild(downLink);
263+
264+
function setStyles(element) {
265+
switch (true) {
266+
case isSection(element):
267+
case isPre(element):
268+
case isCode(element):
269+
case isSpan(element):
270+
element.setAttribute("style", getElementStyles(element));
271+
default:
272+
}
273+
if (element.children.length) {
274+
Array.from(element.children).forEach((child) => setStyles(child));
275+
}
276+
277+
// 判断是否是包裹代码块的 section 元素
278+
function isSection(element) {
279+
return (
280+
element.tagName === "SECTION" &&
281+
Array.from(element.classList).includes("code-snippet__github")
282+
);
283+
}
284+
// 判断是否是包裹代码块的 pre 元素
285+
function isPre(element) {
286+
return (
287+
element.tagName === "PRE" &&
288+
Array.from(element.classList).includes("code__pre")
289+
);
290+
}
291+
// 判断是否是包裹代码块的 code 元素
292+
function isCode(element) {
293+
return (
294+
element.tagName === "CODE" &&
295+
Array.from(element.classList).includes("prettyprint")
296+
);
297+
}
298+
// 判断是否是包裹代码字符的 span 元素
299+
function isSpan(element) {
300+
return (
301+
element.tagName === "SPAN" &&
302+
(isCode(element.parentElement) ||
303+
isCode(element.parentElement.parentElement))
304+
);
305+
}
306+
}
260307
}
261308

262309
/**
@@ -313,3 +360,17 @@ export function checkImage(file) {
313360
}
314361
return { ok: true };
315362
}
363+
364+
/**
365+
* 获取一个 DOM 元素的所有样式,
366+
* @param {DOM 元素} element DOM 元素
367+
* @param {排除的属性} excludes 如果某些属性对结果有不良影响,可以使用这个参数来排除
368+
* @returns 行内样式拼接结果
369+
*/
370+
function getElementStyles(element, excludes = ["width", "height"]) {
371+
const styles = getComputedStyle(element, null);
372+
return Object.entries(styles)
373+
.filter(([key]) => styles.getPropertyValue(key) && !excludes.includes(key))
374+
.map(([key, value]) => `${key}:${value};`)
375+
.join("");
376+
}

src/pages/index/view/CodemirrorEditor.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,12 @@ export default {
334334
},
335335
// 导出编辑器内容为 HTML,并且下载到本地
336336
exportEditorContent() {
337-
exportHTML(this.output);
337+
338+
339+
this.$nextTick(() => {
340+
exportHTML();
341+
})
342+
338343
},
339344
// 格式化文档
340345
formatContent() {

0 commit comments

Comments
 (0)