11import { Compiler } from '../../src/core/render/compiler'
22import { AbstractHistory } from '../../src/core/router/history/abstract'
3- import path from 'path'
4- import fs from 'fs'
3+ import { resolve , basename } from 'path'
4+ import { readFileSync } from 'fs'
5+ import * as tpl from '../../src/core/render/tpl'
6+
7+ function cwd ( ...args ) {
8+ return resolve ( process . cwd ( ) , ...args )
9+ }
10+
11+ function mainTpl ( config ) {
12+ let html = `<nav class="app-nav${ config . repo ? '' : 'no-badge' } "><!--navbar--></nav>`
13+
14+ if ( config . repo ) {
15+ html += tpl . corner ( config . repo )
16+ }
17+ if ( config . coverpage ) {
18+ html += tpl . cover ( )
19+ }
20+
21+ html += tpl . main ( config )
22+
23+ return html
24+ }
525
626export default class Renderer {
727 constructor ( {
@@ -10,16 +30,80 @@ export default class Renderer {
1030 config,
1131 cache
1232 } ) {
13- this . template = template
14- this . path = path
15- this . config = config
33+ this . html = this . template = template
34+ this . path = cwd ( path )
35+ this . config = Object . assign ( config , {
36+ routerMode : 'history'
37+ } )
1638 this . cache = cache
1739
18- this . router = new AbstractHistory ( )
40+ this . router = new AbstractHistory ( config )
1941 this . compiler = new Compiler ( config , this . router )
42+
43+ this . router . getCurrentPath = ( ) => this . url
44+ this . _renderHtml ( 'inject-config' , `<script>window.$docsify = ${ JSON . stringify ( config ) } </script>` )
45+ this . _renderHtml ( 'inject-app' , mainTpl ( config ) )
46+ }
47+
48+ renderToString ( url ) {
49+ this . url = url
50+ // TODO render cover page
51+ const { loadSidebar, loadNavbar } = this . config
52+
53+ const mainFile = cwd ( this . path , `./${ this . router . getFile ( url ) } ` )
54+ this . _renderHtml ( 'main' , this . _render ( mainFile ) )
55+
56+ if ( loadSidebar ) {
57+ const name = loadSidebar === true ? '_sidebar.md' : loadSidebar
58+ const sidebarFile = cwd ( mainFile , '..' , name )
59+ this . _renderHtml ( 'sidebar' , this . _render ( sidebarFile , 'sidebar' ) )
60+ }
61+
62+ if ( loadNavbar ) {
63+ const name = loadNavbar === true ? '_navbar.md' : loadNavbar
64+ const navbarFile = cwd ( mainFile , '..' , name )
65+ this . _renderHtml ( 'navbar' , this . _render ( navbarFile , 'navbar' ) )
66+ }
67+
68+ return this . html
69+ }
70+
71+ _renderHtml ( match , content ) {
72+ this . html = this . html . replace ( new RegExp ( `<!--${ match } -->` , 'g' ) , content )
2073 }
2174
22- renderToString ( url ) {
23- console . log ( url )
75+ _render ( path , type ) {
76+ let html = this . _loadFile ( path )
77+
78+ switch ( type ) {
79+ case 'sidebar' :
80+ html = this . compiler . sidebar ( html )
81+ break
82+ case 'cover' :
83+ html = this . compiler . cover ( html )
84+ break
85+ case 'navbar' :
86+ case 'article' :
87+ default :
88+ html = this . compiler . compile ( html )
89+ break
90+ }
91+
92+ return html
93+ }
94+
95+ _loadFile ( filePath ) {
96+ try {
97+ return readFileSync ( filePath , 'utf8' )
98+ } catch ( e ) {
99+ const fileName = basename ( filePath )
100+ const parentPath = cwd ( filePath , '../..' )
101+
102+ if ( this . path . length < parentPath . length ) {
103+ throw Error ( `Not found file ${ fileName } ` )
104+ }
105+
106+ this . _loadFile ( cwd ( filePath , '../..' , fileName ) )
107+ }
24108 }
25109}
0 commit comments