feat(render): add ext option for custom file extenstion, close #340
This commit is contained in:
parent
54ab4c9ff7
commit
248aa72cd2
7 changed files with 46 additions and 63 deletions
|
|
@ -19,6 +19,7 @@ const config = merge(
|
||||||
executeScript: null,
|
executeScript: null,
|
||||||
noEmoji: false,
|
noEmoji: false,
|
||||||
ga: '',
|
ga: '',
|
||||||
|
ext: '.md',
|
||||||
mergeNavbar: false,
|
mergeNavbar: false,
|
||||||
formatUpdated: '',
|
formatUpdated: '',
|
||||||
externalLinkTarget: '_blank',
|
externalLinkTarget: '_blank',
|
||||||
|
|
@ -43,9 +44,9 @@ if (script) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.loadSidebar === true) config.loadSidebar = '_sidebar.md'
|
if (config.loadSidebar === true) config.loadSidebar = '_sidebar' + config.ext
|
||||||
if (config.loadNavbar === true) config.loadNavbar = '_navbar.md'
|
if (config.loadNavbar === true) config.loadNavbar = '_navbar' + config.ext
|
||||||
if (config.coverpage === true) config.coverpage = '_coverpage.md'
|
if (config.coverpage === true) config.coverpage = '_coverpage' + config.ext
|
||||||
if (config.repo === true) config.repo = ''
|
if (config.repo === true) config.repo = ''
|
||||||
if (config.name === true) config.name = ''
|
if (config.name === true) config.name = ''
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,10 +81,10 @@ export function fetchMixin (proto) {
|
||||||
path = coverpage
|
path = coverpage
|
||||||
}
|
}
|
||||||
} else if (Array.isArray(coverpage)) {
|
} else if (Array.isArray(coverpage)) {
|
||||||
path = coverpage.indexOf(routePath) > -1 && '_coverpage.md'
|
path = coverpage.indexOf(routePath) > -1 && '_coverpage'
|
||||||
} else {
|
} else {
|
||||||
const cover = coverpage[routePath]
|
const cover = coverpage[routePath]
|
||||||
path = cover === true ? '_coverpage.md' : cover
|
path = cover === true ? '_coverpage' : cover
|
||||||
}
|
}
|
||||||
|
|
||||||
this.coverEnable = !!path
|
this.coverEnable = !!path
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import { History } from './base'
|
import { History } from './base'
|
||||||
import { parseQuery, stringifyQuery, cleanPath } from '../util'
|
import { parseQuery } from '../util'
|
||||||
import { merge } from '../../util/core'
|
|
||||||
|
|
||||||
export class AbstractHistory extends History {
|
export class AbstractHistory extends History {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
|
|
@ -23,17 +22,4 @@ export class AbstractHistory extends History {
|
||||||
query: parseQuery(query)
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,11 @@
|
||||||
import { getPath, isAbsolutePath } from '../util'
|
import {
|
||||||
import { noop } from '../../util/core'
|
getPath,
|
||||||
|
isAbsolutePath,
|
||||||
|
stringifyQuery,
|
||||||
|
cleanPath,
|
||||||
|
replaceSlug
|
||||||
|
} from '../util'
|
||||||
|
import { noop, merge } from '../../util/core'
|
||||||
|
|
||||||
const cached = {}
|
const cached = {}
|
||||||
|
|
||||||
|
|
@ -14,10 +20,10 @@ function getAlias (path, alias, last) {
|
||||||
: path
|
: path
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileName (path) {
|
function getFileName (path, ext) {
|
||||||
return /\.(md|html)$/g.test(path)
|
return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path)
|
||||||
? path
|
? path
|
||||||
: /\/$/g.test(path) ? `${path}README.md` : `${path}.md`
|
: /\/$/g.test(path) ? `${path}README${ext}` : `${path}${ext}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export class History {
|
export class History {
|
||||||
|
|
@ -34,10 +40,11 @@ export class History {
|
||||||
|
|
||||||
const { config } = this
|
const { config } = this
|
||||||
const base = this.getBasePath()
|
const base = this.getBasePath()
|
||||||
|
const ext = typeof config.ext !== 'string' ? '.md' : config.ext
|
||||||
|
|
||||||
path = config.alias ? getAlias(path, config.alias) : path
|
path = config.alias ? getAlias(path, config.alias) : path
|
||||||
path = getFileName(path)
|
path = getFileName(path, ext)
|
||||||
path = path === '/README.md' ? config.homepage || path : path
|
path = path === `/README${ext}` ? config.homepage || path : path
|
||||||
path = isAbsolutePath(path) ? path : getPath(base, path)
|
path = isAbsolutePath(path) ? path : getPath(base, path)
|
||||||
|
|
||||||
if (isRelative) {
|
if (isRelative) {
|
||||||
|
|
@ -57,5 +64,20 @@ export class History {
|
||||||
|
|
||||||
parse () {}
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,13 @@
|
||||||
import { History } from './base'
|
import { History } from './base'
|
||||||
import { merge, cached, noop } from '../../util/core'
|
import { noop } from '../../util/core'
|
||||||
import { on } from '../../util/dom'
|
import { on } from '../../util/dom'
|
||||||
import { parseQuery, stringifyQuery, cleanPath } from '../util'
|
import { parseQuery, cleanPath, replaceSlug } from '../util'
|
||||||
|
|
||||||
function replaceHash (path) {
|
function replaceHash (path) {
|
||||||
const i = location.href.indexOf('#')
|
const i = location.href.indexOf('#')
|
||||||
location.replace(location.href.slice(0, i >= 0 ? i : 0) + '#' + path)
|
location.replace(location.href.slice(0, i >= 0 ? i : 0) + '#' + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
const replaceSlug = cached(path => {
|
|
||||||
return path.replace('#', '?id=')
|
|
||||||
})
|
|
||||||
|
|
||||||
export class HashHistory extends History {
|
export class HashHistory extends History {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
super(config)
|
super(config)
|
||||||
|
|
@ -73,19 +69,6 @@ export class HashHistory extends History {
|
||||||
}
|
}
|
||||||
|
|
||||||
toURL (path, params, currentRoute) {
|
toURL (path, params, currentRoute) {
|
||||||
const local = currentRoute && path[0] === '#'
|
return '#' + super.toURL(path, params, currentRoute)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { History } from './base'
|
import { History } from './base'
|
||||||
import { merge, noop } from '../../util/core'
|
import { noop } from '../../util/core'
|
||||||
import { on } from '../../util/dom'
|
import { on } from '../../util/dom'
|
||||||
import { parseQuery, stringifyQuery, getPath, cleanPath } from '../util'
|
import { parseQuery, getPath } from '../util'
|
||||||
|
|
||||||
export class HTML5History extends History {
|
export class HTML5History extends History {
|
||||||
constructor (config) {
|
constructor (config) {
|
||||||
|
|
@ -62,17 +62,4 @@ export class HTML5History extends History {
|
||||||
query: parseQuery(query)
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,3 +54,7 @@ export const getParentPath = cached(path => {
|
||||||
export const cleanPath = cached(path => {
|
export const cleanPath = cached(path => {
|
||||||
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
|
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const replaceSlug = cached(path => {
|
||||||
|
return path.replace('#', '?id=')
|
||||||
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue