feat: finish ssr

This commit is contained in:
qingwei.li 2017-05-30 00:23:43 +08:00 committed by cinwell.li
commit 3444884905
12 changed files with 163 additions and 40 deletions

View file

@ -70,5 +70,12 @@ export function fetchMixin (proto) {
}
export function initFetch (vm) {
vm.$fetch(_ => callHook(vm, 'ready'))
if (vm.rendered) {
vm._fetchCover()
vm.$resetEvents()
callHook(vm, 'doneEach')
callHook(vm, 'ready')
} else {
vm.$fetch(_ => callHook(vm, 'ready'))
}
}

View file

@ -35,7 +35,7 @@ export class Compiler {
if (!text) return text
html = compile(text)
html = emojify(html)
html = config.noEmoji ? html : emojify(html)
slugify.clear()
return html

View file

@ -1,10 +1,12 @@
import { inBrowser } from '../util/env'
function replace (m, $1) {
return '<img class="emoji" src="https://assets-cdn.github.com/images/icons/emoji/' + $1 + '.png" alt="' + $1 + '" />'
}
export function emojify (text) {
return $docsify.noEmoji ? text : text
return text
.replace(/<(pre|template|code)[^>]*?>[\s\S]+?<\/(pre|template|code)>/g, m => m.replace(/:/g, '__colon__'))
.replace(/:(\w+?):/ig, window.emojify || replace)
.replace(/:(\w+?):/ig, (inBrowser && window.emojify) || replace)
.replace(/__colon__/g, ':')
}

View file

@ -172,20 +172,20 @@ export function initRender (vm) {
let html = ''
let navAppendToTarget = dom.body
if (!el) {
el = dom.create(id)
dom.appendTo(dom.body, el)
}
if (config.repo) {
html += tpl.corner(config.repo)
}
if (config.coverpage) {
html += tpl.cover()
}
if (el) {
if (config.repo) {
html += tpl.corner(config.repo)
}
if (config.coverpage) {
html += tpl.cover()
}
html += tpl.main(config)
// Render main app
vm._renderTo(el, html, true)
html += tpl.main(config)
// Render main app
vm._renderTo(el, html, true)
} else {
vm.rendered = true
}
if (config.mergeNavbar && isMobile) {
navAppendToTarget = dom.find('.sidebar')

View file

@ -33,12 +33,12 @@ export function main (config) {
(config.name
? `<h1><a class="app-name-link" data-nosearch>${config.name}</a></h1>`
: '') +
'<div class="sidebar-nav"></div>' +
'<div class="sidebar-nav"><!--sidebar--></div>' +
'</aside>')
return (isMobile ? `${aside}<main>` : `<main>${aside}`) +
'<section class="content">' +
'<article class="markdown-section" id="main"></article>' +
'<article class="markdown-section" id="main"><!--main--></article>' +
'</section>' +
'</main>'
}

View file

@ -1,8 +1,35 @@
import { History } from './base'
import { parseQuery, stringifyQuery, cleanPath } from '../util'
import { merge } from '../../util/core'
export class AbstractHistory extends History {
constructor (config) {
super(config)
this.mode = 'abstract'
}
parse (path) {
let query = ''
const queryIndex = path.indexOf('?')
if (queryIndex >= 0) {
query = path.slice(queryIndex + 1)
path = path.slice(0, queryIndex)
}
return { path, query: parseQuery(query) }
}
toURL (path, params, currentRoute) {
const local = currentRoute && path[0] === '#'
const route = this.parse(path)
route.query = merge({}, route.query, params)
path = route.path + stringifyQuery(route.query)
path = path.replace(/\.md(\?)|\.md$/, '$1')
if (local) path = currentRoute + path
return cleanPath('/' + path)
}
}

View file

@ -1,4 +1,5 @@
import { cached } from '../util/core'
import { inBrowser } from '../util/env'
const decode = decodeURIComponent
const encode = encodeURIComponent
@ -31,10 +32,13 @@ export function stringifyQuery (obj) {
return qs.length ? `?${qs.join('&')}` : ''
}
export const getBasePath = cached(base => {
export const getBasePath = cached((base = '') => {
// TODO
const path = inBrowser ? window.location.pathname : ''
return /^(\/|https?:)/g.test(base)
? base
: cleanPath(window.location.pathname + '/' + base)
: cleanPath(path + '/' + base)
})
export function getPath (...args) {

View file

@ -1,13 +1,11 @@
export const UA = window.navigator.userAgent.toLowerCase()
export const inBrowser = typeof window !== 'undefined'
export const isIE = UA && /msie|trident/.test(UA)
export const isMobile = document.body.clientWidth <= 600
export const isMobile = inBrowser && document.body.clientWidth <= 600
/**
* @see https://github.com/MoOx/pjax/blob/master/lib/is-supported.js
*/
export const supportsPushState = (function () {
export const supportsPushState = inBrowser && (function () {
// Borrowed wholesale from https://github.com/defunkt/jquery-pjax
return window.history &&
window.history.pushState &&