2024-03-01 18:30:20 +08:00
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
|
/*::v-dedp .markdown-body pre{
|
|
|
|
|
|
padding: 0 !important;
|
|
|
|
|
|
}*/
|
|
|
|
|
|
::v-deep .pre-out
|
2024-02-29 19:07:48 +08:00
|
|
|
|
{
|
2024-03-01 18:30:20 +08:00
|
|
|
|
padding: 0;
|
|
|
|
|
|
overflow-x: hidden;
|
2024-02-29 19:07:48 +08:00
|
|
|
|
}
|
2024-03-01 18:30:20 +08:00
|
|
|
|
::v-deep .pre {
|
|
|
|
|
|
padding: 0;
|
|
|
|
|
|
margin-bottom: 0;
|
|
|
|
|
|
overflow-x: hidden;
|
|
|
|
|
|
.header {
|
|
|
|
|
|
background-color: #409eff;
|
|
|
|
|
|
color: white;
|
|
|
|
|
|
height: 30px;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
padding-top: 10px;
|
2024-02-29 19:07:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-03-01 18:30:20 +08:00
|
|
|
|
.language {}
|
|
|
|
|
|
.copy:hover {
|
|
|
|
|
|
cursor: pointer;
|
2024-02-29 19:07:48 +08:00
|
|
|
|
}
|
2024-03-01 18:30:20 +08:00
|
|
|
|
.copy {
|
|
|
|
|
|
margin: 0px 10px;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.code-con {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
|
|
|
|
|
|
.nav {
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
background-color: #282C34;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.code {
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
padding: 10px 10px;
|
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
line-height: 22px;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
overflow-x: auto;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-29 19:07:48 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-01 18:30:20 +08:00
|
|
|
|
::v-deep .nav-ul {
|
|
|
|
|
|
border-right: 1px solid #FFFFFF;
|
|
|
|
|
|
margin-top: 12px;
|
|
|
|
|
|
padding-left: 10px;
|
|
|
|
|
|
padding-right: 2px;
|
|
|
|
|
|
|
|
|
|
|
|
.nav-li {
|
|
|
|
|
|
margin: 5.3px 0;
|
|
|
|
|
|
text-align: right;
|
|
|
|
|
|
margin-right: 3px;
|
|
|
|
|
|
}
|
2024-02-29 19:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
</style>
|
2023-12-14 10:15:23 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div class="markdown-body" v-html="outputHtml"></div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
|
import { marked } from 'marked';
|
|
|
|
|
|
|
|
|
|
|
|
import hljs from "highlight.js";
|
|
|
|
|
|
//可以设置加载样式切换主题
|
2024-02-29 19:07:48 +08:00
|
|
|
|
import '@/assets/atom-one-dark.css'
|
2023-12-14 10:15:23 +08:00
|
|
|
|
import '@/assets/github-markdown.css'
|
2024-03-01 18:30:20 +08:00
|
|
|
|
import {nextTick , ref, watch } from 'vue';
|
|
|
|
|
|
const BREAK_LINE_REGEXP = /\r\n|\r|\n/g;
|
2023-12-14 10:15:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-02-29 19:07:48 +08:00
|
|
|
|
const outputHtml = ref("")
|
2023-12-14 10:15:23 +08:00
|
|
|
|
const props = defineProps(['code'])
|
2024-03-01 18:30:20 +08:00
|
|
|
|
let codeCopyDic=[];
|
|
|
|
|
|
const addCopyEvent=()=>{
|
|
|
|
|
|
const copySpans = document.querySelectorAll('.copy');
|
|
|
|
|
|
console.log(copySpans,"copySpans");
|
|
|
|
|
|
// 为每个 copy span 元素添加点击事件
|
|
|
|
|
|
copySpans.forEach(span => {
|
|
|
|
|
|
|
|
|
|
|
|
span.addEventListener('click', async function() {
|
|
|
|
|
|
await navigator.clipboard.writeText(codeCopyDic.filter(x=>x.id==span.id)[0].code );
|
|
|
|
|
|
ElMessage({
|
|
|
|
|
|
message: "代码块复制成功",
|
|
|
|
|
|
type: "success",
|
|
|
|
|
|
duration: 2000,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//code部分处理、高亮
|
|
|
|
|
|
const codeHandler = (code, language) => {
|
2024-02-29 19:07:48 +08:00
|
|
|
|
const codeIndex = parseInt(Date.now() + "") + Math.floor(Math.random() * 10000000);
|
|
|
|
|
|
// 格式化第一行是右侧language和 “复制” 按钮;
|
|
|
|
|
|
if (code) {
|
2024-03-01 18:30:20 +08:00
|
|
|
|
const navCode = navHandler(code)
|
2024-02-29 19:07:48 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 使用 highlight.js 对代码进行高亮显示
|
2024-03-01 18:30:20 +08:00
|
|
|
|
const preCode = hljs.highlightAuto(code).value;
|
2024-02-29 19:07:48 +08:00
|
|
|
|
// 将代码包裹在 textarea 中,由于防止textarea渲染出现问题,这里将 "<" 用 "<" 代替,不影响复制功能
|
2024-03-01 18:30:20 +08:00
|
|
|
|
let html = `<pre class='hljs pre'><div class="header"><span class="language">${language}</span><span class="copy" id="${codeIndex}">复制代码</span></div><div class="code-con"><div class="nav">${navCode}</div><code class="code">${preCode}</code></div></pre>`;
|
|
|
|
|
|
codeCopyDic.push({id: codeIndex,code:code});
|
|
|
|
|
|
console.log(codeCopyDic.length);
|
2024-02-29 19:07:48 +08:00
|
|
|
|
return html;
|
2024-03-01 18:30:20 +08:00
|
|
|
|
//<textarea style="position: absolute;top: -9999px;left: -9999px;z-index: -9999;" id="copy${codeIndex}">${code.replace(/<\/textarea>/g, "</textarea>")}</textarea>
|
2024-02-29 19:07:48 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-03-01 18:30:20 +08:00
|
|
|
|
//左侧导航栏处理
|
|
|
|
|
|
const navHandler = (code) => {
|
|
|
|
|
|
//获取行数
|
|
|
|
|
|
var linesCount = getLinesCount(code);
|
|
|
|
|
|
|
|
|
|
|
|
var currentLine = 1;
|
|
|
|
|
|
var liHtml = ``;
|
|
|
|
|
|
while (linesCount + 1 >= currentLine) {
|
|
|
|
|
|
liHtml += `<li class="nav-li">${currentLine}</li>`
|
|
|
|
|
|
currentLine++
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let html = `<ul class="nav-ul">${liHtml}</ul>`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return html;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getLinesCount = (text) => {
|
|
|
|
|
|
return (text.trim().match(BREAK_LINE_REGEXP) || []).length;
|
|
|
|
|
|
}
|
2024-02-29 19:07:48 +08:00
|
|
|
|
|
|
|
|
|
|
watch(props, (n, o) => {
|
2024-03-01 18:30:20 +08:00
|
|
|
|
codeCopyDic=[];
|
2023-12-14 10:15:23 +08:00
|
|
|
|
marked.setOptions({
|
|
|
|
|
|
renderer: new marked.Renderer(),
|
2024-03-01 18:30:20 +08:00
|
|
|
|
highlight: function (code, language) {
|
|
|
|
|
|
return codeHandler(code, language);
|
|
|
|
|
|
// return hljs.highlightAuto(code).value;
|
2024-02-29 19:07:48 +08:00
|
|
|
|
},
|
2023-12-14 10:15:23 +08:00
|
|
|
|
pedantic: false,
|
|
|
|
|
|
gfm: true,//允许 Git Hub标准的markdown
|
|
|
|
|
|
tables: true,//支持表格
|
2024-02-29 19:07:48 +08:00
|
|
|
|
|
2023-12-14 10:15:23 +08:00
|
|
|
|
breaks: true,
|
|
|
|
|
|
sanitize: false,
|
|
|
|
|
|
smartLists: true,
|
|
|
|
|
|
smartypants: false,
|
|
|
|
|
|
xhtml: false,
|
|
|
|
|
|
smartLists: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
//需要注意代码块样式
|
2024-03-01 18:30:20 +08:00
|
|
|
|
const soureHtml = marked(n.code);
|
|
|
|
|
|
outputHtml.value= soureHtml.replace(/<pre>/g, '<pre class="pre-out">');
|
|
|
|
|
|
nextTick(()=>{
|
|
|
|
|
|
addCopyEvent();
|
|
|
|
|
|
})
|
2024-02-29 19:07:48 +08:00
|
|
|
|
}, { immediate: true, deep: true })
|
2023-12-14 10:15:23 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|