Skip to content

Commit

Permalink
fix: detect static files (#472)
Browse files Browse the repository at this point in the history
Co-authored-by: Yaël Guilloux <[email protected]>
  • Loading branch information
farnabaz and Tahul authored Jun 21, 2021
1 parent c7cb42b commit 25ac5a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export default <Module>async function docusModule() {
// Setup docus cache
options.alias['~docus-cache'] = join(options.srcDir, 'node_modules/.cache/docus')

/**
* Inject static dir into process that will be used by `link` handler
* to detect static files
*/
process.env.NUXT_STATIC_DIR = join(options.rootDir, options.dir.static)

// Inject Docus theme as ~docus
options.alias['~docus'] = resolve(__dirname, 'runtime')

Expand Down
8 changes: 8 additions & 0 deletions src/core/parser/markdown/handler/link.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { join } from 'path'
import fs from 'fs'
import normalize from 'mdurl/encode'
import { Node } from 'unist'
import { H } from 'mdast-util-to-hast'
Expand All @@ -8,6 +10,12 @@ export default function link(h: H, node: Node) {
href: normalize(node.url)
}

if (props.href.startsWith('/') && !props.href.startsWith('//') && props.href.match(/\.[a-z0-9A-Z]{2,4}$/)) {
if (fs.existsSync(join(process.env.NUXT_STATIC_DIR, props.href))) {
props.static = true
}
}

if (node.title !== null && node.title !== undefined) {
props.title = node.title
}
Expand Down
7 changes: 7 additions & 0 deletions src/defaultTheme/components/atoms/prose/ProseA.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export default defineComponent({
blank: {
type: Boolean,
default: false
},
/**
* `true` if `href` points to a static file
*/
static: {
type: Boolean,
default: false
}
}
})
Expand Down

1 comment on commit 25ac5a9

@vercel
Copy link

@vercel vercel bot commented on 25ac5a9 Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.