feat(render): add ext option for custom file extenstion, close #340

This commit is contained in:
qingwei.li 2018-02-10 18:28:01 +08:00 committed by cinwell.li
parent 54ab4c9ff7
commit 248aa72cd2
7 changed files with 46 additions and 63 deletions

View File

@ -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 = ''
}

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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=')
})