first commit
This commit is contained in:
commit
44ac51b9c2
20 changed files with 331 additions and 0 deletions
10
src/ajax.js
Normal file
10
src/ajax.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
export default function (url, options = {}) {
|
||||
const xhr = new XMLHttpRequest()
|
||||
|
||||
xhr.open(options.method || 'get', url)
|
||||
xhr.send()
|
||||
|
||||
return {
|
||||
then: cb => xhr.addEventListener('load', cb)
|
||||
}
|
||||
}
|
||||
35
src/index.js
Normal file
35
src/index.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import ajax from './ajax'
|
||||
import render from './render'
|
||||
|
||||
class Docsify {
|
||||
constructor (opts = {}) {
|
||||
Docsify.installed = true
|
||||
|
||||
this.dom = document.querySelector(opts.el || 'body')
|
||||
this.loc = document.location.pathname
|
||||
this.loc = this.loc === '/' ? 'README' : this.loc
|
||||
this.load()
|
||||
}
|
||||
|
||||
load () {
|
||||
ajax(`${this.loc}.md`).then(res => {
|
||||
const target = res.target
|
||||
if (target.status >= 400) {
|
||||
this.render('not found')
|
||||
} else {
|
||||
this.render(res.target.response)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
render (content) {
|
||||
this.dom.innerHTML = render(content)
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
if (Docsify.installed) return
|
||||
new Docsify()
|
||||
})
|
||||
|
||||
export default Docsify
|
||||
26
src/render.js
Normal file
26
src/render.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import marked from 'marked'
|
||||
import Prism from 'prismjs'
|
||||
|
||||
const renderer = new marked.Renderer()
|
||||
|
||||
/**
|
||||
* render anchor tag
|
||||
* @link https://github.com/chjj/marked#overriding-renderer-methods
|
||||
*/
|
||||
renderer.heading = function (text, level) {
|
||||
const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-')
|
||||
|
||||
return '<h' + level + '><a name="' + escapedText + '" class="anchor" href="#' +
|
||||
escapedText + '"><span class="header-link"></span></a>' + text +
|
||||
'</h' + level + '>'
|
||||
}
|
||||
|
||||
marked.setOptions({
|
||||
highlight (code, lang) {
|
||||
return Prism.highlight(code, Prism.languages[lang])
|
||||
}
|
||||
})
|
||||
|
||||
export default function (content) {
|
||||
return marked(content, { renderer })
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue