Skip to content

Commit 7b6a2ac

Browse files
committed
fix(render): support html file
1 parent 3e7d6ab commit 7b6a2ac

File tree

7 files changed

+35
-18
lines changed

7 files changed

+35
-18
lines changed

src/core/event/scroll.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export function scrollActiveSidebar () {
99
const anchors = dom.findAll('.anchor')
1010
const sidebar = dom.find('.sidebar')
1111
const wrap = dom.find(sidebar, '.sidebar-nav')
12-
const height = sidebar.clientHeight
1312

1413
const nav = {}
1514
const lis = dom.findAll(sidebar, 'li')
@@ -54,6 +53,7 @@ export function scrollActiveSidebar () {
5453
// scroll into view
5554
// https://github.com/vuejs/vuejs.org/blob/master/themes/vue/source/js/common.js#L282-L297
5655
if (!hoverOver && dom.body.classList.contains('sticky')) {
56+
const height = sidebar.clientHeight
5757
const curOffset = 0
5858
const cur = active.offsetTop + active.clientHeight + 40
5959
const isInView = (

src/core/fetch/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export function fetchMixin (proto) {
1515

1616
last = get(this.$getFile(path), true)
1717

18+
// Current page is html
19+
this.isHTML = /\.html$/g.test(path)
20+
1821
// Load main content
1922
last.then(text => {
2023
this._renderMain(text)
@@ -42,13 +45,15 @@ export function fetchMixin (proto) {
4245
proto._fetchCover = function () {
4346
const { coverpage } = this.config
4447
const root = getRoot(this.route.path)
48+
const path = this.$getFile(root + coverpage)
4549

4650
if (this.route.path !== '/' || !coverpage) {
4751
this._renderCover()
4852
return
4953
}
5054

51-
get(this.$getFile(root + coverpage))
55+
this.coverIsHTML = /\.html$/g.test(path)
56+
get(path)
5257
.then(text => this._renderCover(text))
5358
}
5459

src/core/render/compiler.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { genTree } from './gen-tree'
55
import { slugify, clearSlugCache } from './slugify'
66
import { emojify } from './emojify'
77
import { toURL, parse } from '../route/hash'
8-
import { getBasePath, getPath } from '../route/util'
8+
import { getBasePath, isResolvePath, getPath } from '../route/util'
99
import { isFn, merge, cached } from '../util/core'
1010

1111
let markdownCompiler = marked
@@ -84,9 +84,13 @@ renderer.paragraph = function (text) {
8484
return `<p>${text}</p>`
8585
}
8686
renderer.image = function (href, title, text) {
87-
const url = getPath(contentBase, href)
87+
let url = href
8888
const titleHTML = title ? ` title="${title}"` : ''
8989

90+
if (!isResolvePath(href)) {
91+
url = getPath(contentBase, href)
92+
}
93+
9094
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>`
9195
}
9296

src/core/render/index.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cssVars from '../util/polyfill/css-vars'
55
import * as tpl from './tpl'
66
import { markdown, sidebar, subSidebar, cover } from './compiler'
77
import { callHook } from '../init/lifecycle'
8-
import { getBasePath, getPath } from '../route/util'
8+
import { getBasePath, getPath, isResolvePath } from '../route/util'
99

1010
function executeScript () {
1111
const script = dom.findAll('.markdown-section>script')
@@ -22,6 +22,7 @@ function executeScript () {
2222
function renderMain (html) {
2323
if (!html) {
2424
// TODO: Custom 404 page
25+
html = 'not found'
2526
}
2627

2728
this._renderTo('.markdown-section', html)
@@ -56,11 +57,13 @@ export function renderMixin (proto) {
5657
const active = getAndActive('.sidebar-nav', true, true)
5758
subSidebar(active, subMaxLevel)
5859
// bind event
60+
this.activeLink = active
5961
scrollActiveSidebar()
6062

6163
if (autoHeader && active) {
6264
const main = dom.getNode('#main')
63-
if (main.children[0].tagName !== 'H1') {
65+
const firstNode = main.children[0]
66+
if (firstNode && firstNode.tagName !== 'H1') {
6467
const h1 = dom.create('h1')
6568
h1.innerText = active.innerText
6669
dom.before(main, h1)
@@ -75,7 +78,7 @@ export function renderMixin (proto) {
7578

7679
proto._renderMain = function (text) {
7780
callHook(this, 'beforeEach', text, result => {
78-
const html = markdown(result)
81+
const html = this.isHTML ? result : markdown(result)
7982
callHook(this, 'afterEach', html, text => renderMain.call(this, text))
8083
})
8184
}
@@ -88,15 +91,20 @@ export function renderMixin (proto) {
8891
}
8992
dom.toggleClass(el, 'add', 'show')
9093

91-
let html = cover(text)
94+
let html = this.coverIsHTML ? text : cover(text)
9295
const m = html.trim().match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$')
9396

9497
if (m) {
9598
if (m[2] === 'color') {
9699
el.style.background = m[1] + (m[3] || '')
97100
} else {
101+
let path = m[1]
102+
98103
dom.toggleClass(el, 'add', 'has-mask')
99-
el.style.backgroundImage = `url(${getPath(getBasePath(this.config.basePath), m[1])})`
104+
if (isResolvePath(m[1])) {
105+
path = getPath(getBasePath(this.config.basePath), m[1])
106+
}
107+
el.style.backgroundImage = `url(${path})`
100108
}
101109
html = html.replace(m[0], '')
102110
}
@@ -145,7 +153,7 @@ export function initRender (vm) {
145153
dom.before(dom.body, navEl)
146154

147155
if (config.themeColor) {
148-
dom.$.head += tpl.theme(config.themeColor)
156+
dom.$.head.innerHTML += tpl.theme(config.themeColor)
149157
// Polyfll
150158
cssVars(config.themeColor)
151159
}

src/core/render/tpl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function corner (data) {
1010
data = data.replace(/^git\+/, '')
1111

1212
return (
13-
'<a href="${data}" class="github-corner" aria-label="View source on Github">' +
13+
`<a href="${data}" class="github-corner" aria-label="View source on Github">` +
1414
'<svg viewBox="0 0 250 250" aria-hidden="true">' +
1515
'<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>' +
1616
'<path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>' +

src/core/route/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function routeMixin (proto) {
2323

2424
path = getAlias(path, config.alias)
2525
path = getFileName(path)
26-
path = path === '/README.md' ? ('/' + config.homepage || path) : path
26+
path = path === '/README.md' ? (config.homepage || path) : path
2727
path = getPath(base, path)
2828

2929
return path

src/core/route/util.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ export const getBasePath = cached(base => {
3838
})
3939

4040
export function getPath (...args) {
41-
const path = args.find(path => /:|(\/{2})/.test(path))
42-
43-
if (path) return path
44-
4541
return cleanPath(args.join('/'))
4642
}
4743

44+
export const isResolvePath = cached(path => {
45+
return /:|(\/{2})/.test(path)
46+
})
47+
4848
export const getRoot = cached(path => {
4949
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
5050
})
5151

52-
export function cleanPath (path) {
52+
export const cleanPath = cached(path => {
5353
return path.replace(/\/+/g, '/')
54-
}
54+
})

0 commit comments

Comments
 (0)