Skip to content

Commit

Permalink
init config by window.
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Jan 4, 2017
1 parent 7be6baa commit 8972834
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { load, camel2kebab, isNil, getRoute } from './util'
import { load, camel2kebab, isNil, getRoute, merge } from './util'
import { scrollIntoView, activeLink } from './event'
import * as render from './render'

const OPTIONS = {
const OPTIONS = merge({
el: '#app',
repo: '',
maxLevel: 6,
Expand All @@ -16,7 +16,7 @@ const OPTIONS = {
coverpage: '',
basePath: '',
auto2top: false
}
}, window.$docsify)
const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop()

// load configuration for script attribute
Expand All @@ -32,7 +32,7 @@ if (script) {
}

// load options
render.config(OPTIONS)
render.init(OPTIONS)

let cacheRoute = null
let cacheXhr = null
Expand Down
67 changes: 33 additions & 34 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import marked from 'marked'
import Prism from 'prismjs'
import * as tpl from './tpl'
import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top, sticky } from './event'
import { genTree, getRoute, isMobile, slugify } from './util'
import { genTree, getRoute, isMobile, slugify, merge } from './util'

let OPTIONS = {}
let toc = []
const CACHE = {}

const renderTo = function (dom, content) {
Expand All @@ -14,40 +15,47 @@ const renderTo = function (dom, content) {

return dom
}
let toc = []
const renderer = new marked.Renderer()

/**
* render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods
* init render
* @param {Object} options
*/
renderer.heading = function (text, level) {
const slug = slugify(text)
let route = ''
export function init (options) {
OPTIONS = options

if (OPTIONS.router) {
route = `#/${getRoute()}`
}
const renderer = new marked.Renderer()
/**
* render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
renderer.heading = function (text, level) {
const slug = slugify(text)
let route = ''

if (OPTIONS.router) {
route = `#/${getRoute()}`
}

toc.push({ level, slug: `${route}#${encodeURIComponent(slug)}`, title: text })
toc.push({ level, slug: `${route}#${encodeURIComponent(slug)}`, title: text })

return `<h${level} id="${slug}"><a href="${route}#${slug}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
}
// highlight code
renderer.code = function (code, lang = '') {
const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup)
.replace(/{{/g, '<span>{{</span>')
return `<h${level} id="${slug}"><a href="${route}#${slug}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
}
// highlight code
renderer.code = function (code, lang = '') {
const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup)
.replace(/{{/g, '<span>{{</span>')

return `<pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
}
renderer.link = function (href, title, text) {
if (OPTIONS.router && !/:/.test(href)) {
href = `#/${href}`.replace(/\/\//g, '/')
return `<pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
}
renderer.link = function (href, title, text) {
if (OPTIONS.router && !/:/.test(href)) {
href = `#/${href}`.replace(/\/\//g, '/')
}

return `<a href="${href}" title="${title || ''}">${text}</a>`
return `<a href="${href}" title="${title || ''}">${text}</a>`
}
marked.setOptions(merge({ renderer }, OPTIONS.marked))
}
marked.setOptions({ renderer })

/**
* App
Expand Down Expand Up @@ -191,12 +199,3 @@ export function renderLoading ({ loaded, total, step }) {
}, 200)
}
}

/**
* Load Config
* @param {Object} options
*/
export function config (options) {
OPTIONS = options
}

15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,18 @@ export function slugify (string) {
slugify.clear = function () {
slugify.occurrences = {}
}

const hasOwnProperty = Object.prototype.hasOwnProperty
export const merge = Object.assign || function (to) {
for (let i = 1; i < arguments.length; i++) {
const from = Object(arguments[i])

for (const key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key]
}
}
}

return to
}

0 comments on commit 8972834

Please sign in to comment.