diff --git a/src/core/config.js b/src/core/config.js index c42625778..7b72adfae 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -19,6 +19,7 @@ const config = merge( executeScript: null, noEmoji: false, ga: '', + ext: '.md', mergeNavbar: false, formatUpdated: '', externalLinkTarget: '_blank', @@ -43,9 +44,9 @@ if (script) { } } - if (config.loadSidebar === true) config.loadSidebar = '_sidebar.md' - if (config.loadNavbar === true) config.loadNavbar = '_navbar.md' - if (config.coverpage === true) config.coverpage = '_coverpage.md' + if (config.loadSidebar === true) config.loadSidebar = '_sidebar' + config.ext + if (config.loadNavbar === true) config.loadNavbar = '_navbar' + config.ext + if (config.coverpage === true) config.coverpage = '_coverpage' + config.ext if (config.repo === true) config.repo = '' if (config.name === true) config.name = '' } diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 045089864..ac1a908ed 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -81,10 +81,10 @@ export function fetchMixin (proto) { path = coverpage } } else if (Array.isArray(coverpage)) { - path = coverpage.indexOf(routePath) > -1 && '_coverpage.md' + path = coverpage.indexOf(routePath) > -1 && '_coverpage' } else { const cover = coverpage[routePath] - path = cover === true ? '_coverpage.md' : cover + path = cover === true ? '_coverpage' : cover } this.coverEnable = !!path diff --git a/src/core/router/history/abstract.js b/src/core/router/history/abstract.js index d608efe12..0846f2ba8 100644 --- a/src/core/router/history/abstract.js +++ b/src/core/router/history/abstract.js @@ -1,6 +1,5 @@ import { History } from './base' -import { parseQuery, stringifyQuery, cleanPath } from '../util' -import { merge } from '../../util/core' +import { parseQuery } from '../util' export class AbstractHistory extends History { constructor (config) { @@ -23,17 +22,4 @@ export class AbstractHistory extends History { query: parseQuery(query) } } - - toURL (path, params, currentRoute) { - const local = currentRoute && path[0] === '#' - const route = this.parse(path) - - route.query = merge({}, route.query, params) - path = route.path + stringifyQuery(route.query) - path = path.replace(/\.md(\?)|\.md$/, '$1') - - if (local) path = currentRoute + path - - return cleanPath('/' + path) - } } diff --git a/src/core/router/history/base.js b/src/core/router/history/base.js index 055073b61..0a0ccd357 100644 --- a/src/core/router/history/base.js +++ b/src/core/router/history/base.js @@ -1,5 +1,11 @@ -import { getPath, isAbsolutePath } from '../util' -import { noop } from '../../util/core' +import { + getPath, + isAbsolutePath, + stringifyQuery, + cleanPath, + replaceSlug +} from '../util' +import { noop, merge } from '../../util/core' const cached = {} @@ -14,10 +20,10 @@ function getAlias (path, alias, last) { : path } -function getFileName (path) { - return /\.(md|html)$/g.test(path) +function getFileName (path, ext) { + return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path) ? path - : /\/$/g.test(path) ? `${path}README.md` : `${path}.md` + : /\/$/g.test(path) ? `${path}README${ext}` : `${path}${ext}` } export class History { @@ -34,10 +40,11 @@ export class History { const { config } = this const base = this.getBasePath() + const ext = typeof config.ext !== 'string' ? '.md' : config.ext path = config.alias ? getAlias(path, config.alias) : path - path = getFileName(path) - path = path === '/README.md' ? config.homepage || path : path + path = getFileName(path, ext) + path = path === `/README${ext}` ? config.homepage || path : path path = isAbsolutePath(path) ? path : getPath(base, path) if (isRelative) { @@ -57,5 +64,20 @@ export class History { parse () {} - toURL () {} + toURL (path, params, currentRoute) { + const local = currentRoute && path[0] === '#' + const route = this.parse(replaceSlug(path)) + + route.query = merge({}, route.query, params) + path = route.path + stringifyQuery(route.query) + path = path.replace(/\.md(\?)|\.md$/, '$1') + + if (local) { + const idIndex = currentRoute.indexOf('?') + path = + (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path + } + + return cleanPath('/' + path) + } } diff --git a/src/core/router/history/hash.js b/src/core/router/history/hash.js index 37858521d..04280c958 100644 --- a/src/core/router/history/hash.js +++ b/src/core/router/history/hash.js @@ -1,17 +1,13 @@ import { History } from './base' -import { merge, cached, noop } from '../../util/core' +import { noop } from '../../util/core' import { on } from '../../util/dom' -import { parseQuery, stringifyQuery, cleanPath } from '../util' +import { parseQuery, cleanPath, replaceSlug } from '../util' function replaceHash (path) { const i = location.href.indexOf('#') location.replace(location.href.slice(0, i >= 0 ? i : 0) + '#' + path) } -const replaceSlug = cached(path => { - return path.replace('#', '?id=') -}) - export class HashHistory extends History { constructor (config) { super(config) @@ -73,19 +69,6 @@ export class HashHistory extends History { } toURL (path, params, currentRoute) { - const local = currentRoute && path[0] === '#' - const route = this.parse(replaceSlug(path)) - - route.query = merge({}, route.query, params) - path = route.path + stringifyQuery(route.query) - path = path.replace(/\.md(\?)|\.md$/, '$1') - - if (local) { - const idIndex = currentRoute.indexOf('?') - path = - (idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path - } - - return cleanPath('#/' + path) + return '#' + super.toURL(path, params, currentRoute) } } diff --git a/src/core/router/history/html5.js b/src/core/router/history/html5.js index 7747a447f..6acefaf63 100644 --- a/src/core/router/history/html5.js +++ b/src/core/router/history/html5.js @@ -1,7 +1,7 @@ import { History } from './base' -import { merge, noop } from '../../util/core' +import { noop } from '../../util/core' import { on } from '../../util/dom' -import { parseQuery, stringifyQuery, getPath, cleanPath } from '../util' +import { parseQuery, getPath } from '../util' export class HTML5History extends History { constructor (config) { @@ -62,17 +62,4 @@ export class HTML5History extends History { query: parseQuery(query) } } - - toURL (path, params, currentRoute) { - const local = currentRoute && path[0] === '#' - const route = this.parse(path) - - route.query = merge({}, route.query, params) - path = route.path + stringifyQuery(route.query) - path = path.replace(/\.md(\?)|\.md$/, '$1') - - if (local) path = currentRoute + path - - return cleanPath('/' + path) - } } diff --git a/src/core/router/util.js b/src/core/router/util.js index d3ce9a83c..84219b6fa 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -54,3 +54,7 @@ export const getParentPath = cached(path => { export const cleanPath = cached(path => { return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/') }) + +export const replaceSlug = cached(path => { + return path.replace('#', '?id=') +})