# 代码高亮
使用 prism.js 来实现代码高亮。
每次渲染完 md 文档过后,调用 Prism.hightAll ()
# 安装
|  | npm install prismjs@1.29.0 @types/prismjs@1.26.0 | 
# 配置
|  |  | 
|  | vite: {   | 
|  |   plugins: [   | 
|  |     prismjs({   | 
|  |        | 
|  |       languages: ['cpp',   | 
|  |         'javascript',   | 
|  |         'bash',   | 
|  |         'dart',   | 
|  |         'sql',   | 
|  |         'css',   | 
|  |         'html',   | 
|  |         'java',   | 
|  |         'json',   | 
|  |         'sass',   | 
|  |         'scss',   | 
|  |         'c',   | 
|  |         'log',   | 
|  |         'swift',   | 
|  |         'md',   | 
|  |         'nginx',   | 
|  |         'yaml',   | 
|  |         'xml',   | 
|  |         'shell',   | 
|  |         'ts'   | 
|  |       ],   | 
|  |        | 
|  |        | 
|  |        | 
|  |       plugins: ['line-numbers'],   | 
|  |        | 
|  |       theme: "okaidia",   | 
|  |       css: true   | 
|  |     })   | 
|  |   ]   | 
|  | }, | 
# 使用
|  | import Prism from 'prismjs' | 
|  | ... | 
|  |  | 
|  |  | 
|  | Prism.highlightAll() | 
|  | ... | 
# 引用归纳
将文章中插入的链接,以引用文献的样子放在文章最后。 这个想法来自于阮一峰的周刊。
前端由于浏览器跨域的限制,是无法根据 url 去获取到网站标题的,(不知道 wasm 是否可以)。所以需要一个后端的简单接口,使用 go 或者 python 都可以,我用的 gin 写了个接口
|  | func GetWebsiteTitle(c *gin.Context) {   | 
|  |    url := c.Query("url")   | 
|  |    | 
|  |    if url == "" {   | 
|  |       c.JSON(http.StatusBadRequest, APIResponse{   | 
|  |          ErrorCode:    http.StatusBadRequest,   | 
|  |          ErrorMessage: "URL parameter is missing",   | 
|  |          Result:       "",   | 
|  |       })   | 
|  |       return   | 
|  |    }   | 
|  |    | 
|  |     | 
|  |    collector := colly.NewCollector()   | 
|  |    collector.SetRequestTimeout(1 * time.Second)   | 
|  |    var title string   | 
|  |    | 
|  |    collector.OnHTML("title", func(e *colly.HTMLElement) {   | 
|  |       title = e.Text   | 
|  |    })   | 
|  |    | 
|  |    err := collector.Visit(url)   | 
|  |    if err != nil {   | 
|  |       c.JSON(http.StatusOK, APIResponse{   | 
|  |          ErrorCode:    http.StatusInternalServerError,   | 
|  |          ErrorMessage: "Failed to fetch website data",   | 
|  |          Result:       "",   | 
|  |       })   | 
|  |       return   | 
|  |    }   | 
|  |    | 
|  |    c.JSON(http.StatusOK, APIResponse{   | 
|  |       ErrorCode:    http.StatusOK,   | 
|  |       ErrorMessage: "",   | 
|  |       Result:       title,   | 
|  |    })   | 
|  | } | 
然后在 nuxt 中请求数据方法是 useFetch ,获取所有 url 的标题
|  | import {useFetch} from "#app"; | 
|  |  | 
|  | const { data, pending, error, refresh } = await useFetch("http://localhost:7777/md/get_website_title",{   | 
|  |    query:{   | 
|  |      url:links[i].href   | 
|  |    }   | 
|  |  }) | 
|  |  console.log(data.value.result) | 
# 问题
- 微信样式问题
 发现显示的内容粘贴到微信之后,还能保持该有的样式,发表出去之后,样式反而丢了,很蛋疼
- 引用列表显示的还不是很完美,需要再搞搞