Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect public folder for dead link #290

Merged
merged 2 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ export function createMarkdownToVueRenderFn(
const md = createMarkdownRenderer(root, options)
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))

return (src: string, file: string): MarkdownCompileResult => {
return (
src: string,
file: string,
publicDir: string
): MarkdownCompileResult => {
const relativePath = slash(path.relative(root, file))

const cached = cache.get(src)
Expand Down Expand Up @@ -56,7 +60,10 @@ export function createMarkdownToVueRenderFn(
? url.slice(1)
: path.relative(root, path.resolve(dir, url))
)
if (!pages.includes(resolved)) {
if (
!pages.includes(resolved) &&
!fs.existsSync(path.resolve(dir, publicDir, `${resolved}.html`))
) {
console.warn(
chalk.yellow(
`\n(!) Found dead link ${chalk.cyan(
Expand Down
15 changes: 12 additions & 3 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { Plugin } from 'vite'
import { Plugin, ResolvedConfig } from 'vite'
import { SiteConfig, resolveSiteData } from './config'
import { createMarkdownToVueRenderFn } from './markdownToVue'
import { APP_PATH, SITE_DATA_REQUEST_PATH } from './alias'
Expand Down Expand Up @@ -37,10 +37,15 @@ export function createVitePressPlugin(

let siteData = site
let hasDeadLinks = false
let config: ResolvedConfig

const vitePressPlugin: Plugin = {
name: 'vitepress',

configResolved(resolvedConfig) {
config = resolvedConfig
},

config() {
return {
resolve: {
Expand Down Expand Up @@ -72,7 +77,7 @@ export function createVitePressPlugin(
transform(code, id) {
if (id.endsWith('.md')) {
// transform .md files into vueSrc so plugin-vue can handle it
const { vueSrc, deadLinks } = markdownToVue(code, id)
const { vueSrc, deadLinks } = markdownToVue(code, id, config.publicDir)
if (deadLinks.length) {
hasDeadLinks = true
}
Expand Down Expand Up @@ -176,7 +181,11 @@ export function createVitePressPlugin(
// hot reload .md files as .vue files
if (file.endsWith('.md')) {
const content = await read()
const { pageData, vueSrc } = markdownToVue(content, file)
const { pageData, vueSrc } = markdownToVue(
content,
file,
config.publicDir
)

// notify the client to update page data
server.ws.send({
Expand Down