Skip to content

Commit 248aa72

Browse files
committed
feat(render): add ext option for custom file extenstion, close #340
1 parent 54ab4c9 commit 248aa72

File tree

7 files changed

+46
-63
lines changed

7 files changed

+46
-63
lines changed

src/core/config.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const config = merge(
1919
executeScript: null,
2020
noEmoji: false,
2121
ga: '',
22+
ext: '.md',
2223
mergeNavbar: false,
2324
formatUpdated: '',
2425
externalLinkTarget: '_blank',
@@ -43,9 +44,9 @@ if (script) {
4344
}
4445
}
4546

46-
if (config.loadSidebar === true) config.loadSidebar = '_sidebar.md'
47-
if (config.loadNavbar === true) config.loadNavbar = '_navbar.md'
48-
if (config.coverpage === true) config.coverpage = '_coverpage.md'
47+
if (config.loadSidebar === true) config.loadSidebar = '_sidebar' + config.ext
48+
if (config.loadNavbar === true) config.loadNavbar = '_navbar' + config.ext
49+
if (config.coverpage === true) config.coverpage = '_coverpage' + config.ext
4950
if (config.repo === true) config.repo = ''
5051
if (config.name === true) config.name = ''
5152
}

src/core/fetch/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ export function fetchMixin (proto) {
8181
path = coverpage
8282
}
8383
} else if (Array.isArray(coverpage)) {
84-
path = coverpage.indexOf(routePath) > -1 && '_coverpage.md'
84+
path = coverpage.indexOf(routePath) > -1 && '_coverpage'
8585
} else {
8686
const cover = coverpage[routePath]
87-
path = cover === true ? '_coverpage.md' : cover
87+
path = cover === true ? '_coverpage' : cover
8888
}
8989

9090
this.coverEnable = !!path

src/core/router/history/abstract.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { History } from './base'
2-
import { parseQuery, stringifyQuery, cleanPath } from '../util'
3-
import { merge } from '../../util/core'
2+
import { parseQuery } from '../util'
43

54
export class AbstractHistory extends History {
65
constructor (config) {
@@ -23,17 +22,4 @@ export class AbstractHistory extends History {
2322
query: parseQuery(query)
2423
}
2524
}
26-
27-
toURL (path, params, currentRoute) {
28-
const local = currentRoute && path[0] === '#'
29-
const route = this.parse(path)
30-
31-
route.query = merge({}, route.query, params)
32-
path = route.path + stringifyQuery(route.query)
33-
path = path.replace(/\.md(\?)|\.md$/, '$1')
34-
35-
if (local) path = currentRoute + path
36-
37-
return cleanPath('/' + path)
38-
}
3925
}

src/core/router/history/base.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { getPath, isAbsolutePath } from '../util'
2-
import { noop } from '../../util/core'
1+
import {
2+
getPath,
3+
isAbsolutePath,
4+
stringifyQuery,
5+
cleanPath,
6+
replaceSlug
7+
} from '../util'
8+
import { noop, merge } from '../../util/core'
39

410
const cached = {}
511

@@ -14,10 +20,10 @@ function getAlias (path, alias, last) {
1420
: path
1521
}
1622

17-
function getFileName (path) {
18-
return /\.(md|html)$/g.test(path)
23+
function getFileName (path, ext) {
24+
return new RegExp(`\\.(${ext.replace(/^\./, '')}|html)$`, 'g').test(path)
1925
? path
20-
: /\/$/g.test(path) ? `${path}README.md` : `${path}.md`
26+
: /\/$/g.test(path) ? `${path}README${ext}` : `${path}${ext}`
2127
}
2228

2329
export class History {
@@ -34,10 +40,11 @@ export class History {
3440

3541
const { config } = this
3642
const base = this.getBasePath()
43+
const ext = typeof config.ext !== 'string' ? '.md' : config.ext
3744

3845
path = config.alias ? getAlias(path, config.alias) : path
39-
path = getFileName(path)
40-
path = path === '/README.md' ? config.homepage || path : path
46+
path = getFileName(path, ext)
47+
path = path === `/README${ext}` ? config.homepage || path : path
4148
path = isAbsolutePath(path) ? path : getPath(base, path)
4249

4350
if (isRelative) {
@@ -57,5 +64,20 @@ export class History {
5764

5865
parse () {}
5966

60-
toURL () {}
67+
toURL (path, params, currentRoute) {
68+
const local = currentRoute && path[0] === '#'
69+
const route = this.parse(replaceSlug(path))
70+
71+
route.query = merge({}, route.query, params)
72+
path = route.path + stringifyQuery(route.query)
73+
path = path.replace(/\.md(\?)|\.md$/, '$1')
74+
75+
if (local) {
76+
const idIndex = currentRoute.indexOf('?')
77+
path =
78+
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
79+
}
80+
81+
return cleanPath('/' + path)
82+
}
6183
}

src/core/router/history/hash.js

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { History } from './base'
2-
import { merge, cached, noop } from '../../util/core'
2+
import { noop } from '../../util/core'
33
import { on } from '../../util/dom'
4-
import { parseQuery, stringifyQuery, cleanPath } from '../util'
4+
import { parseQuery, cleanPath, replaceSlug } from '../util'
55

66
function replaceHash (path) {
77
const i = location.href.indexOf('#')
88
location.replace(location.href.slice(0, i >= 0 ? i : 0) + '#' + path)
99
}
1010

11-
const replaceSlug = cached(path => {
12-
return path.replace('#', '?id=')
13-
})
14-
1511
export class HashHistory extends History {
1612
constructor (config) {
1713
super(config)
@@ -73,19 +69,6 @@ export class HashHistory extends History {
7369
}
7470

7571
toURL (path, params, currentRoute) {
76-
const local = currentRoute && path[0] === '#'
77-
const route = this.parse(replaceSlug(path))
78-
79-
route.query = merge({}, route.query, params)
80-
path = route.path + stringifyQuery(route.query)
81-
path = path.replace(/\.md(\?)|\.md$/, '$1')
82-
83-
if (local) {
84-
const idIndex = currentRoute.indexOf('?')
85-
path =
86-
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
87-
}
88-
89-
return cleanPath('#/' + path)
72+
return '#' + super.toURL(path, params, currentRoute)
9073
}
9174
}

src/core/router/history/html5.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { History } from './base'
2-
import { merge, noop } from '../../util/core'
2+
import { noop } from '../../util/core'
33
import { on } from '../../util/dom'
4-
import { parseQuery, stringifyQuery, getPath, cleanPath } from '../util'
4+
import { parseQuery, getPath } from '../util'
55

66
export class HTML5History extends History {
77
constructor (config) {
@@ -62,17 +62,4 @@ export class HTML5History extends History {
6262
query: parseQuery(query)
6363
}
6464
}
65-
66-
toURL (path, params, currentRoute) {
67-
const local = currentRoute && path[0] === '#'
68-
const route = this.parse(path)
69-
70-
route.query = merge({}, route.query, params)
71-
path = route.path + stringifyQuery(route.query)
72-
path = path.replace(/\.md(\?)|\.md$/, '$1')
73-
74-
if (local) path = currentRoute + path
75-
76-
return cleanPath('/' + path)
77-
}
7865
}

src/core/router/util.js

+4
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ export const getParentPath = cached(path => {
5454
export const cleanPath = cached(path => {
5555
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
5656
})
57+
58+
export const replaceSlug = cached(path => {
59+
return path.replace('#', '?id=')
60+
})

0 commit comments

Comments
 (0)