fix(render): support html file

This commit is contained in:
qingwei.li 2017-02-19 00:10:38 +08:00 • committed by cinwell.li
commit 7b6a2ac404
7 changed files with 35 additions and 18 deletions

View file

@ -5,7 +5,7 @@ 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'
import { getBasePath, getPath, isResolvePath } from '../route/util'
function executeScript () {
const script = dom.findAll('.markdown-section>script')
@ -22,6 +22,7 @@ function executeScript () {
function renderMain (html) {
if (!html) {
// TODO: Custom 404 page
html = 'not found'
}
this._renderTo('.markdown-section', html)
@ -56,11 +57,13 @@ export function renderMixin (proto) {
const active = getAndActive('.sidebar-nav', true, true)
subSidebar(active, subMaxLevel)
// bind event
this.activeLink = active
scrollActiveSidebar()
if (autoHeader && active) {
const main = dom.getNode('#main')
if (main.children[0].tagName !== 'H1') {
const firstNode = main.children[0]
if (firstNode && firstNode.tagName !== 'H1') {
const h1 = dom.create('h1')
h1.innerText = active.innerText
dom.before(main, h1)
@ -75,7 +78,7 @@ export function renderMixin (proto) {
proto._renderMain = function (text) {
callHook(this, 'beforeEach', text, result => {
const html = markdown(result)
const html = this.isHTML ? result : markdown(result)
callHook(this, 'afterEach', html, text => renderMain.call(this, text))
})
}
@ -88,15 +91,20 @@ export function renderMixin (proto) {
}
dom.toggleClass(el, 'add', 'show')
let html = cover(text)
let html = this.coverIsHTML ? text : cover(text)
const m = html.trim().match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$')
if (m) {
if (m[2] === 'color') {
el.style.background = m[1] + (m[3] || '')
} else {
let path = m[1]
dom.toggleClass(el, 'add', 'has-mask')
el.style.backgroundImage = `url(${getPath(getBasePath(this.config.basePath), m[1])})`
if (isResolvePath(m[1])) {
path = getPath(getBasePath(this.config.basePath), m[1])
}
el.style.backgroundImage = `url(${path})`
}
html = html.replace(m[0], '')
}
@ -145,7 +153,7 @@ export function initRender (vm) {
dom.before(dom.body, navEl)
if (config.themeColor) {
dom.$.head += tpl.theme(config.themeColor)
dom.$.head.innerHTML += tpl.theme(config.themeColor)
// Polyfll
cssVars(config.themeColor)
}