Skip to content

Commit b29b3b3

Browse files
authored
refactor: drop dependency on mrmime (#3400)
1 parent 27f60e0 commit b29b3b3

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
"focus-trap": "^7.5.4",
101101
"mark.js": "8.11.1",
102102
"minisearch": "^6.3.0",
103-
"mrmime": "^2.0.0",
104103
"shikiji": "^0.9.15",
105104
"shikiji-core": "^0.9.15",
106105
"shikiji-transformers": "^0.9.15",

pnpm-lock.yaml

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client/app/router.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { reactive, inject, markRaw, nextTick, readonly } from 'vue'
22
import type { Component, InjectionKey } from 'vue'
3-
import { lookup } from 'mrmime'
4-
import { notFoundPageData } from '../shared'
3+
import { notFoundPageData, treatAsHtml } from '../shared'
54
import type { PageData, PageDataPayload, Awaitable } from '../shared'
65
import { inBrowser, withBase } from './utils'
76
import { siteDataRef } from './data'
@@ -182,17 +181,15 @@ export function createRouter(
182181
link.baseURI
183182
)
184183
const currentUrl = window.location
185-
const mimeType = lookup(pathname)
186-
// only intercept inbound links
184+
// only intercept inbound html links
187185
if (
188186
!e.ctrlKey &&
189187
!e.shiftKey &&
190188
!e.altKey &&
191189
!e.metaKey &&
192190
!target &&
193191
origin === currentUrl.origin &&
194-
// intercept only html and unknown types (assume html)
195-
(!mimeType || mimeType === 'text/html')
192+
treatAsHtml(pathname)
196193
) {
197194
e.preventDefault()
198195
if (

src/client/theme-default/support/utils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { withBase } from 'vitepress'
2-
import { lookup } from 'mrmime'
32
import { useData } from '../composables/data'
4-
import { isExternal } from '../../shared'
3+
import { isExternal, treatAsHtml } from '../../shared'
54

65
export function throttleAndDebounce(fn: () => void, delay: number): () => void {
76
let timeoutId: NodeJS.Timeout
@@ -28,7 +27,7 @@ export function normalizeLink(url: string): string {
2827
isExternal(url) ||
2928
url.startsWith('#') ||
3029
!protocol.startsWith('http') ||
31-
(/\.(?!html|md)\w+($|\?)/i.test(url) && lookup(url))
30+
!treatAsHtml(pathname)
3231
)
3332
return url
3433

src/node/markdown/plugins/link.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import type MarkdownIt from 'markdown-it'
66
import { URL } from 'url'
7-
import { EXTERNAL_URL_RE, isExternal, type MarkdownEnv } from '../../shared'
7+
import {
8+
EXTERNAL_URL_RE,
9+
isExternal,
10+
treatAsHtml,
11+
type MarkdownEnv
12+
} from '../../shared'
813

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

@@ -35,13 +40,15 @@ export const linkPlugin = (
3540
}
3641
hrefAttr[1] = url
3742
} else {
43+
const { pathname, protocol } = new URL(url, 'http://a.com')
44+
3845
if (
39-
// internal anchor links
46+
// skip internal anchor links
4047
!url.startsWith('#') &&
41-
// mail/custom protocol links
42-
new URL(url, 'http://a.com').protocol.startsWith('http') &&
43-
// links to files (other than html/md)
44-
!/\.(?!html|md)\w+($|\?)/i.test(url)
48+
// skip mail/custom protocol links
49+
protocol.startsWith('http') &&
50+
// skip links to files (other than html/md)
51+
treatAsHtml(pathname)
4552
) {
4653
normalizeHref(hrefAttr, env)
4754
} else if (url.startsWith('#')) {

src/node/markdownToVue.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
slash,
1515
type HeadConfig,
1616
type MarkdownEnv,
17-
type PageData
17+
type PageData,
18+
treatAsHtml
1819
} from './shared'
1920
import { getGitTimestamp } from './utils/getGitTimestamp'
2021
import { processIncludes } from './utils/processIncludes'
@@ -145,7 +146,8 @@ export async function createMarkdownToVueRenderFn(
145146
if (links) {
146147
const dir = path.dirname(file)
147148
for (let url of links) {
148-
if (/\.(?!html|md)\w+($|\?)/i.test(url)) continue
149+
const { pathname } = new URL(url, 'http://a.com')
150+
if (!treatAsHtml(pathname)) continue
149151

150152
url = url.replace(/[?#].*$/, '').replace(/\.(html|md)$/, '')
151153
if (url.endsWith('/')) url += `index`

src/shared/shared.ts

+11
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,14 @@ export function sanitizeFileName(name: string): string {
174174
export function slash(p: string): string {
175175
return p.replace(/\\/g, '/')
176176
}
177+
178+
// md, html? are intentionally omitted, see treatAsHtml
179+
const commonMimeTypes = `3g2,3gp,7z,aac,abw,ai,aif,aifc,aiff,arc,asf,asr,asx,au,avi,avif,axs,azw,bin,bmp,bz,bz2,c,cda,cer,class,crl,crt,csh,css,csv,dcr,der,dll,doc,docx,eot,eps,epub,exe,gif,gtar,gz,gzip,ico,ics,ief,jar,jpe,jpeg,jpg,js,json,jsonld,latex,m3u,man,mdb,mht,mhtml,mid,midi,mjs,mov,mp2,mp3,mp4,mpa,mpe,mpeg,mpg,mpkg,mpp,odp,ods,odt,oga,ogv,ogx,opus,otf,p10,p12,p7b,p7c,p7m,p7r,p7s,pbm,pdf,pfx,php,png,ppt,pptx,ps,pub,qt,rar,roff,rtf,rtx,ser,sh,spc,svg,swf,t,tar,tcl,tex,texi,texinfo,tgz,tif,tiff,tr,ts,tsv,ttf,txt,ua,viv,vivo,vsd,wav,weba,webm,webp,woff,woff2,xbm,xhtml,xls,xlsx,xml,xul,zip`
180+
181+
const set = new Set(commonMimeTypes.split(','))
182+
183+
export function treatAsHtml(filename: string): boolean {
184+
const ext = filename.split('.').pop()?.toLowerCase()
185+
186+
return ext == null || !set.has(ext)
187+
}

0 commit comments

Comments
 (0)