Skip to content

Commit 45743cb

Browse files
authored
refactor: remove non-standard markdown-it extensions (#1240)
1 parent 3579883 commit 45743cb

File tree

4 files changed

+21
-38
lines changed

4 files changed

+21
-38
lines changed

src/node/markdown/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export interface MarkdownEnv extends MarkdownItEnv {
55
path: string
66
relativePath: string
77
cleanUrls: CleanUrlsMode
8+
links?: string[]
89
}

src/node/markdown/markdown.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
4848
externalLinks?: Record<string, string>
4949
}
5050

51-
export interface MarkdownParsedData {
52-
links?: string[]
53-
}
54-
55-
export interface MarkdownRenderer extends MarkdownIt {
56-
__path: string
57-
__relativePath: string
58-
__data: MarkdownParsedData
59-
}
51+
export type MarkdownRenderer = MarkdownIt
6052

6153
export type { Header }
6254

@@ -125,12 +117,5 @@ export const createMarkdownRenderer = async (
125117
if (options.lineNumbers) {
126118
md.use(lineNumberPlugin)
127119
}
128-
129-
const originalRender = md.render
130-
md.render = (...args) => {
131-
md.__data = {}
132-
return originalRender.call(md, ...args)
133-
}
134-
135120
return md
136121
}

src/node/markdown/plugins/link.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
import MarkdownIt from 'markdown-it'
66
import type { MarkdownEnv } from '../env'
7-
import type { MarkdownRenderer } from '../markdown'
87
import { URL } from 'url'
9-
import { EXTERNAL_URL_RE, CleanUrlsMode } from '../../shared'
8+
import { EXTERNAL_URL_RE } from '../../shared'
109

1110
const indexRE = /(^|.*\/)index.md(#?.*)$/i
1211

@@ -34,7 +33,7 @@ export const linkPlugin = (
3433
})
3534
// catch localhost links as dead link
3635
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {
37-
pushLink(url)
36+
pushLink(url, env)
3837
}
3938
} else if (
4039
// internal anchor links
@@ -44,7 +43,7 @@ export const linkPlugin = (
4443
// links to files (other than html/md)
4544
!/\.(?!html|md)\w+($|\?)/i.test(url)
4645
) {
47-
normalizeHref(hrefAttr, env.cleanUrls)
46+
normalizeHref(hrefAttr, env)
4847
}
4948

5049
// encode vite-specific replace strings in case they appear in URLs
@@ -57,10 +56,7 @@ export const linkPlugin = (
5756
return self.renderToken(tokens, idx, options)
5857
}
5958

60-
function normalizeHref(
61-
hrefAttr: [string, string],
62-
shouldCleanUrls: CleanUrlsMode
63-
) {
59+
function normalizeHref(hrefAttr: [string, string], env: MarkdownEnv) {
6460
let url = hrefAttr[1]
6561

6662
const indexMatch = url.match(indexRE)
@@ -73,12 +69,12 @@ export const linkPlugin = (
7369
if (cleanUrl.endsWith('.md')) {
7470
cleanUrl = cleanUrl.replace(
7571
/\.md$/,
76-
shouldCleanUrls === 'disabled' ? '.html' : ''
72+
env.cleanUrls === 'disabled' ? '.html' : ''
7773
)
7874
}
7975
// transform ./foo -> ./foo[.html]
8076
if (
81-
shouldCleanUrls === 'disabled' &&
77+
env.cleanUrls === 'disabled' &&
8278
!cleanUrl.endsWith('.html') &&
8379
!cleanUrl.endsWith('/')
8480
) {
@@ -94,7 +90,7 @@ export const linkPlugin = (
9490
}
9591

9692
// export it for existence check
97-
pushLink(url.replace(/\.html$/, ''))
93+
pushLink(url.replace(/\.html$/, ''), env)
9894

9995
// append base to internal (non-relative) urls
10096
if (url.startsWith('/')) {
@@ -105,9 +101,8 @@ export const linkPlugin = (
105101
hrefAttr[1] = decodeURI(url)
106102
}
107103

108-
function pushLink(link: string) {
109-
const data = (md as MarkdownRenderer).__data
110-
const links = data.links || (data.links = [])
104+
function pushLink(link: string, env: MarkdownEnv) {
105+
const links = env.links || (env.links = [])
111106
links.push(link)
112107
}
113108
}

src/node/markdownToVue.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,20 @@ export async function createMarkdownToVueRenderFn(
7373
}
7474
})
7575

76-
// reset state before render
77-
md.__path = file
78-
md.__relativePath = relativePath
79-
76+
// reset env before render
8077
const env: MarkdownEnv = {
8178
path: file,
8279
relativePath,
8380
cleanUrls
8481
}
8582
const html = md.render(src, env)
86-
const data = md.__data
87-
const { frontmatter = {}, headers = [], sfcBlocks, title = '' } = env
83+
const {
84+
frontmatter = {},
85+
headers = [],
86+
links = [],
87+
sfcBlocks,
88+
title = ''
89+
} = env
8890

8991
// validate data.links
9092
const deadLinks: string[] = []
@@ -101,9 +103,9 @@ export async function createMarkdownToVueRenderFn(
101103
deadLinks.push(url)
102104
}
103105

104-
if (data.links) {
106+
if (links) {
105107
const dir = path.dirname(file)
106-
for (let url of data.links) {
108+
for (let url of links) {
107109
if (/\.(?!html|md)\w+($|\?)/i.test(url)) continue
108110

109111
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {

0 commit comments

Comments
 (0)