mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
feat: fetch files with the query params, fixed #303
This commit is contained in:
parent
0d54c0cf83
commit
2a2ed96eb8
@ -1,29 +1,31 @@
|
|||||||
import { get } from './ajax'
|
import { get } from './ajax'
|
||||||
import { callHook } from '../init/lifecycle'
|
import { callHook } from '../init/lifecycle'
|
||||||
import { getParentPath } from '../router/util'
|
import { getParentPath, stringifyQuery } from '../router/util'
|
||||||
import { noop } from '../util/core'
|
import { noop } from '../util/core'
|
||||||
import { getAndActive } from '../event/sidebar'
|
import { getAndActive } from '../event/sidebar'
|
||||||
|
|
||||||
function loadNested (path, file, next, vm, first) {
|
function loadNested (path, qs, file, next, vm, first) {
|
||||||
path = first ? path : path.replace(/\/$/, '')
|
path = first ? path : path.replace(/\/$/, '')
|
||||||
path = getParentPath(path)
|
path = getParentPath(path)
|
||||||
|
|
||||||
if (!path) return
|
if (!path) return
|
||||||
|
|
||||||
get(vm.router.getFile(path + file))
|
get(vm.router.getFile(path + file) + qs).then(next, _ =>
|
||||||
.then(next, _ => loadNested(path, file, next, vm))
|
loadNested(path, qs, file, next, vm)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchMixin (proto) {
|
export function fetchMixin (proto) {
|
||||||
let last
|
let last
|
||||||
proto._fetch = function (cb = noop) {
|
proto._fetch = function (cb = noop) {
|
||||||
const { path } = this.route
|
const { path, query } = this.route
|
||||||
|
const qs = stringifyQuery(query, ['id'])
|
||||||
const { loadNavbar, loadSidebar } = this.config
|
const { loadNavbar, loadSidebar } = this.config
|
||||||
|
|
||||||
// Abort last request
|
// Abort last request
|
||||||
last && last.abort && last.abort()
|
last && last.abort && last.abort()
|
||||||
|
|
||||||
last = get(this.router.getFile(path), true)
|
last = get(this.router.getFile(path) + qs, true)
|
||||||
|
|
||||||
// Current page is html
|
// Current page is html
|
||||||
this.isHTML = /\.html$/g.test(path)
|
this.isHTML = /\.html$/g.test(path)
|
||||||
@ -31,29 +33,42 @@ export function fetchMixin (proto) {
|
|||||||
const loadSideAndNav = () => {
|
const loadSideAndNav = () => {
|
||||||
if (!loadSidebar) return cb()
|
if (!loadSidebar) return cb()
|
||||||
|
|
||||||
const fn = result => { this._renderSidebar(result); cb() }
|
const fn = result => {
|
||||||
|
this._renderSidebar(result)
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
|
||||||
// Load sidebar
|
// Load sidebar
|
||||||
loadNested(path, loadSidebar, fn, this, true)
|
loadNested(path, qs, loadSidebar, fn, this, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load main content
|
// Load main content
|
||||||
last.then((text, opt) => {
|
last.then(
|
||||||
|
(text, opt) => {
|
||||||
this._renderMain(text, opt)
|
this._renderMain(text, opt)
|
||||||
loadSideAndNav()
|
loadSideAndNav()
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
this._renderMain(null)
|
this._renderMain(null)
|
||||||
loadSideAndNav()
|
loadSideAndNav()
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// Load nav
|
// Load nav
|
||||||
loadNavbar &&
|
loadNavbar &&
|
||||||
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
|
loadNested(
|
||||||
|
path,
|
||||||
|
qs,
|
||||||
|
loadNavbar,
|
||||||
|
text => this._renderNav(text),
|
||||||
|
this,
|
||||||
|
true
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
proto._fetchCover = function () {
|
proto._fetchCover = function () {
|
||||||
const { coverpage } = this.config
|
const { coverpage } = this.config
|
||||||
|
const query = this.route.query
|
||||||
const root = getParentPath(this.route.path)
|
const root = getParentPath(this.route.path)
|
||||||
const path = this.router.getFile(root + coverpage)
|
const path = this.router.getFile(root + coverpage)
|
||||||
|
|
||||||
@ -63,8 +78,9 @@ export function fetchMixin (proto) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.coverIsHTML = /\.html$/g.test(path)
|
this.coverIsHTML = /\.html$/g.test(path)
|
||||||
get(path)
|
get(path + stringifyQuery(query, ['id'])).then(text =>
|
||||||
.then(text => this._renderCover(text))
|
this._renderCover(text)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
proto.$fetch = function (cb = noop) {
|
proto.$fetch = function (cb = noop) {
|
||||||
|
|||||||
@ -22,10 +22,11 @@ export function parseQuery (query) {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stringifyQuery (obj) {
|
export function stringifyQuery (obj, ignores = []) {
|
||||||
const qs = []
|
const qs = []
|
||||||
|
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
|
if (ignores.indexOf(key) > -1) continue
|
||||||
qs.push(
|
qs.push(
|
||||||
obj[key]
|
obj[key]
|
||||||
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()
|
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user