Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bead9472
authored
Jul 24, 2024
by
cherishsince
Committed by
YunaiV
Aug 02, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【优化】marked 替换成 markdown-it
parent
fa59b3c1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
34 deletions
+16
-34
src/components/MarkdownView/index.vue
+16
-34
No files found.
src/components/MarkdownView/index.vue
View file @
bead9472
<
template
>
<
template
>
<div
ref=
"contentRef"
class=
"markdown-view"
v-html=
"contentHtml"
></div>
<!--
<div
ref=
"contentRef"
class=
"markdown-view"
v-html=
"contentHtml"
></div>
-->
<div
ref=
"contentRef"
class=
"markdown-view"
v-html=
"renderedMarkdown"
></div>
</
template
>
</
template
>
<
script
setup
lang=
"ts"
>
<
script
setup
lang=
"ts"
>
import
{
useClipboard
}
from
'@vueuse/core'
import
{
useClipboard
}
from
'@vueuse/core'
import
{
marked
}
from
'marked
'
import
MarkdownIt
from
'markdown-it
'
import
'highlight.js/styles/vs2015.min.css'
import
'highlight.js/styles/vs2015.min.css'
import
hljs
from
'highlight.js'
import
hljs
from
'highlight.js'
...
@@ -19,45 +20,26 @@ const props = defineProps({
...
@@ -19,45 +20,26 @@ const props = defineProps({
const
message
=
useMessage
()
// 消息弹窗
const
message
=
useMessage
()
// 消息弹窗
const
{
copy
}
=
useClipboard
()
// 初始化 copy 到粘贴板
const
{
copy
}
=
useClipboard
()
// 初始化 copy 到粘贴板
const
contentRef
=
ref
()
const
contentRef
=
ref
()
const
contentHtml
=
ref
<
any
>
()
// 渲染的html内容
const
{
content
}
=
toRefs
(
props
)
// 将 props 变为引用类型
const
{
content
}
=
toRefs
(
props
)
// 将 props 变为引用类型
// 代码高亮:https://highlightjs.org/
const
md
=
new
MarkdownIt
({
// 转换 markdown:marked
highlight
:
function
(
str
,
lang
)
{
if
(
lang
&&
hljs
.
getLanguage
(
lang
))
{
/** marked 渲染器 */
try
{
const
renderer
=
{
const
copyHtml
=
`<div id="copy" data-copy='
${
str
}
' style="position: absolute; right: 10px; top: 5px; color: #fff;cursor: pointer;">复制</div>`
code
(
code
,
language
,
c
)
{
return
`<pre style="position: relative;">
${
copyHtml
}
<code class="hljs">
${
hljs
.
highlight
(
lang
,
str
,
true
).
value
}
</code></pre>`
let
highlightHtml
}
catch
(
__
)
{}
try
{
highlightHtml
=
hljs
.
highlight
(
code
,
{
language
:
language
,
ignoreIllegals
:
true
}).
value
}
catch
(
e
)
{
// skip
}
}
const
copyHtml
=
`<div id="copy" data-copy='
${
code
}
' style="position: absolute; right: 10px; top: 5px; color: #fff;cursor: pointer;">复制</div>`
return
``
return
`<pre style="position: relative;">
${
copyHtml
}
<code class="hljs">
${
highlightHtml
}
</code></pre>`
}
}
}
});
// 配置 marked
marked
.
use
({
renderer
:
renderer
})
/** 监听 content 变化 */
const
renderedMarkdown
=
computed
(()
=>
{
watch
(
content
,
async
(
newValue
,
oldValue
)
=>
{
return
md
.
render
(
props
.
content
);
await
renderMarkdown
(
newValue
)
});
})
/** 渲染 markdown */
const
renderMarkdown
=
async
(
content
:
string
)
=>
{
contentHtml
.
value
=
await
marked
(
content
)
}
/** 初始化 **/
/** 初始化 **/
onMounted
(
async
()
=>
{
onMounted
(
async
()
=>
{
// 解析转换 markdown
await
renderMarkdown
(
props
.
content
as
string
)
// 添加 copy 监听
// 添加 copy 监听
contentRef
.
value
.
addEventListener
(
'click'
,
(
e
:
any
)
=>
{
contentRef
.
value
.
addEventListener
(
'click'
,
(
e
:
any
)
=>
{
if
(
e
.
target
.
id
===
'copy'
)
{
if
(
e
.
target
.
id
===
'copy'
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment