Skip to content

Commit

Permalink
feat(build): provide a pathname:// protocol to escape SPA (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
samestep authored Dec 21, 2022
1 parent a5e408f commit ae21a3a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/guide/asset-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Assets placed in `public` will be copied to the root of the dist directory as-is

Note that you should reference files placed in `public` using root absolute path - for example, `public/icon.png` should always be referenced in source code as `/icon.png`.

There is one exception to this: if you have an HTML page in `public` and link to it from the main site, the router will yield a 404 by default. To get around this, VitePress provides a `pathname://` protocol which allows you to link to another page in the same domain as if the link is external. Contrast these two links:

- [/pure.html](/pure.html)
- <pathname:///pure.html>

## Base URL

If your site is deployed to a non-root URL, you will need to set the `base` option in `.vitepress/config.js`. For example, if you plan to deploy your site to `https://foo.github.io/bar/`, then `base` should be set to `'/bar/'` (it should always start and end with a slash).
Expand Down
12 changes: 12 additions & 0 deletions docs/public/pure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Plain HTML page | VitePress</title>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
</head>
<body>
<h1>Not part of the main VitePress docs site</h1>
<div>This page is plain HTML in the <code>public</code> directory.</div>
</body>
</html>
4 changes: 2 additions & 2 deletions src/client/theme-default/support/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ref } from 'vue'
import { withBase, useData } from 'vitepress'
import { EXTERNAL_URL_RE } from '../../shared.js'
import { EXTERNAL_URL_RE, PATHNAME_PROTOCOL_RE } from '../../shared.js'

export const HASH_RE = /#.*$/
export const EXT_RE = /(index)?\.(md|html)$/
Expand Down Expand Up @@ -71,7 +71,7 @@ export function normalize(path: string): string {

export function normalizeLink(url: string): string {
if (isExternal(url)) {
return url
return url.replace(PATHNAME_PROTOCOL_RE, '')
}

const { site } = useData()
Expand Down
3 changes: 2 additions & 1 deletion src/node/markdown/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import MarkdownIt from 'markdown-it'
import type { MarkdownEnv } from '../env'
import { URL } from 'url'
import { EXTERNAL_URL_RE } from '../../shared'
import { EXTERNAL_URL_RE, PATHNAME_PROTOCOL_RE } from '../../shared'

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

Expand Down Expand Up @@ -35,6 +35,7 @@ export const linkPlugin = (
if (url.replace(EXTERNAL_URL_RE, '').startsWith('//localhost:')) {
pushLink(url, env)
}
hrefAttr[1] = url.replace(PATHNAME_PROTOCOL_RE, '')
} else if (
// internal anchor links
!url.startsWith('#') &&
Expand Down
1 change: 1 addition & 0 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type {
} from '../../types/shared.js'

export const EXTERNAL_URL_RE = /^[a-z]+:/i
export const PATHNAME_PROTOCOL_RE = /^pathname:\/\//
export const APPEARANCE_KEY = 'vitepress-theme-appearance'

export const inBrowser = typeof window !== 'undefined'
Expand Down

0 comments on commit ae21a3a

Please sign in to comment.