refactor(ssr): path => context
This commit is contained in:
parent
891475e132
commit
d4f6072d10
5 changed files with 88 additions and 39 deletions
|
|
@ -11,17 +11,15 @@ yarn add docsify-server-render
|
|||
```js
|
||||
var Renderer = require('docsify-server-renderer')
|
||||
var readFileSync = require('fs').readFileSync
|
||||
var resolve = require('path').resolve
|
||||
|
||||
// init
|
||||
var renderer = new Renderer({
|
||||
template: readFileSync('./index.template.html', 'utf-8').,
|
||||
path: resolve(_dirname, './docs'),
|
||||
template: readFileSync('./docs/index.template.html', 'utf-8').,
|
||||
context: './docs',
|
||||
config: {
|
||||
name: 'docsify',
|
||||
repo: 'qingwei-li/docsify'
|
||||
}
|
||||
//,cache: () => {}
|
||||
})
|
||||
|
||||
renderer.renderToString({ url })
|
||||
|
|
|
|||
|
|
@ -28,12 +28,12 @@ function mainTpl (config) {
|
|||
export default class Renderer {
|
||||
constructor ({
|
||||
template,
|
||||
path,
|
||||
context,
|
||||
config,
|
||||
cache
|
||||
}) {
|
||||
this.html = template
|
||||
this.path = cwd(path || './')
|
||||
this.context = cwd(context || './')
|
||||
this.config = config = Object.assign({}, config, {
|
||||
routerMode: 'history'
|
||||
})
|
||||
|
|
@ -54,7 +54,7 @@ export default class Renderer {
|
|||
|
||||
return isAbsolutePath(file)
|
||||
? file
|
||||
: cwd(this.path, `./${file}`)
|
||||
: cwd(this.context, `./${file}`)
|
||||
}
|
||||
|
||||
async renderToString (url) {
|
||||
|
|
@ -114,6 +114,12 @@ export default class Renderer {
|
|||
|
||||
async _loadFile (filePath) {
|
||||
try {
|
||||
this.lock = this.lock || 0
|
||||
if (++this.lock > 10) {
|
||||
this.lock = 0
|
||||
return
|
||||
}
|
||||
|
||||
if (isAbsolutePath(filePath)) {
|
||||
const res = await fetch(filePath)
|
||||
return await res.text()
|
||||
|
|
@ -124,7 +130,7 @@ export default class Renderer {
|
|||
const fileName = basename(filePath)
|
||||
const parentPath = cwd(filePath, '../..')
|
||||
|
||||
if (this.path.length < parentPath.length) {
|
||||
if (this.context.length < parentPath.length) {
|
||||
throw Error(`Not found file ${fileName}`)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue