refactor: remove non-standard markdown-it extensions (#1240)

pull/1244/head
meteorlxy 2 years ago committed by GitHub
parent 3579883058
commit 45743cb2e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,4 +5,5 @@ export interface MarkdownEnv extends MarkdownItEnv {
path: string
relativePath: string
cleanUrls: CleanUrlsMode
links?: string[]
}

@ -48,15 +48,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
externalLinks?: Record<string, string>
}
export interface MarkdownParsedData {
links?: string[]
}
export interface MarkdownRenderer extends MarkdownIt {
__path: string
__relativePath: string
__data: MarkdownParsedData
}
export type MarkdownRenderer = MarkdownIt
export type { Header }
@ -125,12 +117,5 @@ export const createMarkdownRenderer = async (
if (options.lineNumbers) {
md.use(lineNumberPlugin)
}
const originalRender = md.render
md.render = (...args) => {
md.__data = {}
return originalRender.call(md, ...args)
}
return md
}

@ -4,9 +4,8 @@
import MarkdownIt from 'markdown-it'
import type { MarkdownEnv } from '../env'
import type { MarkdownRenderer } from '../markdown'
import { URL } from 'url'
import { EXTERNAL_URL_RE, CleanUrlsMode } from '../../shared'
import { EXTERNAL_URL_RE } from '../../shared'
const indexRE = /(^|.*\/)index.md(#?.*)$/i
@ -34,7 +33,7 @@ export const linkPlugin = (
})
// catch localhost links as dead link
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {
pushLink(url)
pushLink(url, env)
}
} else if (
// internal anchor links
@ -44,7 +43,7 @@ export const linkPlugin = (
// links to files (other than html/md)
!/\.(?!html|md)\w+($|\?)/i.test(url)
) {
normalizeHref(hrefAttr, env.cleanUrls)
normalizeHref(hrefAttr, env)
}
// encode vite-specific replace strings in case they appear in URLs
@ -57,10 +56,7 @@ export const linkPlugin = (
return self.renderToken(tokens, idx, options)
}
function normalizeHref(
hrefAttr: [string, string],
shouldCleanUrls: CleanUrlsMode
) {
function normalizeHref(hrefAttr: [string, string], env: MarkdownEnv) {
let url = hrefAttr[1]
const indexMatch = url.match(indexRE)
@ -73,12 +69,12 @@ export const linkPlugin = (
if (cleanUrl.endsWith('.md')) {
cleanUrl = cleanUrl.replace(
/\.md$/,
shouldCleanUrls === 'disabled' ? '.html' : ''
env.cleanUrls === 'disabled' ? '.html' : ''
)
}
// transform ./foo -> ./foo[.html]
if (
shouldCleanUrls === 'disabled' &&
env.cleanUrls === 'disabled' &&
!cleanUrl.endsWith('.html') &&
!cleanUrl.endsWith('/')
) {
@ -94,7 +90,7 @@ export const linkPlugin = (
}
// export it for existence check
pushLink(url.replace(/\.html$/, ''))
pushLink(url.replace(/\.html$/, ''), env)
// append base to internal (non-relative) urls
if (url.startsWith('/')) {
@ -105,9 +101,8 @@ export const linkPlugin = (
hrefAttr[1] = decodeURI(url)
}
function pushLink(link: string) {
const data = (md as MarkdownRenderer).__data
const links = data.links || (data.links = [])
function pushLink(link: string, env: MarkdownEnv) {
const links = env.links || (env.links = [])
links.push(link)
}
}

@ -73,18 +73,20 @@ export async function createMarkdownToVueRenderFn(
}
})
// reset state before render
md.__path = file
md.__relativePath = relativePath
// reset env before render
const env: MarkdownEnv = {
path: file,
relativePath,
cleanUrls
}
const html = md.render(src, env)
const data = md.__data
const { frontmatter = {}, headers = [], sfcBlocks, title = '' } = env
const {
frontmatter = {},
headers = [],
links = [],
sfcBlocks,
title = ''
} = env
// validate data.links
const deadLinks: string[] = []
@ -101,9 +103,9 @@ export async function createMarkdownToVueRenderFn(
deadLinks.push(url)
}
if (data.links) {
if (links) {
const dir = path.dirname(file)
for (let url of data.links) {
for (let url of links) {
if (/\.(?!html|md)\w+($|\?)/i.test(url)) continue
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {

Loading…
Cancel
Save