前言
base-admin從開源至今,已經收穫了2k Stat,而我們一直都沒有一份像樣的線上文件,最近寫了一個部落格園隨筆備份Java指令碼,將部落格隨筆備份到本地,格式是md文件格式,就有意去找將md文件轉成html的工具,最後選用了下面這個文件網站生成器,經過一段時間的學習、編寫文件,配合Gitee Pages服務,重要有了一個像樣的專案網站
https://huanzi-qch.gitee.io/base-admin
文件網站生成器
- 無需構建,寫完文件直接釋出
- 容易使用並且輕量 (壓縮後 ~21kB)
- 智慧的全文搜尋
- 提供多套主題
- 豐富的 API
- 支援 Emoji
- 相容 IE11
- 支援服務端渲染 SSR
npm i docsify-cli -g
也可以手動建立檔案,一個index.html入口頁面,一個README.md文件,還有一個.nojekyll的檔案
把這些檔案丟到tomcat或者nginx中,把容器執行起來就可以訪問!
Gitee Pages服務
有人說你為什麼不買個伺服器、域名?搞開源嘛就是用愛發電,能白嫖就白嫖,不寒磣!
同時,為了節省倉庫空間,文件中部分圖片是直接引用部落格園部落格裡面的圖片地址,因此要做反防盜鏈處理
//img圖片反防盜鏈,給所有img標籤都加上referrerPolicy屬性 for(let img of document.getElementsByTagName('img')){ img.setAttribute('referrerPolicy','no-referrer'); }
在gitee上建立一個與使用者名稱同名的私有倉庫,建立一個資料夾存放docsify相關檔案
部署gitee pages服務
這樣我們的專案網站就部署成功了!
封裝
為了方便其他地方也使用同一份配置,我封裝了自己的mydocsify.js、mydocsify.css指令碼頁面訪問統計
封裝css
/* 側邊欄 */ .sidebar{ padding: 25px 0 0 !important; } /* 自定義右側目錄 */ .catalogue{ display: block; overflow: hidden; position: fixed; right: 15px; top: 100px; padding: 15px 30px; background: white; color: rgb(102, 102, 102); border: 1px solid rgb(221, 221, 221); border-radius: 4px; line-height: 42px; font-size: 16px; text-align: center; box-shadow: rgb(238 238 238) 0px 0px 6px; cursor: pointer; } .catalogue-h2{ display: block; text-decoration: none; color: #505d6b; margin-left: 0px; font-size: 14px; line-height: 25px; text-align: left; } .catalogue-h3{ display: block; text-decoration: none; color: #505d6b; margin-left: 20px; font-size: 14px; line-height: 25px; text-align: left; } /* 首頁按鈕 */ section.cover .cover-main > p:last-child a { border-radius: 0.5rem !important; }
封裝js
let body = document.body; let head = document.head; let logaPath = 'https://huanzi-qch.gitee.io/file-server/images/logo.png'; let nodeArray = [ //logo {tagName:'link',rel:'shortcut icon',type:'image/x-icon',href:logaPath}, //docsify指令碼 {tagName:'link',rel:'stylesheet',href:'https://cdn.jsdelivr.net/npm/docsify/themes/vue.css'}, {tagName:'script',src:'https://cdn.jsdelivr.net/npm/docsify/lib/docsify.js'}, //外掛 {tagName:'script',src:'https://cdn.jsdelivr.net/npm/docsify/lib/plugins/zoom-image.min.js'}, {tagName:'script',src:'https://busuanzi.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback'}, ]; //通用配置 window.$docsify = { name: 'huanzi-qch', loadNavbar: true,//導航欄 loadSidebar: true,//目錄欄 coverpage: true,//封面 onlyCover: true, auto2top: true, busuanzi_value:{ site_uv: 0, page_pv: 0, site_pv: 0 }, plugins: [ //底部版權宣告 function(hook, vm) { hook.beforeEach(function(html) { let newHtml = html + "\n" + "<hr>" + //"<a target='_blank' href=''></a><br/>" + "<p>AD廣告位(長期招租,如有需要請私信)</p>" + "<a target='_blank' href='https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ckkryd9h&utm_source=ckkryd9h'>【阿里雲】新老使用者同享,上雲優化聚集地!</a><br/>" + "<a target='_blank' href='https://www.aliyun.com/activity?userCode=ckkryd9h'>【阿里雲】最新活動頁,上新必買搶先知,勁爆優惠不錯過!</a><br/>" + "<a target='_blank' href='https://www.aliyun.com/daily-act/ecs/activity_selection?userCode=ckkryd9h'>【阿里雲】輕量應用伺服器2核2G 低至60元/年起!香港與海外伺服器最低24元/月起!</a><br/>" + "<a target='_blank' href='https://www.aliyun.com/daily-act/ecs/care?userCode=ckkryd9h'>【阿里雲】ECS例項升級、續費,享低至 6.3折 限時折扣!</a><br/>" + "<a target='_blank' href='https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=e1c9db729edccd479fc902634492bf53&from=console'>【騰訊雲】雲產品限時秒殺,爆款1核2G雲伺服器,首年74元!</a><br/>" + "<a target='_blank' href='https://cloud.tencent.com/act/cps/redirect?redirect=1068&cps_key=e1c9db729edccd479fc902634492bf53&from=console'>【騰訊雲】境外1核2G伺服器低至2折,半價續費券限量免費領取!</a><br/>" + "<a target='_blank' href='https://cloud.tencent.com/act/cps/redirect?redirect=1063&cps_key=e1c9db729edccd479fc902634492bf53&from=console'>【騰訊雲】星星海SA2雲伺服器,1核2G首年99元起,高價效比首選!</a><br/>" + "<a target='_blank' href='https://cloud.tencent.com/act/cps/redirect?redirect=1060&cps_key=e1c9db729edccd479fc902634492bf53&from=console'>【騰訊雲】中小企業福利專場,多款剛需產品,滿足企業通用場景需求,雲伺服器2.5折起!</a><br/>" + "<br/><br/>" + "<span style='float: left;'>Copyright © 2021 - 至今 huanzi-qch</span>" + "<span id='busuanzi_container_site_pv' style='float: right;'>本站總訪問量 <span id='busuanzi_value_site_pv'>"+window.$docsify.busuanzi_value.site_pv+"</span> 次</span>"; return newHtml; }); }, //返回頂部 function(hook, vm) { var CONFIG = { auto: true, text: 'Top', right: 15, bottom: 140, offset: 500 } var opts = vm.config.scrollToTop || CONFIG; CONFIG.auto = opts.auto && typeof opts.auto === "boolean" ? opts.auto : CONFIG.auto; CONFIG.text = opts.text && typeof opts.text === "string" ? opts.text : CONFIG.text; CONFIG.right = opts.right && typeof opts.right === "number" ? opts.right : CONFIG.right; CONFIG.bottom = opts.bottom && typeof opts.bottom === "number" ? opts.bottom : CONFIG.bottom; CONFIG.offset = opts.offset && typeof opts.offset === "number" ? opts.offset : CONFIG.offset; var onScroll = function(e) { if (!CONFIG.auto) { return } var offset = window.document.documentElement.scrollTop; var $scrollBtn = Docsify.dom.find("span.scroll-to-top"); $scrollBtn.style.display = offset >= CONFIG.offset ? "block" : "none" }; hook.mounted(function() { var scrollBtn = document.createElement("span"); scrollBtn.className = "scroll-to-top"; scrollBtn.style.display = CONFIG.auto ? "none" : "block"; scrollBtn.style.overflow = "hidden"; scrollBtn.style.position = "fixed"; scrollBtn.style.right = CONFIG.right + "px"; scrollBtn.style.bottom = CONFIG.bottom + "px"; scrollBtn.style.width = "50px"; scrollBtn.style.height = "50px"; scrollBtn.style.background = "white"; scrollBtn.style.color = "#666"; scrollBtn.style.border = "1px solid #ddd"; scrollBtn.style.borderRadius = "4px"; scrollBtn.style.lineHeight = "42px"; scrollBtn.style.fontSize = "16px"; scrollBtn.style.textAlign = "center"; scrollBtn.style.boxShadow = "0px 0px 6px #eee"; scrollBtn.style.cursor = "pointer"; var textNode = document.createTextNode(CONFIG.text); scrollBtn.appendChild(textNode); document.body.appendChild(scrollBtn); window.addEventListener("scroll", onScroll); scrollBtn.onclick = function(e) { e.stopPropagation(); var step = window.scrollY / 15; var scroll = function() { window.scrollTo(0, window.scrollY - step); if (window.scrollY > 0) { setTimeout(scroll, 15) } }; scroll() } }) } ] }; //更新自定義右側目錄,以及反防盜鏈 function update(url){ //自動目錄 let catalogue = document.getElementsByClassName('catalogue'); if(catalogue.length <= 0){ catalogue = document.createElement('div'); catalogue.className = 'catalogue'; body.append(catalogue); }else{ catalogue = catalogue[0]; catalogue.innerHTML = ""; } catalogue.style.display='none'; setTimeout(function () { //img圖片反防盜鏈 for(let img of document.getElementsByTagName('img')){ img.setAttribute('referrerPolicy','no-referrer'); } //自動目錄 for(let child of document.getElementById('main').children){ if(child.nodeName === 'H2' || child.nodeName === 'H3'){ let a = document.createElement('a'); a.href = url + "?id=" + child.id; a.innerText = child.innerText; if(child.nodeName === 'H2'){ a.className = 'catalogue-h2'; }else{ a.className = 'catalogue-h3'; } catalogue.append(a); } } catalogue.style.display='block'; },1000); } //不蒜子 頁面訪問統計回撥 function BusuanziCallback(data){ window.$docsify.busuanzi_value = data; document.getElementById('busuanzi_value_site_pv').innerText = window.$docsify.busuanzi_value.site_pv; } //DOM載入完成執行 (function (){ //動態引入指令碼 for(let node of nodeArray){ let element = document.createElement(node.tagName); for(let key in node){ if('tagName' != key){ element[key] = node[key]; } } if('link' == node.tagName){ head.append(element); }else{ body.append(element); } } //離線模式 if (typeof navigator.serviceWorker !== 'undefined') { navigator.serviceWorker.register('https://huanzi-qch.gitee.io/file-server/files/docsify-sw.js'); } //監聽URL發生變化,是否生成自定義右側目錄 let url = window.location.href; if(url.indexOf('docs') !== -1){ url = window.location.hash.split("?")[0]; update(url); } window.onhashchange = function () { let hash = window.location.hash.split("?")[0]; if(hash != '#/' && url.indexOf(hash) === -1){ url = hash; update(url); }else{ let catalogue = document.getElementsByClassName('catalogue'); if(window.location.href.indexOf("docs") === -1 && catalogue.length > 0){ catalogue[0].remove(); } } }; })();
用的是快速統計外掛不蒜子
封面的專案Star數,直接引用這兩個圖片連結即可(把專案地址 huanzi-qch/base-admin 換成自己的專案)
https://img.shields.io/github/stars/huanzi-qch/base-admin.svg?style=social https://gitee.com/huanzi-qch/base-admin/badge/star.svg
巧了
巧了嘛這不是,把從部落格園備份下來的《SpringBoot系列》、《SpringCloud系列》也直接丟到Gitee Pages服務,直接用我們封裝好的docsify指令碼,不就正好可以做成線上文件了!
《SpringBoot系列》,https://huanzi-qch.gitee.io/spring-boot
《SpringCloud系列》,https://huanzi-qch.gitee.io/spring-cloud
後記
base-admin的文件,已經提交到專案中,歡迎大家一起參與編輯
後續有空再分享更多騷操作!