Skip to content

Commit

Permalink
feat(render): add ext option for custom file extenstion, close #340
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 11, 2018
1 parent 54ab4c9 commit 248aa72
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 63 deletions.
7 changes: 4 additions & 3 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const config = merge(
executeScript: null,
noEmoji: false,
ga: '',
ext: '.md',
mergeNavbar: false,
formatUpdated: '',
externalLinkTarget: '_blank',
Expand All @@ -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 = ''
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions src/core/router/history/abstract.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
}
38 changes: 30 additions & 8 deletions src/core/router/history/base.js
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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)
}
}
23 changes: 3 additions & 20 deletions src/core/router/history/hash.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
}
}
17 changes: 2 additions & 15 deletions src/core/router/history/html5.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
}
4 changes: 4 additions & 0 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=')
})

0 comments on commit 248aa72

Please sign in to comment.