fix(@vant/cli): added markdown link open plugin (#9597)

This commit is contained in:
neverland
2021-09-30 10:45:55 +08:00
committed by GitHub
parent 6a054ca0e3
commit 1457a27fb4
4 changed files with 61 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ import {
} from '../common/constant';
import { injectHtml } from 'vite-plugin-html';
import type { InlineConfig } from 'vite';
import type MarkdownIt from 'markdown-it';
function markdownHighlight(str: string, lang: string) {
if (lang && hljs.getLanguage(lang)) {
@@ -40,6 +41,24 @@ function markdownCardWrapper(htmlCode: string) {
.join('');
}
// add target="_blank" to all links
function markdownLinkOpen(md: MarkdownIt) {
const defaultRender =
md.renderer.rules.link_open ||
((tokens, idx, options, env, self) =>
self.renderToken(tokens, idx, options));
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const aIndex = tokens[idx].attrIndex('target');
if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank']); // add new attribute
}
return defaultRender(tokens, idx, options, env, self);
};
}
function getSiteConfig(vantConfig: any) {
const siteConfig = vantConfig.site;
@@ -96,9 +115,12 @@ export function getViteConfigForSiteDev(): InlineConfig {
markdownItOptions: {
highlight: markdownHighlight,
},
markdownItSetup(md: any) {
markdownItSetup(md: MarkdownIt) {
const { slugify } = require('transliteration');
const markdownItAnchor = require('markdown-it-anchor');
markdownLinkOpen(md);
md.use(markdownItAnchor, {
level: 2,
slugify,