Skip to content

Commit

Permalink
fix: set root to empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 17, 2020
1 parent 2f85fd7 commit ab0a930
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
17 changes: 16 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ Finally serve this directory as a static website:
- **python**: `python -m SimpleHTTPServer`
- ...etc, you can use any static file server, for real.

### How Files Are Resolved

If current `location.pathname` is `/`, i.e. the homepage, it fetches `/README.md`.

If current `location.pathname` is `/docs/`, it fetches `/docs/README.md`.

If current `location.pathname` is `/docs/en`, it fetches `/docs/en.md`.

Basically if the pathname ends with a slash, we treat it as a directory and try to load the `README.md` file under that path, you can also use [indexFile](#indexfile) option to change `README.md` to other file if you want. If the pathname does not end with slash, we would fetch `pathname + '.md'`.

You can also use [root](#root) option to set the origin of the files, for example if you want to load files from other domain, you can set `root: 'https://sub.domain.com/data'`.


## Guide

### Site Title
Expand Down Expand Up @@ -318,10 +331,12 @@ interface NavLink {
- Type: `string`
- Default: `README.md`

Used for path ending with a slash.

#### root

- Type: `string`
- Default: `./`
- Default: `''`

The root path we use to resolve files from.

Expand Down
4 changes: 2 additions & 2 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Navbar } from './Navbar'
import { Sidebar } from './Sidebar'
import { Main } from './Main'
import { renderMarkdown, SidebarMenuItem } from '../markdown'
import { loadLanguages, scrollToHash, updateURLHash, throttle } from '../utils'
import { loadLanguages, scrollToHash, updateURLHash, throttle, getFileUrl } from '../utils'

const handleScroll = throttle(() => {
const headings = document.querySelectorAll('.content .heading')
Expand Down Expand Up @@ -42,7 +42,7 @@ export const App: FunctionComponent<{ options: InstanceOptions }> = ({

useEffect(() => {
Promise.all([
fetch(`${options.root}${options.indexFile}`),
fetch(getFileUrl(options.root, options.indexFile, location.pathname)),
options.highlightLanguages && loadLanguages(options.highlightLanguages),
])
.then(([res]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/docup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Docup {
this.options = {
title: options.title || document.title || 'Docup',
indexFile: 'README.md',
root: './',
root: '',
highlight: true,
...options,
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,22 @@ export function throttle<T extends Function>(func: T, wait: number) {
args = undefined
}
}

const TRAILING_SLASH_RE = /\/$/
const MD_RE = /\.md$/

export function getFileUrl(root: string, indexFile: string, pathname: string) {
let url = root
// Remove trailing slash
// Becuase pathname always starts with slash
.replace(TRAILING_SLASH_RE, '')
// `pathname` ends with slash
// this is a directory
// use `indexFile`
if (TRAILING_SLASH_RE.test(pathname)) {
url += `${pathname}${indexFile}`
} else if (!MD_RE.test(pathname)) {
url += `${pathname}.md`
}
return url
}

1 comment on commit ab0a930

@vercel
Copy link

@vercel vercel bot commented on ab0a930 May 17, 2020

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.