Skip to content

Commit

Permalink
feat: init ocsify-server-renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed May 29, 2017
1 parent f095eb8 commit 6dea685
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 7 deletions.
26 changes: 26 additions & 0 deletions build/build-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var rollup = require('rollup')
var buble = require('rollup-plugin-buble')
var commonjs = require('rollup-plugin-commonjs')
var isProd = process.argv[process.argv.length - 1] !== '--dev'

rollup
.rollup({
entry: 'packages/docsify-server-renderer/index.js',
plugins: [
buble(),
commonjs()
],
onwarn: function() {}
})
.then(function (bundle) {
var dest = 'packages/docsify-server-renderer/build.js'

console.log(dest)
bundle.write({
format: 'cjs',
dest: dest
})
})
.catch(function (err) {
console.error(err)
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build": "rm -rf lib themes && node build/build.js && mkdir lib/themes && mkdir themes && node build/build-css.js",
"dev:build": "rm -rf lib themes && mkdir themes && node build/build.js --dev && node build/build-css.js --dev",
"dev": "node app.js & nodemon -w src -e js,css --exec 'npm run dev:build'",
"build:ssr": "node build/build-ssr",
"test": "eslint src --fix"
},
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/docsify-server-renderer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build.js
node_modules
*.log
.git
49 changes: 49 additions & 0 deletions packages/docsify-server-renderer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# docsify-server-renderer

## Install

```bash
yarn add docsify-server-render
```

## Usage

```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'),
config: {
name: 'docsify',
repo: 'qingwei-li/docsify'
}
//,cache: () => {}
})

renderer.renderToString({ url })
.then(html => {})
.catch(err => {})
```

*index.template.html*

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>docsify</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//unpkg.com/docsify/themes/buble.css" title="buble" disabled>
</head>
<body>
<div id="app"></div>
<!--inject-docsify-config-->
<script src="//unpkg.com/docsify/lib/docsify.js"></script>
</body>
</html>
```
25 changes: 25 additions & 0 deletions packages/docsify-server-renderer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Compiler } from '../../src/core/render/compiler'
import { AbstractHistory } from '../../src/core/router/history/abstract'
import path from 'path'
import fs from 'fs'

export default class Renderer {
constructor ({
template,
path,
config,
cache
}) {
this.template = template
this.path = path
this.config = config
this.cache = cache

this.router = new AbstractHistory()
this.compiler = new Compiler(config, this.router)
}

renderToString(url) {
console.log(url)
}
}
17 changes: 17 additions & 0 deletions packages/docsify-server-renderer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "docsify-server-renderer",
"version": "4.0.0",
"description": "docsify server renderer",
"author": {
"name": "qingwei-li",
"email": "[email protected]",
"url": "https://github.com/QingWei-Li"
},
"homepage": "https://docsify.js.org",
"license": "MIT",
"repository": "QingWei-Li/docsify",
"main": "build.js",
"scripts": {
"test": "echo 'hello'"
}
}
5 changes: 1 addition & 4 deletions src/core/router/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AbstractHistory } from './history/abstract'
import { HashHistory } from './history/hash'
import { HTML5History } from './history/html5'
import { supportsPushState, inBrowser } from '../util/env'
import { supportsPushState } from '../util/env'

export function routerMixin (proto) {
proto.route = {}
Expand All @@ -16,8 +15,6 @@ export function initRouter (vm) {

if (mode === 'history' && supportsPushState) {
router = new HTML5History(config)
} else if (!inBrowser || mode === 'abstract') {
router = new AbstractHistory(config)
} else {
router = new HashHistory(config)
}
Expand Down
4 changes: 1 addition & 3 deletions src/core/util/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ export const isIE = UA && /msie|trident/.test(UA)

export const isMobile = document.body.clientWidth <= 600

export const inBrowser = typeof window !== 'undefined'

/**
* @see https://github.com/MoOx/pjax/blob/master/lib/is-supported.js
*/
export const supportsPushState = inBrowser && (function () {
export const supportsPushState = (function () {
// Borrowed wholesale from https://github.com/defunkt/jquery-pjax
return window.history &&
window.history.pushState &&
Expand Down

0 comments on commit 6dea685

Please sign in to comment.