Skip to content

Commit ebc10c4

Browse files
committed
fix: ssr compatible embedd
1 parent b265fdd commit ebc10c4

File tree

7 files changed

+60
-39
lines changed

7 files changed

+60
-39
lines changed

package.json

+6-8
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
"url": "git+https://github.com/QingWei-Li/docsify.git"
1515
},
1616
"main": "lib/docsify.js",
17-
"files": [
18-
"lib",
19-
"src",
20-
"themes"
21-
],
17+
"files": ["lib", "src", "themes"],
2218
"scripts": {
2319
"bootstrap": "npm i && lerna bootstrap && npm run build:ssr",
24-
"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",
25-
"dev:build": "rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev",
20+
"build":
21+
"rm -rf lib themes && node build/build && mkdir lib/themes && mkdir themes && node build/build-css && npm run build:ssr && node build/build-cover",
22+
"dev:build":
23+
"rm -rf lib themes && mkdir themes && node build/build --dev && node build/build-css --dev",
2624
"dev": "node app & nodemon -w src -e js,css --exec 'npm run dev:build'",
2725
"build:ssr": "node build/build-ssr",
2826
"test": "eslint {src,packages} --fix",
@@ -70,4 +68,4 @@
7068
"url": "https://opencollective.com/docsify",
7169
"logo": "https://docsify.js.org/_media/icon.svg"
7270
}
73-
}
71+
}

src/core/fetch/ajax.js

-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import progressbar from '../render/progressbar'
22
import { noop } from '../util/core'
33

44
const cache = {}
5-
let uid = 0
65

76
/**
87
* Simple ajax get
@@ -25,7 +24,6 @@ export function get (url, hasBar = false) {
2524
xhr.send()
2625

2726
return {
28-
uid: uid++,
2927
then: function (success, error = noop) {
3028
if (hasBar) {
3129
const id = setInterval(

src/core/render/compiler.js

+40-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { isFn, merge, cached } from '../util/core'
99
import { get } from '../fetch/ajax'
1010

1111
const cachedLinks = {}
12+
let uid = 0
13+
1214
function getAndRemoveConfig (str = '') {
1315
const config = {}
1416

@@ -25,14 +27,23 @@ function getAndRemoveConfig (str = '') {
2527
}
2628
const compileMedia = {
2729
markdown (url, config) {
28-
const request = get(url, false)
29-
const id = `docsify-get-${request.uid}`
30+
const id = `docsify-get-${uid++}`
3031

31-
request.then(text => {
32-
document.getElementById(id).innerHTML = this.compile(text)
33-
})
32+
if (!process.env.SSR) {
33+
get(url, false).then(text => {
34+
document.getElementById(id).innerHTML = this.compile(text)
35+
})
3436

35-
return `<div data-origin="${url}" id=${id}></div>`
37+
return `<div data-origin="${url}" id=${id}></div>`
38+
} else {
39+
return `<div data-origin="${url}" id=${uid}></div>
40+
<script>
41+
var compile = window.__current_docsify_compiler__
42+
Docsify.get('${url}', false).then(function(text) {
43+
document.getElementById('${uid}').innerHTML = compile(text)
44+
})
45+
</script>`
46+
}
3647
},
3748
html (url, config) {
3849
return `<iframe src="${url}" ${config || 'width=100% height=400'}></iframe>`
@@ -44,19 +55,33 @@ const compileMedia = {
4455
return `<audio src="${url}" ${config || 'controls'}>Not Support</audio>`
4556
},
4657
code (url, config) {
47-
const request = get(url, false)
48-
const id = `docsify-get-${request.uid}`
58+
const id = `docsify-get-${uid++}`
4959
let ext = url.match(/\.(\w+)$/)
5060

51-
ext = config.ext || (ext && ext[0])
61+
ext = config.ext || (ext && ext[1])
62+
if (ext === 'md') ext = 'markdown'
5263

53-
request.then(text => {
54-
document.getElementById(id).innerHTML = this.compile(
55-
'```' + ext + '\n ' + text + '\n```\n'
56-
)
57-
})
64+
if (!process.env.SSR) {
65+
get(url, false).then(text => {
66+
document.getElementById(id).innerHTML = this.compile(
67+
'```' + ext + '\n' + text.replace(/`/g, '@qm@') + '\n```\n'
68+
).replace(/@qm@/g, '`')
69+
})
5870

59-
return `<div data-origin="${url}" id=${id}></div>`
71+
return `<div data-origin="${url}" id=${id}></div>`
72+
} else {
73+
return `<div data-origin="${url}" id=${id}></div>
74+
<script>
75+
setTimeout(() => {
76+
var compiler = window.__current_docsify_compiler__
77+
Docsify.get('${url}', false).then(function(text) {
78+
document.getElementById('${id}').innerHTML = compiler
79+
.compile('\`\`\`${ext}\\n' + text.replace(/\`/g, '@qm@') + '\\n\`\`\`\\n')
80+
.replace(/@qm@/g, '\`')
81+
})
82+
})
83+
</script>`
84+
}
6085
}
6186
}
6287

src/core/render/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { callHook } from '../init/lifecycle'
66
import { Compiler } from './compiler'
77
import { getAndActive, sticky } from '../event/sidebar'
88
import { getPath, isAbsolutePath } from '../router/util'
9-
import { isMobile } from '../util/env'
9+
import { isMobile, inBrowser } from '../util/env'
1010
import { isPrimitive } from '../util/core'
1111
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
1212

@@ -38,7 +38,6 @@ function renderMain (html) {
3838
html = 'not found'
3939
}
4040

41-
dom.toggleClass(dom.getNode('main'), 'add', 'ready')
4241
this._renderTo('.markdown-section', html)
4342
// Render sidebar with the TOC
4443
!this.config.loadSidebar && this._renderSidebar()
@@ -180,6 +179,9 @@ export function initRender (vm) {
180179

181180
// Init markdown compiler
182181
vm.compiler = new Compiler(config, vm.router)
182+
if (inBrowser) {
183+
window['__current_docsify_compiler__'] = vm.compiler
184+
}
183185

184186
const id = config.el || '#app'
185187
const navEl = dom.find('nav') || dom.create('nav')

src/core/util/dom.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isFn } from '../util/core'
2+
import { inBrowser } from './env'
23

34
const cacheNode = {}
45

@@ -13,17 +14,17 @@ export function getNode (el, noCache = false) {
1314
if (typeof window.Vue !== 'undefined') {
1415
return find(el)
1516
}
16-
el = noCache ? find(el) : (cacheNode[el] || (cacheNode[el] = find(el)))
17+
el = noCache ? find(el) : cacheNode[el] || (cacheNode[el] = find(el))
1718
}
1819

1920
return el
2021
}
2122

22-
export const $ = document
23+
export const $ = inBrowser && document
2324

24-
export const body = $.body
25+
export const body = inBrowser && $.body
2526

26-
export const head = $.head
27+
export const head = inBrowser && $.head
2728

2829
/**
2930
* Find element
@@ -42,7 +43,9 @@ export function find (el, node) {
4243
* findAll(nav, 'a') => [].slice.call(nav.querySelectorAll('a'))
4344
*/
4445
export function findAll (el, node) {
45-
return [].slice.call(node ? el.querySelectorAll(node) : $.querySelectorAll(el))
46+
return [].slice.call(
47+
node ? el.querySelectorAll(node) : $.querySelectorAll(el)
48+
)
4649
}
4750

4851
export function create (node, tpl) {
@@ -85,4 +88,3 @@ export function toggleClass (el, type, val) {
8588
export function style (content) {
8689
appendTo(head, create('style', content))
8790
}
88-

src/core/util/env.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const inBrowser = typeof window !== 'undefined'
1+
export const inBrowser = !process.env.SSR
22

33
export const isMobile = inBrowser && document.body.clientWidth <= 600
44

src/themes/basic/_layout.css

+1-5
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,12 @@ li input[type='checkbox'] {
217217

218218
/* main */
219219
main {
220-
display: none;
220+
display: block;
221221
position: relative;
222222
size: 100vw 100%;
223223
z-index: 0;
224224
}
225225

226-
main.ready {
227-
display: block;
228-
}
229-
230226
.anchor {
231227
display: inline-block;
232228
text-decoration: none;

0 commit comments

Comments
 (0)