Skip to content

Commit 4626157

Browse files
committed
fix(ssr): remove context
1 parent d53b8da commit 4626157

File tree

12 files changed

+58
-59
lines changed

12 files changed

+58
-59
lines changed

docs/ssr.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
mergeNavbar: true,
1414
maxLevel: 4,
1515
subMaxLevel: 2,
16+
basePath: '/docs/',
1617
name: 'docsify',
1718
search: {
1819
noData: {
@@ -28,6 +29,5 @@ module.exports = {
2829
}
2930
}
3031
},
31-
context: './docs',
32-
template: './ssr.html'
32+
template: './docs/ssr.html'
3333
}

docs/ssr.html

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<meta name="description" content="A magical documentation generator.">
99
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
1010
<link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/vue.css" title="vue">
11-
<link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/dark.css" title="dark" disabled>
12-
<link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/buble.css" title="buble" disabled>
13-
<link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/pure.css" title="pure" disabled>
11+
<!-- <link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/dark.css" title="dark" disabled> -->
12+
<!-- <link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/buble.css" title="buble" disabled> -->
13+
<!-- <link rel="stylesheet" href="//unpkg.com/docsify@next/lib/themes/pure.css" title="pure" disabled> -->
1414
<style>
1515
nav.app-nav li ul {
1616
min-width: 100px;
@@ -21,9 +21,9 @@
2121
<!--inject-app-->
2222
<!--inject-config-->
2323
</body>
24-
<script src="//unpkg.com/docsify@next/lib/docsify.min.js"></script>
25-
<script src="//unpkg.com/docsify@next/lib/plugins/search.min.js"></script>
26-
<script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script>
27-
<script src="//unpkg.com/prismjs/components/prism-markdown.min.js"></script>
28-
<script src="//unpkg.com/prismjs/components/prism-nginx.min.js"></script>
24+
<script src="/lib/docsify.js"></script>
25+
<script src="/lib/plugins/search.js"></script>
26+
<!-- <script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script> -->
27+
<!-- <script src="//unpkg.com/prismjs/components/prism-markdown.min.js"></script> -->
28+
<!-- <script src="//unpkg.com/prismjs/components/prism-nginx.min.js"></script> -->
2929
</html>

packages/docsify-server-renderer/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var readFileSync = require('fs').readFileSync
1515
// init
1616
var renderer = new Renderer({
1717
template: readFileSync('./docs/index.template.html', 'utf-8').,
18-
context: './docs',
1918
config: {
2019
name: 'docsify',
2120
repo: 'qingwei-li/docsify'

packages/docsify-server-renderer/index.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function cwd (...args) {
1111
}
1212

1313
function mainTpl (config) {
14-
let html = `<nav class="app-nav${config.repo ? '' : 'no-badge'}"><!--navbar--></nav>`
14+
let html = `<nav class="app-nav${config.repo ? '' : ' no-badge'}"><!--navbar--></nav>`
1515

1616
if (config.repo) {
1717
html += tpl.corner(config.repo)
@@ -28,12 +28,10 @@ function mainTpl (config) {
2828
export default class Renderer {
2929
constructor ({
3030
template,
31-
context,
3231
config,
3332
cache
3433
}) {
3534
this.html = template
36-
this.context = cwd(context || './')
3735
this.config = config = Object.assign({}, config, {
3836
routerMode: 'history'
3937
})
@@ -54,7 +52,7 @@ export default class Renderer {
5452

5553
return isAbsolutePath(file)
5654
? file
57-
: cwd(this.context, `./${file}`)
55+
: cwd(`./${file}`)
5856
}
5957

6058
async renderToString (url) {
@@ -113,27 +111,27 @@ export default class Renderer {
113111
}
114112

115113
async _loadFile (filePath) {
114+
let content
116115
try {
116+
if (isAbsolutePath(filePath)) {
117+
const res = await fetch(filePath)
118+
content = await res.text()
119+
this.lock = 0
120+
} else {
121+
content = await readFileSync(filePath, 'utf8')
122+
this.lock = 0
123+
}
124+
return content
125+
} catch (e) {
117126
this.lock = this.lock || 0
118127
if (++this.lock > 10) {
119128
this.lock = 0
120129
return
121130
}
122131

123-
if (isAbsolutePath(filePath)) {
124-
const res = await fetch(filePath)
125-
return await res.text()
126-
} else {
127-
return readFileSync(filePath, 'utf8')
128-
}
129-
} catch (e) {
130132
const fileName = basename(filePath)
131133
const parentPath = cwd(filePath, '../..')
132134

133-
if (this.context.length < parentPath.length) {
134-
throw Error(`Not found file ${fileName}`)
135-
}
136-
137135
await this._loadFile(cwd(filePath, '../..', fileName))
138136
}
139137
}

src/core/fetch/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function fetchMixin (proto) {
5050
const root = getParentPath(this.route.path)
5151
const path = this.router.getFile(root + coverpage)
5252

53+
console.log(this.route.path, root, path)
5354
if (this.route.path !== '/' || !coverpage) {
5455
this._renderCover()
5556
return

src/core/render/compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { helper as helperTpl, tree as treeTpl } from './tpl'
44
import { genTree } from './gen-tree'
55
import { slugify } from './slugify'
66
import { emojify } from './emojify'
7-
import { getBasePath, isAbsolutePath, getPath } from '../router/util'
7+
import { isAbsolutePath, getPath } from '../router/util'
88
import { isFn, merge, cached } from '../util/core'
99

1010
export class Compiler {
@@ -14,7 +14,7 @@ export class Compiler {
1414
this.cacheTree = {}
1515
this.toc = []
1616
this.linkTarget = config.externalLinkTarget || '_blank'
17-
this.contentBase = getBasePath(config.basePath)
17+
this.contentBase = router.getBasePath()
1818

1919
const renderer = this._initRenderer()
2020
let compile

src/core/render/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import tinydate from 'tinydate'
55
import { callHook } from '../init/lifecycle'
66
import { Compiler } from './compiler'
77
import { getAndActive, sticky } from '../event/sidebar'
8-
import { getBasePath, getPath, isAbsolutePath } from '../router/util'
8+
import { getPath, isAbsolutePath } from '../router/util'
99
import { isMobile } from '../util/env'
1010
import { isPrimitive } from '../util/core'
1111
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
@@ -143,7 +143,7 @@ export function renderMixin (proto) {
143143

144144
dom.toggleClass(el, 'add', 'has-mask')
145145
if (!isAbsolutePath(m[1])) {
146-
path = getPath(getBasePath(this.config.basePath), m[1])
146+
path = getPath(vm.router.getBasePath(), m[1])
147147
}
148148
el.style.backgroundImage = `url(${path})`
149149
el.style.backgroundSize = 'cover'

src/core/router/history/base.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getBasePath, getPath, isAbsolutePath } from '../util'
1+
import { getPath, isAbsolutePath } from '../util'
22
import { noop } from '../../util/core'
33

44
function getAlias (path, alias) {
@@ -18,9 +18,13 @@ export class History {
1818
this.config = config
1919
}
2020

21+
getBasePath() {
22+
return this.config.basePath
23+
}
24+
2125
getFile (path) {
2226
const { config } = this
23-
const base = getBasePath(config.basePath)
27+
const base = this.getBasePath()
2428

2529
path = config.alias ? getAlias(path, config.alias) : path
2630
path = getFileName(path)

src/core/router/history/hash.js

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ export class HashHistory extends History {
2020
this.mode = 'hash'
2121
}
2222

23+
getBasePath() {
24+
const path = window.location.pathname || ''
25+
const base = this.config.basePath
26+
27+
return /^(\/|https?:)/g.test(base)
28+
? base
29+
: cleanPath(path + '/' + base)
30+
}
31+
2332
getCurrentPath () {
2433
// We can't use location.hash here because it's not
2534
// consistent across browsers - Firefox will pre-decode it!

src/core/router/util.js

-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { cached } from '../util/core'
2-
import { inBrowser } from '../util/env'
32

43
const decode = decodeURIComponent
54
const encode = encodeURIComponent
@@ -32,15 +31,6 @@ export function stringifyQuery (obj) {
3231
return qs.length ? `?${qs.join('&')}` : ''
3332
}
3433

35-
export const getBasePath = cached((base = '') => {
36-
// TODO
37-
const path = inBrowser ? window.location.pathname : ''
38-
39-
return /^(\/|https?:)/g.test(base)
40-
? base
41-
: cleanPath(path + '/' + base)
42-
})
43-
4434
export function getPath (...args) {
4535
return cleanPath(args.join('/'))
4636
}

src/plugins/search/component.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { search } from './search'
22

3-
let dom
43
let NO_DATA_TEXT = ''
54

65
function style () {
@@ -65,25 +64,25 @@ function style () {
6564
.search p.empty {
6665
text-align: center;
6766
}`
68-
const style = dom.create('style', code)
69-
dom.appendTo(dom.head, style)
67+
const style = Docsify.dom.create('style', code)
68+
Docsify.dom.appendTo(Docsify.dom.head, style)
7069
}
7170

7271
function tpl (opts, defaultValue = '') {
7372
const html =
7473
`<input type="search" value="${defaultValue}" />` +
7574
'<div class="results-panel"></div>' +
7675
'</div>'
77-
const el = dom.create('div', html)
78-
const aside = dom.find('aside')
76+
const el = Docsify.dom.create('div', html)
77+
const aside = Docsify.dom.find('aside')
7978

80-
dom.toggleClass(el, 'search')
81-
dom.before(aside, el)
79+
Docsify.dom.toggleClass(el, 'search')
80+
Docsify.dom.before(aside, el)
8281
}
8382

8483
function doSearch (value) {
85-
const $search = dom.find('div.search')
86-
const $panel = dom.find($search, '.results-panel')
84+
const $search = Docsify.dom.find('div.search')
85+
const $panel = Docsify.dom.find($search, '.results-panel')
8786

8887
if (!value) {
8988
$panel.classList.remove('show')
@@ -105,22 +104,23 @@ function doSearch (value) {
105104
}
106105

107106
function bindEvents () {
108-
const $search = dom.find('div.search')
109-
const $input = dom.find($search, 'input')
107+
const $search = Docsify.dom.find('div.search')
108+
const $input = Docsify.dom.find($search, 'input')
110109

111110
let timeId
112111
// Prevent to Fold sidebar
113-
dom.on($search, 'click',
112+
Docsify.dom.on($search, 'click',
114113
e => e.target.tagName !== 'A' && e.stopPropagation())
115-
dom.on($input, 'input', e => {
114+
Docsify.dom.on($input, 'input', e => {
116115
clearTimeout(timeId)
117116
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
118117
})
119118
}
120119

121120
function updatePlaceholder (text, path) {
122-
const $input = dom.getNode('.search input[type="search"]')
121+
const $input = Docsify.dom.getNode('.search input[type="search"]')
123122

123+
if (!$input) return
124124
if (typeof text === 'string') {
125125
$input.placeholder = text
126126
} else {
@@ -139,7 +139,6 @@ function updateNoData (text, path) {
139139
}
140140

141141
export function init (opts, vm) {
142-
dom = Docsify.dom
143142
const keywords = vm.router.parse().query.s
144143

145144
style()

src/plugins/search/search.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ function saveData (maxAge) {
4141
export function genIndex (path, content = '', router) {
4242
const tokens = window.marked.lexer(content)
4343
const slugify = window.Docsify.slugify
44-
const toURL = router.toURL
4544
const index = {}
4645
let slug
4746

4847
tokens.forEach(token => {
4948
if (token.type === 'heading' && token.depth <= 2) {
50-
slug = toURL(path, { id: slugify(token.text) })
49+
slug = router.toURL(path, { id: slugify(token.text) })
5150
index[slug] = { slug, title: token.text, body: '' }
5251
} else {
5352
if (!slug) return

0 commit comments

Comments
 (0)