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

pull/1244/head
meteorlxy 3 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 path: string
relativePath: string relativePath: string
cleanUrls: CleanUrlsMode cleanUrls: CleanUrlsMode
links?: string[]
} }

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

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

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

Loading…
Cancel
Save