import * as dom from '../util/dom' import { getAndActive, sticky } from '../event/sidebar' import { scrollActiveSidebar, scroll2Top } from '../event/scroll' import cssVars from '../util/polyfill/css-vars' import * as tpl from './tpl' import { markdown, sidebar, subSidebar, cover } from './compiler' import { callHook } from '../init/lifecycle' import { getBasePath, getPath } from '../route/util' function executeScript () { const script = dom.findAll('.markdown-section>script') .filter(s => !/template/.test(s.type))[0] if (!script) return false const code = script.innerText.trim() if (!code) return false window.__EXECUTE_RESULT__ = new Function('return ' + code)() } function renderMain (html) { if (!html) { // TODO: Custom 404 page } this._renderTo('.markdown-section', html) // Render sidebar with the TOC !this.config.loadSidebar && this._renderSidebar() // execute script this.config.executeScript && executeScript() if (!this.config.executeScript && typeof window.Vue !== 'undefined' && !executeScript()) { window.__EXECUTE_RESULT__ = new window.Vue().$mount('#main') } if (this.config.auto2top) { setTimeout(() => scroll2Top(this.config.auto2top), 0) } } export function renderMixin (proto) { proto._renderTo = function (el, content, replace) { const node = dom.getNode(el) if (node) node[replace ? 'outerHTML' : 'innerHTML'] = content } proto._renderSidebar = function (text) { const { maxLevel, subMaxLevel } = this.config this._renderTo('.sidebar-nav', sidebar(text, maxLevel)) subSidebar(getAndActive('.sidebar-nav', true), subMaxLevel) // bind event scrollActiveSidebar() } proto._renderNav = function (text) { text && this._renderTo('nav', markdown(text)) getAndActive('nav') } proto._renderMain = function (text) { callHook(this, 'beforeEach', text, result => { const html = markdown(result) callHook(this, 'afterEach', html, text => renderMain.call(this, text)) }) } proto._renderCover = function (text) { const el = dom.getNode('.cover') if (!text) { dom.toggleClass(el, 'remove', 'show') return } dom.toggleClass(el, 'add', 'show') let html = cover(text) const m = html.trim().match('