fix: ssr compatible embedd

This commit is contained in:
qingwei.li 2018-02-10 16:09:01 +08:00 • committed by cinwell.li
commit ebc10c4e43
7 changed files with 60 additions and 39 deletions

View file

@ -14,15 +14,13 @@
"url": "git+https://github.com/QingWei-Li/docsify.git" "url": "git+https://github.com/QingWei-Li/docsify.git"
}, },
"main": "lib/docsify.js", "main": "lib/docsify.js",
"files": [ "files": ["lib", "src", "themes"],
"lib",
"src",
"themes"
],
"scripts": { "scripts": {
"bootstrap": "npm i && lerna bootstrap && npm run build:ssr", "bootstrap": "npm i && lerna bootstrap && npm run build:ssr",
"build": "rm -rf lib themes && node build/build && mkdir lib/themes && mkdir themes && node build/build-css && npm run build:ssr && node build/build-cover", "build":
"dev:build": "rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev", "rm -rf lib themes && node build/build && mkdir lib/themes && mkdir themes && node build/build-css && npm run build:ssr && node build/build-cover",
"dev:build":
"rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev",
"dev": "node app & nodemon -w src -e js,css --exec 'npm run dev:build'", "dev": "node app & nodemon -w src -e js,css --exec 'npm run dev:build'",
"build:ssr": "node build/build-ssr", "build:ssr": "node build/build-ssr",
"test": "eslint {src,packages} --fix", "test": "eslint {src,packages} --fix",

View file

@ -2,7 +2,6 @@ import progressbar from '../render/progressbar'
import { noop } from '../util/core' import { noop } from '../util/core'
const cache = {} const cache = {}
let uid = 0
/** /**
* Simple ajax get * Simple ajax get
@ -25,7 +24,6 @@ export function get (url, hasBar = false) {
xhr.send() xhr.send()
return { return {
uid: uid++,
then: function (success, error = noop) { then: function (success, error = noop) {
if (hasBar) { if (hasBar) {
const id = setInterval( const id = setInterval(

View file

@ -9,6 +9,8 @@ import { isFn, merge, cached } from '../util/core'
import { get } from '../fetch/ajax' import { get } from '../fetch/ajax'
const cachedLinks = {} const cachedLinks = {}
let uid = 0
function getAndRemoveConfig (str = '') { function getAndRemoveConfig (str = '') {
const config = {} const config = {}
@ -25,14 +27,23 @@ function getAndRemoveConfig (str = '') {
} }
const compileMedia = { const compileMedia = {
markdown (url, config) { markdown (url, config) {
const request = get(url, false) const id = `docsify-get-${uid++}`
const id = `docsify-get-${request.uid}`
request.then(text => { if (!process.env.SSR) {
get(url, false).then(text => {
document.getElementById(id).innerHTML = this.compile(text) document.getElementById(id).innerHTML = this.compile(text)
}) })
return `<div data-origin="${url}" id=${id}></div>` return `<div data-origin="${url}" id=${id}></div>`
} else {
return `<div data-origin="${url}" id=${uid}></div>
<script>
var compile = window.__current_docsify_compiler__
Docsify.get('${url}', false).then(function(text) {
document.getElementById('${uid}').innerHTML = compile(text)
})
</script>`
}
}, },
html (url, config) { html (url, config) {
return `<iframe src="${url}" ${config || 'width=100% height=400'}></iframe>` return `<iframe src="${url}" ${config || 'width=100% height=400'}></iframe>`
@ -44,19 +55,33 @@ const compileMedia = {
return `<audio src="${url}" ${config || 'controls'}>Not Support</audio>` return `<audio src="${url}" ${config || 'controls'}>Not Support</audio>`
}, },
code (url, config) { code (url, config) {
const request = get(url, false) const id = `docsify-get-${uid++}`
const id = `docsify-get-${request.uid}`
let ext = url.match(/\.(\w+)$/) let ext = url.match(/\.(\w+)$/)
ext = config.ext || (ext && ext[0]) ext = config.ext || (ext && ext[1])
if (ext === 'md') ext = 'markdown'
request.then(text => { if (!process.env.SSR) {
get(url, false).then(text => {
document.getElementById(id).innerHTML = this.compile( document.getElementById(id).innerHTML = this.compile(
'```' + ext + '\n ' + text + '\n```\n' '```' + ext + '\n' + text.replace(/`/g, '@qm@') + '\n```\n'
) ).replace(/@qm@/g, '`')
}) })
return `<div data-origin="${url}" id=${id}></div>` return `<div data-origin="${url}" id=${id}></div>`
} else {
return `<div data-origin="${url}" id=${id}></div>
<script>
setTimeout(() => {
var compiler = window.__current_docsify_compiler__
Docsify.get('${url}', false).then(function(text) {
document.getElementById('${id}').innerHTML = compiler
.compile('\`\`\`${ext}\\n' + text.replace(/\`/g, '@qm@') + '\\n\`\`\`\\n')
.replace(/@qm@/g, '\`')
})
})
</script>`
}
} }
} }

View file

@ -6,7 +6,7 @@ import { callHook } from '../init/lifecycle'
import { Compiler } from './compiler' import { Compiler } from './compiler'
import { getAndActive, sticky } from '../event/sidebar' import { getAndActive, sticky } from '../event/sidebar'
import { getPath, isAbsolutePath } from '../router/util' import { getPath, isAbsolutePath } from '../router/util'
import { isMobile } from '../util/env' import { isMobile, inBrowser } from '../util/env'
import { isPrimitive } from '../util/core' import { isPrimitive } from '../util/core'
import { scrollActiveSidebar, scroll2Top } from '../event/scroll' import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
@ -38,7 +38,6 @@ function renderMain (html) {
html = 'not found' html = 'not found'
} }
dom.toggleClass(dom.getNode('main'), 'add', 'ready')
this._renderTo('.markdown-section', html) this._renderTo('.markdown-section', html)
// Render sidebar with the TOC // Render sidebar with the TOC
!this.config.loadSidebar && this._renderSidebar() !this.config.loadSidebar && this._renderSidebar()
@ -180,6 +179,9 @@ export function initRender (vm) {
// Init markdown compiler // Init markdown compiler
vm.compiler = new Compiler(config, vm.router) vm.compiler = new Compiler(config, vm.router)
if (inBrowser) {
window['__current_docsify_compiler__'] = vm.compiler
}
const id = config.el || '#app' const id = config.el || '#app'
const navEl = dom.find('nav') || dom.create('nav') const navEl = dom.find('nav') || dom.create('nav')

View file

@ -1,4 +1,5 @@
import { isFn } from '../util/core' import { isFn } from '../util/core'
import { inBrowser } from './env'
const cacheNode = {} const cacheNode = {}
@ -13,17 +14,17 @@ export function getNode (el, noCache = false) {
if (typeof window.Vue !== 'undefined') { if (typeof window.Vue !== 'undefined') {
return find(el) return find(el)
} }
el = noCache ? find(el) : (cacheNode[el] || (cacheNode[el] = find(el))) el = noCache ? find(el) : cacheNode[el] || (cacheNode[el] = find(el))
} }
return el return el
} }
export const $ = document export const $ = inBrowser && document
export const body = $.body export const body = inBrowser && $.body
export const head = $.head export const head = inBrowser && $.head
/** /**
* Find element * Find element
@ -42,7 +43,9 @@ export function find (el, node) {
* findAll(nav, 'a') => [].slice.call(nav.querySelectorAll('a')) * findAll(nav, 'a') => [].slice.call(nav.querySelectorAll('a'))
*/ */
export function findAll (el, node) { export function findAll (el, node) {
return [].slice.call(node ? el.querySelectorAll(node) : $.querySelectorAll(el)) return [].slice.call(
node ? el.querySelectorAll(node) : $.querySelectorAll(el)
)
} }
export function create (node, tpl) { export function create (node, tpl) {
@ -85,4 +88,3 @@ export function toggleClass (el, type, val) {
export function style (content) { export function style (content) {
appendTo(head, create('style', content)) appendTo(head, create('style', content))
} }

View file

@ -1,4 +1,4 @@
export const inBrowser = typeof window !== 'undefined' export const inBrowser = !process.env.SSR
export const isMobile = inBrowser && document.body.clientWidth <= 600 export const isMobile = inBrowser && document.body.clientWidth <= 600

View file

@ -217,16 +217,12 @@ li input[type='checkbox'] {
/* main */ /* main */
main { main {
display: none; display: block;
position: relative; position: relative;
size: 100vw 100%; size: 100vw 100%;
z-index: 0; z-index: 0;
} }
main.ready {
display: block;
}
.anchor { .anchor {
display: inline-block; display: inline-block;
text-decoration: none; text-decoration: none;