Skip to content

Commit a88ff9a

Browse files
committed
move render methods into renderMixin class
1 parent 146e486 commit a88ff9a

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

src/core/render/index.js

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,82 +11,81 @@ import { Compiler } from './compiler';
1111
import * as tpl from './tpl';
1212
import { prerenderEmbed } from './embed';
1313

14-
function executeScript() {
15-
const script = dom
16-
.findAll('.markdown-section>script')
17-
.filter(s => !/template/.test(s.type))[0];
18-
if (!script) {
19-
return false;
20-
}
21-
22-
const code = script.innerText.trim();
23-
if (!code) {
24-
return false;
25-
}
26-
27-
setTimeout(_ => {
28-
window.__EXECUTE_RESULT__ = new Function(code)();
29-
}, 0);
30-
}
14+
export function renderMixin(Base = class {}) {
15+
return class extends Base {
16+
_executeScript() {
17+
const script = dom
18+
.findAll('.markdown-section>script')
19+
.filter(s => !/template/.test(s.type))[0];
20+
if (!script) {
21+
return false;
22+
}
3123

32-
function formatUpdated(html, updated, fn) {
33-
updated =
34-
typeof fn === 'function'
35-
? fn(updated)
36-
: typeof fn === 'string'
37-
? tinydate(fn)(new Date(updated))
38-
: updated;
24+
const code = script.innerText.trim();
25+
if (!code) {
26+
return false;
27+
}
3928

40-
return html.replace(/{docsify-updated}/g, updated);
41-
}
29+
setTimeout(_ => {
30+
window.__EXECUTE_RESULT__ = new Function(code)();
31+
}, 0);
32+
}
4233

43-
function renderMain(html) {
44-
if (!html) {
45-
html = '<h1>404 - Not found</h1>';
46-
}
47-
48-
this._renderTo('.markdown-section', html);
49-
// Render sidebar with the TOC
50-
!this.config.loadSidebar && this._renderSidebar();
51-
52-
// Execute script
53-
if (
54-
this.config.executeScript !== false &&
55-
typeof window.Vue !== 'undefined' &&
56-
!executeScript()
57-
) {
58-
setTimeout(_ => {
59-
const vueVM = window.__EXECUTE_RESULT__;
60-
vueVM && vueVM.$destroy && vueVM.$destroy();
61-
window.__EXECUTE_RESULT__ = new window.Vue().$mount('#main');
62-
}, 0);
63-
} else {
64-
this.config.executeScript && executeScript();
65-
}
66-
}
34+
_formatUpdated(html, updated, fn) {
35+
updated =
36+
typeof fn === 'function'
37+
? fn(updated)
38+
: typeof fn === 'string'
39+
? tinydate(fn)(new Date(updated))
40+
: updated;
41+
42+
return html.replace(/{docsify-updated}/g, updated);
43+
}
44+
45+
__renderMain(html) {
46+
if (!html) {
47+
html = '<h1>404 - Not found</h1>';
48+
}
6749

68-
function renderNameLink(vm) {
69-
const el = dom.getNode('.app-name-link');
70-
const nameLink = vm.config.nameLink;
71-
const path = vm.route.path;
50+
this._renderTo('.markdown-section', html);
51+
// Render sidebar with the TOC
52+
!this.config.loadSidebar && this._renderSidebar();
53+
54+
// Execute script
55+
if (
56+
this.config.executeScript !== false &&
57+
typeof window.Vue !== 'undefined' &&
58+
!this._executeScript()
59+
) {
60+
setTimeout(_ => {
61+
const vueVM = window.__EXECUTE_RESULT__;
62+
vueVM && vueVM.$destroy && vueVM.$destroy();
63+
window.__EXECUTE_RESULT__ = new window.Vue().$mount('#main');
64+
}, 0);
65+
} else {
66+
this.config.executeScript && this._executeScript();
67+
}
68+
}
7269

73-
if (!el) {
74-
return;
75-
}
70+
_renderNameLink(vm) {
71+
const el = dom.getNode('.app-name-link');
72+
const nameLink = vm.config.nameLink;
73+
const path = vm.route.path;
7674

77-
if (isPrimitive(vm.config.nameLink)) {
78-
el.setAttribute('href', nameLink);
79-
} else if (typeof nameLink === 'object') {
80-
const match = Object.keys(nameLink).filter(
81-
key => path.indexOf(key) > -1
82-
)[0];
75+
if (!el) {
76+
return;
77+
}
8378

84-
el.setAttribute('href', nameLink[match]);
85-
}
86-
}
79+
if (isPrimitive(vm.config.nameLink)) {
80+
el.setAttribute('href', nameLink);
81+
} else if (typeof nameLink === 'object') {
82+
const match = Object.keys(nameLink).filter(
83+
key => path.indexOf(key) > -1
84+
)[0];
8785

88-
export function renderMixin(Base = class {}) {
89-
return class extends Base {
86+
el.setAttribute('href', nameLink[match]);
87+
}
88+
}
9089
_renderTo(el, content, replace) {
9190
const node = dom.getNode(el);
9291
if (node) {
@@ -147,21 +146,21 @@ export function renderMixin(Base = class {}) {
147146

148147
_renderMain(text, opt = {}, next) {
149148
if (!text) {
150-
return renderMain.call(this, text);
149+
return this.__renderMain(text);
151150
}
152151

153152
this.callHook('beforeEach', text, result => {
154153
let html;
155154
const callback = () => {
156155
if (opt.updatedAt) {
157-
html = formatUpdated(
156+
html = this._formatUpdated(
158157
html,
159158
opt.updatedAt,
160159
this.config.formatUpdated
161160
);
162161
}
163162

164-
this.callHook('afterEach', html, text => renderMain.call(this, text));
163+
this.callHook('afterEach', html, text => this.__renderMain(text));
165164
};
166165

167166
if (this.isHTML) {
@@ -230,7 +229,7 @@ export function renderMixin(Base = class {}) {
230229

231230
_render_updateRender() {
232231
// Render name link
233-
renderNameLink(this);
232+
this._renderNameLink(this);
234233
}
235234

236235
initRender() {

0 commit comments

Comments
 (0)