refactor(ssr): return promise

This commit is contained in:
qingwei.li 2017-05-30 03:28:43 +08:00 committed by cinwell.li
parent 1f4514dcad
commit 1acfdd335c
4 changed files with 39 additions and 23 deletions

View File

@ -1,14 +1,12 @@
var rollup = require('rollup')
var buble = require('rollup-plugin-buble')
var commonjs = require('rollup-plugin-commonjs')
var async = require('rollup-plugin-async')
var isProd = process.argv[process.argv.length - 1] !== '--dev'
rollup
.rollup({
entry: 'packages/docsify-server-renderer/index.js',
plugins: [
buble(),
commonjs()
async()
],
onwarn: function() {}
})

View File

@ -41,6 +41,7 @@
"postcss": "^5.2.16",
"postcss-salad": "^1.0.8",
"rollup": "^0.41.6",
"rollup-plugin-async": "^1.2.0",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-node-resolve": "^2.0.0",

View File

@ -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 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) {
return resolve(process.cwd(), ...args)
@ -31,8 +33,8 @@ export default class Renderer {
cache
}) {
this.html = template
this.path = cwd(path)
this.config = Object.assign(config, {
this.path = cwd(path || './')
this.config = config = Object.assign({}, config, {
routerMode: 'history'
})
this.cache = cache
@ -47,24 +49,31 @@ export default class Renderer {
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
// TODO render cover page
const { loadSidebar, loadNavbar } = this.config
const mainFile = cwd(this.path, `./${this.router.getFile(url)}`)
this._renderHtml('main', this._render(mainFile))
const mainFile = this._getPath(url)
this._renderHtml('main', await this._render(mainFile))
if (loadSidebar) {
const name = loadSidebar === true ? '_sidebar.md' : loadSidebar
const sidebarFile = cwd(mainFile, '..', name)
this._renderHtml('sidebar', this._render(sidebarFile, 'sidebar'))
const sidebarFile = this._getPath(resolve(url, `../${name}`))
this._renderHtml('sidebar', await this._render(sidebarFile, 'sidebar'))
}
if (loadNavbar) {
const name = loadNavbar === true ? '_navbar.md' : loadNavbar
const navbarFile = cwd(mainFile, '..', name)
this._renderHtml('navbar', this._render(navbarFile, 'navbar'))
const navbarFile = this._getPath(resolve(url, `../${name}`))
this._renderHtml('navbar', await this._render(navbarFile, 'navbar'))
}
const html = this.html
@ -79,8 +88,8 @@ export default class Renderer {
return this.html
}
_render (path, type) {
let html = this._loadFile(path)
async _render (path, type) {
let html = await this._loadFile(path)
const { subMaxLevel, maxLevel } = this.config
switch (type) {
@ -103,9 +112,14 @@ export default class Renderer {
return html
}
_loadFile (filePath) {
async _loadFile (filePath) {
try {
return readFileSync(filePath, 'utf8')
if (isAbsolutePath(filePath)) {
const res = await fetch(filePath)
return await res.text()
} else {
return readFileSync(filePath, 'utf8')
}
} catch (e) {
const fileName = basename(filePath)
const parentPath = cwd(filePath, '../..')
@ -114,7 +128,7 @@ export default class Renderer {
throw Error(`Not found file ${fileName}`)
}
this._loadFile(cwd(filePath, '../..', fileName))
await this._loadFile(cwd(filePath, '../..', fileName))
}
}
}

View File

@ -13,5 +13,8 @@
"main": "build.js",
"scripts": {
"test": "echo 'hello'"
},
"dependencies": {
"node-fetch": "^1.7.0"
}
}