Skip to content

Commit f095eb8

Browse files
committedMay 29, 2017
feat: support history mode
1 parent 8741c74 commit f095eb8

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed
 

‎src/core/render/compiler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class Compiler {
1414
this.cacheTree = {}
1515
this.toc = []
1616
this.linkTarget = config.externalLinkTarget || '_blank'
17-
this.contentBase = getBasePath(config.base)
17+
this.contentBase = getBasePath(config.basePath)
1818

1919
const renderer = this._initRenderer()
2020
let compile
@@ -44,7 +44,7 @@ export class Compiler {
4444

4545
_initRenderer () {
4646
const renderer = new marked.Renderer()
47-
const { linkTarget, router, toc } = this
47+
const { linkTarget, router, toc, contentBase } = this
4848
/**
4949
* render anchor tag
5050
* @link https://github.com/chjj/marked#overriding-renderer-methods
@@ -102,7 +102,7 @@ export class Compiler {
102102
const titleHTML = title ? ` title="${title}"` : ''
103103

104104
if (!isAbsolutePath(href)) {
105-
url = getPath(this.contentBase, href)
105+
url = getPath(contentBase, href)
106106
}
107107

108108
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>`
@@ -120,7 +120,7 @@ export class Compiler {
120120

121121
if (text) {
122122
html = this.compile(text)
123-
html = html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
123+
html = html && html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
124124
} else {
125125
const tree = this.cacheTree[currentPath] || genTree(this.toc, level)
126126
html = treeTpl(tree, '<ul>')

‎src/core/router/history/base.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ export class History {
1818
this.config = config
1919
}
2020

21-
onchange (cb = noop) {
22-
cb()
23-
}
24-
2521
getFile (path) {
2622
const { config } = this
2723
const base = getBasePath(config.basePath)
@@ -34,6 +30,10 @@ export class History {
3430
return path
3531
}
3632

33+
onchange (cb = noop) {
34+
cb()
35+
}
36+
3737
getCurrentPath () {}
3838

3939
normalize () {}

‎src/core/router/history/html5.js

+75
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,83 @@
11
import { History } from './base'
2+
import { merge, noop } from '../../util/core'
3+
import { on } from '../../util/dom'
4+
import { parseQuery, stringifyQuery, cleanPath } from '../util'
25

36
export class HTML5History extends History {
47
constructor (config) {
58
super(config)
69
this.mode = 'history'
710
}
11+
12+
getCurrentPath () {
13+
const base = this.config.base
14+
let path = window.location.pathname
15+
16+
if (base && path.indexOf(base) === 0) {
17+
path = path.slice(base.length)
18+
}
19+
20+
return (path || '/') + window.location.search + window.location.hash
21+
}
22+
23+
onchange (cb = noop) {
24+
on('click', e => {
25+
const el = e.target.tagName === 'A'
26+
? e.target
27+
: e.target.parentNode
28+
29+
if (el.tagName === 'A' && !/_blank/.test(el.target)) {
30+
e.preventDefault()
31+
const url = el.href
32+
window.history.pushState({ key: url }, '', url)
33+
cb()
34+
}
35+
})
36+
37+
on('popstate', cb)
38+
}
39+
40+
normalize () {
41+
let path = this.getCurrentPath()
42+
43+
path = path.replace('#', '?id=')
44+
window.history.pushState({ key: path }, '', path)
45+
46+
return path
47+
}
48+
49+
/**
50+
* Parse the url
51+
* @param {string} [path=location.href]
52+
* @return {object} { path, query }
53+
*/
54+
parse (path = location.href) {
55+
let query = ''
56+
57+
const queryIndex = path.indexOf('?')
58+
if (queryIndex >= 0) {
59+
query = path.slice(queryIndex + 1)
60+
path = path.slice(0, queryIndex)
61+
}
62+
63+
const baseIndex = path.indexOf(location.origin)
64+
if (baseIndex > -1) {
65+
path = path.slice(baseIndex + location.origin.length)
66+
}
67+
68+
return { path, query: parseQuery(query) }
69+
}
70+
71+
toURL (path, params, currentRoute) {
72+
const local = currentRoute && path[0] === '#'
73+
const route = this.parse(path)
74+
75+
route.query = merge({}, route.query, params)
76+
path = route.path + stringifyQuery(route.query)
77+
path = path.replace(/\.md(\?)|\.md$/, '$1')
78+
79+
if (local) path = currentRoute + path
80+
81+
return cleanPath('/' + path)
82+
}
883
}

‎src/core/util/env.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ export const isMobile = document.body.clientWidth <= 600
66

77
export const inBrowser = typeof window !== 'undefined'
88

9+
/**
10+
* @see https://github.com/MoOx/pjax/blob/master/lib/is-supported.js
11+
*/
912
export const supportsPushState = inBrowser && (function () {
10-
const ua = window.navigator.userAgent
11-
12-
if (
13-
(ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
14-
ua.indexOf('Mobile Safari') !== -1 &&
15-
ua.indexOf('Chrome') === -1 &&
16-
ua.indexOf('Windows Phone') === -1
17-
) {
18-
return false
19-
}
20-
21-
return window.history && 'pushState' in window.history
13+
// Borrowed wholesale from https://github.com/defunkt/jquery-pjax
14+
return window.history &&
15+
window.history.pushState &&
16+
window.history.replaceState &&
17+
// pushState isn’t reliable on iOS until 5.
18+
!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
2219
})()

0 commit comments

Comments
 (0)
Please sign in to comment.