Vue配置marked連接添加target="_blank"

1. 引入marked

package.jsonjavascript

"dependencies": {
    "marked": "^0.3.9"
}
import marked from "marked";

2. 修改生成的HTML的方法

const renderer = new marked.Renderer();
//https://github.com/markedjs/marked/issues/655#issuecomment-383226346
const linkRenderer = renderer.link;
renderer.link = (href, title, text) => {
    const html = linkRenderer.call(renderer, href, title, text);
    return html.replace(/^<a /, '<a target="_blank" rel="nofollow" ');
};
const html = marked(markdown, { renderer });
Tips:marked參數配置
const renderer = new marked.Renderer();
//基本設置
marked.setOptions({
    renderer: renderer,
    gfm: true, //容許 Git Hub標準的markdown.
    tables: true, //容許支持表格語法(該選項要求 gfm 爲true)
    breaks: true, //容許回車換行(該選項要求 gfm 爲true)
    pedantic: false, //不糾正原始模型任何的不良行爲和錯誤(默認爲false)
    sanitize: false, //對輸出進行過濾(清理),將忽略任何已經輸入的html代碼(標籤)
    smartLists: true, //使用比原生markdown更時髦的列表
    smartypants: false //使用更爲時髦的標點
});
相關文章
相關標籤/搜索