Skip to content

Commit

Permalink
fix(embed): compatible ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 12, 2018
1 parent 62ce447 commit dc0c3ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
16 changes: 15 additions & 1 deletion packages/docsify-server-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { readFileSync } from 'fs'
import { resolve, basename } from 'path'
import resolvePathname from 'resolve-pathname'
import debug from 'debug'
import { prerenderEmbed } from '../../src/core/render/embed'

function cwd (...args) {
return resolve(process.cwd(), ...args)
Expand Down Expand Up @@ -61,7 +62,7 @@ export default class Renderer {
const { loadSidebar, loadNavbar, coverpage } = this.config

const mainFile = this._getPath(url)
this._renderHtml('main', await this._render(mainFile))
this._renderHtml('main', await this._render(mainFile, 'main'))

if (loadSidebar) {
const name = loadSidebar === true ? '_sidebar.md' : loadSidebar
Expand Down Expand Up @@ -120,6 +121,19 @@ export default class Renderer {
case 'cover':
html = this.compiler.cover(html)
break
case 'main':
const tokens = await new Promise(r => {
prerenderEmbed(
{
fetch: url => this._loadFile(this._getPath(url)),
compiler: this.compiler,
raw: html
},
r
)
})
html = this.compiler.compile(tokens)
break
case 'navbar':
case 'article':
default:
Expand Down
3 changes: 2 additions & 1 deletion src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class Compiler {

if (config.include) {
if (!isAbsolutePath(href)) {
href = getPath(this.contentBase, href)
href = getPath(process.env.SSR ? '' : this.contentBase, href)
}

let media
Expand Down Expand Up @@ -182,6 +182,7 @@ export class Compiler {
}
// highlight code
origin.code = renderer.code = function (code, lang = '') {
code = code.replace(/@DOCSIFY_QM@/g, '`')
const hl = Prism.highlight(
code,
Prism.languages[lang] || Prism.languages.markup
Expand Down
42 changes: 25 additions & 17 deletions src/core/render/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@ import { merge } from '../util/core'

const cached = {}

function walkFetchEmbed ({ step = 0, embedTokens, compile }, cb) {
function walkFetchEmbed ({ step = 0, embedTokens, compile, fetch }, cb) {
const token = embedTokens[step]

if (!token) {
return cb({})
}

get(token.embed.url).then(text => {
const next = text => {
let embedToken

if (token.embed.type === 'markdown') {
embedToken = compile.lexer(text)
} else if (token.embed.type === 'code') {
embedToken = compile.lexer(
'```' +
token.embed.lang +
'\n' +
text.replace(/`/g, '@DOCSIFY_QM@') +
'\n```\n'
)
if (text) {
if (token.embed.type === 'markdown') {
embedToken = compile.lexer(text)
} else if (token.embed.type === 'code') {
embedToken = compile.lexer(
'```' +
token.embed.lang +
'\n' +
text.replace(/`/g, '@DOCSIFY_QM@') +
'\n```\n'
)
}
}
cb({ token, embedToken })
walkFetchEmbed({ step: ++step, compile, embedTokens }, cb)
})
walkFetchEmbed({ step: ++step, compile, embedTokens, fetch }, cb)
}

if (process.env.SSR) {
console.log(token.embed.url)
fetch(token.embed.url).then(next)
} else {
get(token.embed.url).then(next)
}
}

export function prerenderEmbed ({ compiler, raw }, done) {
export function prerenderEmbed ({ compiler, raw, fetch }, done) {
let hit
if ((hit = cached[raw])) {
return done(hit)
Expand Down Expand Up @@ -65,7 +73,7 @@ export function prerenderEmbed ({ compiler, raw }, done) {
})

let moveIndex = 0
walkFetchEmbed({ compile, embedTokens }, ({ embedToken, token }) => {
walkFetchEmbed({ compile, embedTokens, fetch }, ({ embedToken, token }) => {
if (token) {
const index = token.index + moveIndex

Expand Down

0 comments on commit dc0c3ce

Please sign in to comment.