Skip to content

Commit f0ba690

Browse files
committed
feat(compiler): support embedded markdown, html, video, etc files, close #383, close #333
1 parent 450d9a2 commit f0ba690

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

Diff for: src/core/fetch/ajax.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import progressbar from '../render/progressbar'
22
import { noop } from '../util/core'
33

44
const cache = {}
5+
let uid = 0
56

67
/**
78
* Simple ajax get
@@ -24,6 +25,7 @@ export function get (url, hasBar = false) {
2425
xhr.send()
2526

2627
return {
28+
uid: uid++,
2729
then: function (success, error = noop) {
2830
if (hasBar) {
2931
const id = setInterval(

Diff for: src/core/render/compiler.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { slugify } from './slugify'
66
import { emojify } from './emojify'
77
import { isAbsolutePath, getPath } from '../router/util'
88
import { isFn, merge, cached } from '../util/core'
9+
import { get } from '../fetch/ajax'
910

1011
const cachedLinks = {}
1112
function getAndRemoveConfig (str = '') {
@@ -22,6 +23,27 @@ function getAndRemoveConfig (str = '') {
2223

2324
return { str, config }
2425
}
26+
const compileMedia = {
27+
markdown (url, config) {
28+
const request = get(url, false)
29+
const id = `docsify-get-${request.uid}`
30+
31+
request.then(text => {
32+
document.getElementById(id).innerHTML = this.compile(text)
33+
})
34+
35+
return `<div data-origin="${url}" id=${id}></div>`
36+
},
37+
html (url, config) {
38+
return `<iframe src="${url}" ${config || 'width=100% height=400'}></iframe>`
39+
},
40+
video (url, config) {
41+
return `<video src="${url}" ${config || 'controls'}>Not Support</video>`
42+
},
43+
audio (url, config) {
44+
return `<audio src="${url}" ${config || 'controls'}>Not Support</audio>`
45+
}
46+
}
2547

2648
export class Compiler {
2749
constructor (config, router) {
@@ -172,14 +194,36 @@ export class Compiler {
172194
url = getPath(contentBase, href)
173195
}
174196

197+
let media
198+
if (config.type && (media = compileMedia[config.type])) {
199+
return media.call(_self, url, title)
200+
}
201+
202+
let type = null
203+
if (/\.(md|markdown)/.test(url)) {
204+
type = 'markdown'
205+
} else if (/\.html?/.test(url)) {
206+
type = 'html'
207+
} else if (/\.(mp4|ogg)/.test(url)) {
208+
type = 'video'
209+
} else if (/\.mp3/.test(url)) {
210+
type = 'audio'
211+
}
212+
if (type) {
213+
return compileMedia[type].call(_self, url, title)
214+
}
215+
175216
return `<img src="${url}"data-origin="${href}" alt="${text}"${attrs}>`
176217
}
177218

178219
const CHECKED_RE = /^\[([ x])\] +/
179220
origin.listitem = renderer.listitem = function (text) {
180221
const checked = CHECKED_RE.exec(text)
181222
if (checked) {
182-
text = text.replace(CHECKED_RE, `<input type="checkbox" ${checked[1] === 'x' ? 'checked' : ''} />`)
223+
text = text.replace(
224+
CHECKED_RE,
225+
`<input type="checkbox" ${checked[1] === 'x' ? 'checked' : ''} />`
226+
)
183227
}
184228
return `<li${checked ? ` class="task-list-item"` : ''}>${text}</li>\n`
185229
}

Diff for: src/core/render/index.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import { isPrimitive } from '../util/core'
1111
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
1212

1313
function executeScript () {
14-
const script = dom.findAll('.markdown-section>script')
15-
.filter(s => !/template/.test(s.type))[0]
14+
const script = dom
15+
.findAll('.markdown-section>script')
16+
.filter(s => !/template/.test(s.type))[0]
1617
if (!script) return false
1718
const code = script.innerText.trim()
1819
if (!code) return false
@@ -23,11 +24,10 @@ function executeScript () {
2324
}
2425

2526
function formatUpdated (html, updated, fn) {
26-
updated = typeof fn === 'function'
27-
? fn(updated)
28-
: typeof fn === 'string'
29-
? tinydate(fn)(new Date(updated))
30-
: updated
27+
updated =
28+
typeof fn === 'function'
29+
? fn(updated)
30+
: typeof fn === 'string' ? tinydate(fn)(new Date(updated)) : updated
3131

3232
return html.replace(/{docsify-updated}/g, updated)
3333
}
@@ -43,9 +43,11 @@ function renderMain (html) {
4343
!this.config.loadSidebar && this._renderSidebar()
4444

4545
// execute script
46-
if (this.config.executeScript !== false &&
47-
typeof window.Vue !== 'undefined' &&
48-
!executeScript()) {
46+
if (
47+
this.config.executeScript !== false &&
48+
typeof window.Vue !== 'undefined' &&
49+
!executeScript()
50+
) {
4951
setTimeout(_ => {
5052
const vueVM = window.__EXECUTE_RESULT__
5153
vueVM && vueVM.$destroy && vueVM.$destroy()
@@ -84,7 +86,8 @@ export function renderMixin (proto) {
8486
this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel))
8587
const activeEl = getAndActive(this.router, '.sidebar-nav', true, true)
8688
if (loadSidebar && activeEl) {
87-
activeEl.parentNode.innerHTML += (this.compiler.subSidebar(subMaxLevel) || '')
89+
activeEl.parentNode.innerHTML +=
90+
this.compiler.subSidebar(subMaxLevel) || ''
8891
} else {
8992
// reset toc
9093
this.compiler.subSidebar()
@@ -140,7 +143,9 @@ export function renderMixin (proto) {
140143
dom.toggleClass(el, 'add', 'show')
141144

142145
let html = this.coverIsHTML ? text : this.compiler.cover(text)
143-
const m = html.trim().match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$')
146+
const m = html
147+
.trim()
148+
.match('<p><img.*?data-origin="(.*?)"[^a]+alt="(.*?)">([^<]*?)</p>$')
144149

145150
if (m) {
146151
if (m[2] === 'color') {

Diff for: src/themes/basic/_layout.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ kbd {
9595
list-style-type: none;
9696
}
9797

98-
li input[type=checkbox] {
98+
li input[type='checkbox'] {
9999
margin: 0 0.2em 0.25em -1.6em;
100100
vertical-align: middle;
101101
}
@@ -381,6 +381,10 @@ body.sticky {
381381
margin: 2em 0;
382382
}
383383

384+
.markdown-section iframe {
385+
border: 1px solid #eee;
386+
}
387+
384388
.markdown-section table {
385389
border-collapse: collapse;
386390
border-spacing: 0;

0 commit comments

Comments
 (0)