Skip to content

Commit c4f3194

Browse files
committed
feat:#1 更好Markdown为Lute并使用前端渲染
1 parent d194704 commit c4f3194

File tree

10 files changed

+147
-31
lines changed

10 files changed

+147
-31
lines changed

lib/markdownUtil.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1-
import Showdown from "showdown"
2-
3-
const converter = new Showdown.Converter({tables: true});
1+
// import Showdown from "showdown"
2+
// const converter = new Showdown.Converter({tables: true});
43

54
/**
65
* 渲染Markdown
76
* @param md
87
*/
98
export function renderHTML(md: string) {
10-
return converter.makeHtml(md);
9+
// @ts-ignore
10+
const lute = Lute.New()
11+
// const renderers = {
12+
// renderText: (node:any, entering:any) => {
13+
// if (entering) {
14+
// console.log(" render text")
15+
// // @ts-ignore
16+
// return [node.Text() + " via Lute", Lute.WalkContinue]
17+
// }
18+
// // @ts-ignore
19+
// return ["", Lute.WalkContinue]
20+
// },
21+
// renderStrong: (node:any, entering:any) => {
22+
// entering ? console.log(" start render strong") : console.log(" end render strong")
23+
// // @ts-ignore
24+
// return ["", Lute.WalkContinue]
25+
// },
26+
// renderParagraph: (node:any, entering:any) => {
27+
// entering ? console.log(" start render paragraph") : console.log(" end render paragraph")
28+
// // @ts-ignore
29+
// return ["", Lute.WalkContinue]
30+
// }
31+
// }
32+
// lute.SetJSRenderers({
33+
// renderers: {
34+
// Md2HTML: renderers
35+
// },
36+
// })
37+
// return converter.makeHtml(md);
38+
return lute.MarkdownStr("", md)
1139
}

lib/platform/metaweblog/metaweblogApi.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ export class MetaWeblogApi {
146146
const postStruct = ret.params.param.value || []
147147
const post = this.parsePost(postStruct)
148148

149-
let htmlContent = renderHTML(post.description)
150-
htmlContent = prettyHtml(htmlContent)
151-
post.description = htmlContent
149+
// let htmlContent = renderHTML(post.description)
150+
// htmlContent = prettyHtml(htmlContent)
151+
// post.description = htmlContent
152152

153153
return Promise.resolve(post)
154154
}

lib/platform/siyuan/siYuanApiAdaptor.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ export class SiYuanApiAdaptor implements IApi {
106106
const shortDesc = attrs["custom-desc"] || ""
107107

108108
// 渲染Markdown
109-
let htmlContent = renderHTML(md.content)
110-
htmlContent = prettyHtml(htmlContent)
109+
// let htmlContent = renderHTML(md.content)
110+
// htmlContent = prettyHtml(htmlContent)
111+
let mdContent = md.content
111112

112113
let title = siyuanPost.content || ""
113114
title = removeTitleNumber(title)
@@ -116,7 +117,7 @@ export class SiYuanApiAdaptor implements IApi {
116117
let commonPost = new Post()
117118
commonPost.postid = siyuanPost.root_id || ""
118119
commonPost.title = title
119-
commonPost.description = htmlContent || ""
120+
commonPost.description = mdContent || ""
120121
commonPost.shortDesc = shortDesc || ""
121122
commonPost.mt_keywords = attrs.tags || ""
122123
commonPost.isPublished = isPublished

lib/strUtil.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @function unescapeHTML 还原html脚本 < > & " '
3+
* @param a -
4+
* 字符串
5+
*/
6+
export const unescapeHTML = function (a: string) {
7+
a = "" + a;
8+
return a.replace(/\&amp;/g, "&").replace(/\&lt;/g, "<")
9+
.replace(/\&gt;/g, ">")
10+
}

nuxt.config.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import {defineNuxtConfig} from 'nuxt'
1+
import {defineNuxtConfig} from 'nuxt/config'
22
import ElementPlus from 'unplugin-element-plus/vite'
33

44
// https://v3.nuxtjs.org/api/configuration/nuxt.config
55
export default defineNuxtConfig({
6+
app: {
7+
head: {
8+
script: [
9+
{
10+
src: "/lute.min.js",
11+
},
12+
],
13+
},
14+
},
15+
16+
617
// meta
718
meta: {
819
title: 'Element Plus + Nuxt 3',

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111
"devDependencies": {
1212
"@pinia/nuxt": "^0.4.3",
13-
"@types/showdown": "^2.0.0",
1413
"@types/xmlrpc": "^1.3.7",
1514
"@unocss/nuxt": "^0.45.29",
1615
"@vueuse/nuxt": "^9.3.0",
@@ -31,7 +30,6 @@
3130
"element-plus": "^2.2.15",
3231
"highlight.js": "^11.6.0",
3332
"pinia": "^2.0.23",
34-
"showdown": "^2.1.0",
3533
"vue": "^3.2.40",
3634
"xmlrpc": "^1.3.2"
3735
}

plugins/hljs/codecopy/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @copyright Copyright 2021. All rights reserved.
55
*/
66

7+
import {unescapeHTML} from "~/lib/strUtil";
8+
79
/**
810
* Adds a copy button to highlightjs code blocks
911
*/
@@ -40,7 +42,7 @@ export class CopyButtonPlugin {
4042
button.onclick = function () {
4143
if (!navigator.clipboard) return;
4244

43-
let newText = text;
45+
let newText = unescapeHTML(text);
4446
// @ts-ignore
4547
// eslint-disable-next-line no-undef
4648
// if (hook && typeof hook === "function") {

plugins/hljs/vue-hljs/main.js

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Hljs from "highlight.js";
22
import {CopyButtonPlugin} from "../codecopy";
33
import "../codecopy/codecopy.css";
44
import "./vs.css";
5+
import {renderHTML} from "~/lib/markdownUtil";
6+
import {unescapeHTML} from "~/lib/strUtil";
7+
import logUtil from "~/lib/logUtil";
58

69
const vueHljs = {};
710

@@ -12,9 +15,25 @@ vueHljs.install = Vue => {
1215
);
1316

1417
Vue.directive("highlight", el => {
18+
const html = renderHTML(el.innerHTML)
19+
// console.log(html)
20+
el.innerHTML = html;
21+
1522
const blocks = el.querySelectorAll("pre code");
1623
Array.prototype.forEach.call(blocks, Hljs.highlightBlock);
1724

25+
// 取消转义
26+
Array.prototype.forEach.call(blocks, function (bel) {
27+
const bclass = bel.getAttribute("class");
28+
console.log(bclass)
29+
let newHtml = bel.innerHTML
30+
// console.log(newHtml)
31+
32+
newHtml = unescapeHTML(newHtml)
33+
bel.innerHTML = newHtml
34+
// console.log(newHtml)
35+
})
36+
1837
// 代码选项卡
1938
// 代码块
2039
const codeGroups = el.querySelectorAll("code-group");

public/lute.min.js

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yarn.lock

-17
Original file line numberDiff line numberDiff line change
@@ -759,11 +759,6 @@
759759
dependencies:
760760
"@types/node" "*"
761761

762-
"@types/showdown@^2.0.0":
763-
version "2.0.0"
764-
resolved "https://registry.npmmirror.com/@types/showdown/-/showdown-2.0.0.tgz#3e800eca8573848cac4e5555f4377ba3a0e7b1f2"
765-
integrity sha512-70xBJoLv+oXjB5PhtA8vo7erjLDp9/qqI63SRHm4REKrwuPOLs8HhXwlZJBJaB4kC18cCZ1UUZ6Fb/PLFW4TCA==
766-
767762
"@types/web-bluetooth@^0.0.15":
768763
version "0.0.15"
769764
resolved "https://registry.npmmirror.com/@types/web-bluetooth/-/web-bluetooth-0.0.15.tgz#d60330046a6ed8a13b4a53df3813c44942ebdf72"
@@ -1636,11 +1631,6 @@ commander@^8.0.0:
16361631
resolved "https://registry.npmmirror.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
16371632
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==
16381633

1639-
commander@^9.0.0:
1640-
version "9.4.0"
1641-
resolved "https://registry.npmmirror.com/commander/-/commander-9.4.0.tgz#bc4a40918fefe52e22450c111ecd6b7acce6f11c"
1642-
integrity sha512-sRPT+umqkz90UA8M1yqYfnHlZA7fF6nSphDtxeywPZ49ysjxDQybzk13CL+mXekDRG92skbcqCLVovuCusNmFw==
1643-
16441634
commondir@^1.0.1:
16451635
version "1.0.1"
16461636
resolved "https://registry.npmmirror.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
@@ -4180,13 +4170,6 @@ shebang-regex@^3.0.0:
41804170
resolved "https://registry.npmmirror.com/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
41814171
integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
41824172

4183-
showdown@^2.1.0:
4184-
version "2.1.0"
4185-
resolved "https://registry.npmmirror.com/showdown/-/showdown-2.1.0.tgz#1251f5ed8f773f0c0c7bfc8e6fd23581f9e545c5"
4186-
integrity sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==
4187-
dependencies:
4188-
commander "^9.0.0"
4189-
41904173
signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
41914174
version "3.0.7"
41924175
resolved "https://registry.npmmirror.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"

0 commit comments

Comments
 (0)