mirror of
https://github.com/zhigang1992/docsify.git
synced 2026-04-28 17:35:53 +08:00
refactor(ssr): return promise
This commit is contained in:
@@ -1,14 +1,12 @@
|
|||||||
var rollup = require('rollup')
|
var rollup = require('rollup')
|
||||||
var buble = require('rollup-plugin-buble')
|
var async = require('rollup-plugin-async')
|
||||||
var commonjs = require('rollup-plugin-commonjs')
|
|
||||||
var isProd = process.argv[process.argv.length - 1] !== '--dev'
|
var isProd = process.argv[process.argv.length - 1] !== '--dev'
|
||||||
|
|
||||||
rollup
|
rollup
|
||||||
.rollup({
|
.rollup({
|
||||||
entry: 'packages/docsify-server-renderer/index.js',
|
entry: 'packages/docsify-server-renderer/index.js',
|
||||||
plugins: [
|
plugins: [
|
||||||
buble(),
|
async()
|
||||||
commonjs()
|
|
||||||
],
|
],
|
||||||
onwarn: function() {}
|
onwarn: function() {}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,6 +41,7 @@
|
|||||||
"postcss": "^5.2.16",
|
"postcss": "^5.2.16",
|
||||||
"postcss-salad": "^1.0.8",
|
"postcss-salad": "^1.0.8",
|
||||||
"rollup": "^0.41.6",
|
"rollup": "^0.41.6",
|
||||||
|
"rollup-plugin-async": "^1.2.0",
|
||||||
"rollup-plugin-buble": "^0.15.0",
|
"rollup-plugin-buble": "^0.15.0",
|
||||||
"rollup-plugin-commonjs": "^8.0.2",
|
"rollup-plugin-commonjs": "^8.0.2",
|
||||||
"rollup-plugin-node-resolve": "^2.0.0",
|
"rollup-plugin-node-resolve": "^2.0.0",
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Compiler } from '../../src/core/render/compiler'
|
|
||||||
import { AbstractHistory } from '../../src/core/router/history/abstract'
|
|
||||||
import { resolve, basename } from 'path'
|
|
||||||
import { readFileSync } from 'fs'
|
|
||||||
import * as tpl from '../../src/core/render/tpl'
|
import * as tpl from '../../src/core/render/tpl'
|
||||||
|
import fetch from 'node-fetch'
|
||||||
|
import { AbstractHistory } from '../../src/core/router/history/abstract'
|
||||||
|
import { Compiler } from '../../src/core/render/compiler'
|
||||||
|
import { isAbsolutePath } from '../../src/core/router/util'
|
||||||
|
import { readFileSync } from 'fs'
|
||||||
|
import { resolve, basename } from 'path'
|
||||||
|
|
||||||
function cwd (...args) {
|
function cwd (...args) {
|
||||||
return resolve(process.cwd(), ...args)
|
return resolve(process.cwd(), ...args)
|
||||||
@@ -31,8 +33,8 @@ export default class Renderer {
|
|||||||
cache
|
cache
|
||||||
}) {
|
}) {
|
||||||
this.html = template
|
this.html = template
|
||||||
this.path = cwd(path)
|
this.path = cwd(path || './')
|
||||||
this.config = Object.assign(config, {
|
this.config = config = Object.assign({}, config, {
|
||||||
routerMode: 'history'
|
routerMode: 'history'
|
||||||
})
|
})
|
||||||
this.cache = cache
|
this.cache = cache
|
||||||
@@ -47,24 +49,31 @@ export default class Renderer {
|
|||||||
this.template = this.html
|
this.template = this.html
|
||||||
}
|
}
|
||||||
|
|
||||||
renderToString (url) {
|
_getPath(url) {
|
||||||
|
const file = this.router.getFile(url)
|
||||||
|
|
||||||
|
return isAbsolutePath(file)
|
||||||
|
? file
|
||||||
|
: cwd(this.path, `./${file}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
async renderToString (url) {
|
||||||
this.url = url = this.router.parse(url).path
|
this.url = url = this.router.parse(url).path
|
||||||
// TODO render cover page
|
|
||||||
const { loadSidebar, loadNavbar } = this.config
|
const { loadSidebar, loadNavbar } = this.config
|
||||||
|
|
||||||
const mainFile = cwd(this.path, `./${this.router.getFile(url)}`)
|
const mainFile = this._getPath(url)
|
||||||
this._renderHtml('main', this._render(mainFile))
|
this._renderHtml('main', await this._render(mainFile))
|
||||||
|
|
||||||
if (loadSidebar) {
|
if (loadSidebar) {
|
||||||
const name = loadSidebar === true ? '_sidebar.md' : loadSidebar
|
const name = loadSidebar === true ? '_sidebar.md' : loadSidebar
|
||||||
const sidebarFile = cwd(mainFile, '..', name)
|
const sidebarFile = this._getPath(resolve(url, `../${name}`))
|
||||||
this._renderHtml('sidebar', this._render(sidebarFile, 'sidebar'))
|
this._renderHtml('sidebar', await this._render(sidebarFile, 'sidebar'))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadNavbar) {
|
if (loadNavbar) {
|
||||||
const name = loadNavbar === true ? '_navbar.md' : loadNavbar
|
const name = loadNavbar === true ? '_navbar.md' : loadNavbar
|
||||||
const navbarFile = cwd(mainFile, '..', name)
|
const navbarFile = this._getPath(resolve(url, `../${name}`))
|
||||||
this._renderHtml('navbar', this._render(navbarFile, 'navbar'))
|
this._renderHtml('navbar', await this._render(navbarFile, 'navbar'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const html = this.html
|
const html = this.html
|
||||||
@@ -79,8 +88,8 @@ export default class Renderer {
|
|||||||
return this.html
|
return this.html
|
||||||
}
|
}
|
||||||
|
|
||||||
_render (path, type) {
|
async _render (path, type) {
|
||||||
let html = this._loadFile(path)
|
let html = await this._loadFile(path)
|
||||||
const { subMaxLevel, maxLevel } = this.config
|
const { subMaxLevel, maxLevel } = this.config
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -103,9 +112,14 @@ export default class Renderer {
|
|||||||
return html
|
return html
|
||||||
}
|
}
|
||||||
|
|
||||||
_loadFile (filePath) {
|
async _loadFile (filePath) {
|
||||||
try {
|
try {
|
||||||
return readFileSync(filePath, 'utf8')
|
if (isAbsolutePath(filePath)) {
|
||||||
|
const res = await fetch(filePath)
|
||||||
|
return await res.text()
|
||||||
|
} else {
|
||||||
|
return readFileSync(filePath, 'utf8')
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const fileName = basename(filePath)
|
const fileName = basename(filePath)
|
||||||
const parentPath = cwd(filePath, '../..')
|
const parentPath = cwd(filePath, '../..')
|
||||||
@@ -114,7 +128,7 @@ export default class Renderer {
|
|||||||
throw Error(`Not found file ${fileName}`)
|
throw Error(`Not found file ${fileName}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
this._loadFile(cwd(filePath, '../..', fileName))
|
await this._loadFile(cwd(filePath, '../..', fileName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,5 +13,8 @@
|
|||||||
"main": "build.js",
|
"main": "build.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo 'hello'"
|
"test": "echo 'hello'"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": "^1.7.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user