From f9d75ab93198451510604e2732e18767a5448b38 Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Tue, 20 Dec 2016 21:18:52 +0800 Subject: [PATCH] Feat/1.2 (#33) * Add homepage option, fixed #30 * Add custom basePath support, resolve #31 --- CHANGELOG.md | 5 +++++ docs/README.md | 21 +++++++++++++++++++++ docs/zh-cn.md | 21 +++++++++++++++++++++ package.json | 2 +- src/index.js | 20 ++++++++++++++------ 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d7e4a7..2c821ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.2.0 +### Features +- custom basePath +- custom homepage + ## 1.1.7 ### Bug fixes - Optimize progress bar diff --git a/docs/README.md b/docs/README.md index 0f07da5..83f8617 100644 --- a/docs/README.md +++ b/docs/README.md @@ -240,3 +240,24 @@ Scroll to the top on changing hash. ``` +#### homepage + +`README.md` will be render as homepage for your website in docs folder, but sometimes we want to specify another file as a homepage, or even use the `README.md` in your repo. we can use: + +```html + + + +``` + + +#### basePath + +If your HTML entry file and the markdown files are in different directories, we can use: + +```html + + + + +``` diff --git a/docs/zh-cn.md b/docs/zh-cn.md index 7926ed5..e6cd696 100644 --- a/docs/zh-cn.md +++ b/docs/zh-cn.md @@ -237,3 +237,24 @@ Sidebar 开关按钮 ``` +#### homepage + +默认情况下网站会将根目录下 `README.md` 作为首页渲染,但是有些时候我们想指定其他文件,甚至想直接将 repo 下的 README 作为首页。你可以这样做: + + +```html + + + +``` + +#### basePath + +指定加载文档的路径,如果你的 HTML 入口文件和文档是放在不同地方,你可以设置: + +```html + + + + +``` diff --git a/package.json b/package.json index a0d2ede..3bd1c2a 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ ], "scripts": { "build": "rm -rf lib themes && node build/build.js && mkdir lib/themes && mkdir themes && node build/build-css.js", - "dev": "node app.js & nodemon -w src --exec 'npm run build'", + "dev": "node app.js & nodemon -w src -e js,css --exec 'npm run build'", "test": "eslint src test" }, "repository": { diff --git a/src/index.js b/src/index.js index 9d35660..6e3f815 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,8 @@ const OPTIONS = { loadSidebar: null, loadNavbar: null, router: false, + homepage: 'README.md', + basePath: '', auto2top: false } const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop() @@ -33,8 +35,9 @@ let cacheRoute = null let cacheXhr = null const mainRender = function (cb) { - const route = getRoute() + const route = OPTIONS.basePath + getRoute() if (cacheRoute === route) return cb() + let wait let basePath = cacheRoute = route @@ -44,13 +47,18 @@ const mainRender = function (cb) { basePath = basePath.match(/(\S*\/)[^\/]+$/)[1] } + let page + if (!route) { + page = OPTIONS.homepage || 'README.md' + } else if (/\/$/.test(route)) { + page = `${route}README.md` + } else { + page = `${route}.md` + } + cacheXhr && cacheXhr.abort && cacheXhr.abort() // Render markdown file - cacheXhr = load( - (!route || /\/$/.test(route)) ? `${route}README.md` : `${route}.md`, - 'GET', - render.renderLoading) - + cacheXhr = load(page, 'GET', render.renderLoading) cacheXhr.then(result => { render.renderArticle(result) if (OPTIONS.loadSidebar) {