使用语法高亮 Prism.js 让你写的代码美化起来(以Vue3为例)
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
1.前台展示tinymce编辑器编辑的内容时踩的坑 2.安装PrimsJS代码高亮库 3.解决PrimsJS添加后代码不高亮的问题 4.给代码块添加行号 5.PrimsJS代码高亮的原理 一、前台展示tinymce编辑器编辑的内容时踩的坑1. <template> 2. <div class="article-wrapper"> 3. <template v-for="item in articleInfoData"> 4. <h2 class="title">{{ item.title }}</h2> 5. <p class="desc" v-if="item.desc">摘要:{{ item.desc }}</p> 6. <div class="content" v-html="item.content"></div> 7. <p class="date">{{ item.created_at }}</p> 8. </template> 9. </div> 10.</template> 二、安装Prismjs代码高亮库1. //安装 2. npm install prismjs 1. //引入Prism(我们只需要在局部引入即可,我这里是在文章内容展示组件中引入) 2. <script setup lang="ts"> 3. import { ref,
reactive, onMounted } from 'vue' 4. import type {Ref } from 'vue' 5. import {
useRouter, useRoute } from 'vue-router' 6. import { api
} from "@/assets/config/api" 7. import {
request } from "@/assets/common/request" 8. import Prism from "prismjs"//导入代码高亮插件的core(里面提供了其他官方插件及代码高亮样式主题,你只需要引入即可) 9. import "prismjs/themes/prism-tomorrow.min.css"//引入代码高亮主题(这个去node_modules的安装prismjs中找到想使用的主题即可) 10. 11.const router = useRouter() 12.const route = useRoute() 13.const articleInfoData: Ref<Array<object>> = ref([]); 14. 15.onMounted(() => { 16. getArticleInfo() 17. Prism.highlightAll()// 全局代码高亮 18.}) 19.function getArticleInfo(): void {//从后台请求数据 20.
request.get(api.GetArticleInfo,
{ 21. id: route.params.id 22.
}).then(res => { 23.
articleInfoData.value = res?.data?.data 24.
}).catch(err => { 25. console.log(err) 26.
}) 27.
}) 28. 29.} 30.</script> 三、解决PrimsJS添加后代码不高亮的问题1. onMounted(() => { 2. getArticleInfo() 3. setTimeout(() => { 4. Prism.highlightAll()// 全局代码高亮 5. }, 100) 6. }) 1. <script setup lang="ts"> 2. import { ref,
reactive, onMounted } from 'vue' 3. import type {Ref } from 'vue' 4. import {
useRouter, useRoute } from 'vue-router' 5. import { api
} from "@/assets/config/api" 6. import {
request } from "@/assets/common/request" 7. import Prism from "prismjs"//代码高亮插件的core 8. import "prismjs/themes/prism-tomorrow.min.css"//高亮主题 9. const router
= useRouter() 10.const route = useRoute() 11.const articleInfoData: Ref<Array<object>> = ref([]); 12.onMounted(async () => { 13. await getArticleInfo().then(res => { 14.
articleInfoData.value = res 15.
}).catch(err => { 16. console.log(err); 17.
}) 18. Prism.highlightAll()// 全局代码高亮(必须等获取数据之后,代码高亮才能生效,也可以用定时器定时) 19.}) 20.function getArticleInfo(): Promise<Array<object>> {//从后台请求数据 21. return new Promise((resolve, reject) => { 22.
request.get(api.GetArticleInfo,
{ 23. id: route.params.id 24.
}).then(res => { 25. resolve(res?.data?.data) 26.
}).catch(err => { 27. reject(err) 28.
}) 29.
}) 30.} 31.</script> 四、给代码块添加行号1. <script setup lang="ts"> 2. import { ref,
reactive, onMounted } from 'vue' 3. import type {Ref } from 'vue' 4. import {
useRouter, useRoute } from 'vue-router' 5. import { api
} from "@/assets/config/api" 6. import {
request } from "@/assets/common/request" 7. import Prism from "prismjs"//代码高亮core 8. import "prismjs/plugins/line-numbers/prism-line-numbers.min.js"//行号插件 9. import "prismjs/themes/prism-tomorrow.min.css"//高亮主题 10.import "prismjs/plugins/line-numbers/prism-line-numbers.min.css"//行号插件的样式 11.const router = useRouter() 12.const route = useRoute() 13.const articleInfoData: Ref<Array<object>> = ref([]); 14.onMounted(async () => { 15. await getArticleInfo().then(res => { 16.
articleInfoData.value = res 17.
}).catch(err => { 18. console.log(err); 19.
}) 20. Prism.highlightAll()// 全局代码高亮(必须等获取数据之后,代码高亮才能生效,也可以用定时器定时) 21.}) 22.function getArticleInfo(): Promise<Array<object>> {//从后台请求数据 23. return new Promise((resolve, reject) => { 24.
request.get(api.GetArticleInfo,
{ 25. id: route.params.id 26.
}).then(res => { 27.
resolve(res?.data?.data) 28.
}).catch(err => { 29. reject(err) 30.
}) 31.
}) 32.} 33.</script> 1. <template> 2. <div class="article-wrapper"> 3. <template v-for="item in articleInfoData"> 4. <h3 class="title">{{ item.title }}</h3> 5. <p class="desc" v-if="item.desc">摘要:{{ item.desc }}</p> 6. <section class="author"> 7. <span>作者:三叶雨</span> 8. <span>{{ item.created_at }}</span> 9. </section> 10. <div class="content line-numbers" v-html="item.content"></div> 11.
</template> 12.
</div> 13.</template> 五、Prismjs代码高亮的实现原理该文章在 2024/3/29 10:17:23 编辑过 |
关键字查询
相关文章
正在查询... |