日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现自己的“单页”博客,只需要一个指令 (Moka)

發(fā)布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现自己的“单页”博客,只需要一个指令 (Moka) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如今,單頁應(yīng)用"橫行霸道", 而且新時代知識信息海量,我們更需要自己的Blog來沉淀知識。
綜上,Moka走入了我們的實現(xiàn)。

github.com/moyuyc/moka

Usage

為了第一眼能看到效果, 我先把如何安裝使用說一下。

  • 一切從npm開始

    $ npm i -g moka-cli
  • 安裝完成后

    $ moka -h # 幫助 $ moka -v # 版本$ mkdir myBlog $ cd myBlog $ moka i # 開啟自己的spa Blog $ moka g # generate static pages $ moka s # 開啟本地服務(wù),可以在本地看到效果了! $ moka n abc # 新建一個article$ moka d # 根據(jù) moka.config.json deploy 發(fā)布 $ moka b # 根據(jù) moka.config.json bak 備份所有文件
  • 線上效果
    moyuyc.github.io

  • Moka主題配置

    默認(rèn)主題是用react/webpack開發(fā)的,
    但...不幸的是, 本人誤操作把源碼都刪了..., 但萬幸的是...留下了build, 生產(chǎn)環(huán)境的代碼...

    {"avatar": "/head.jpg", // 頭像"title": " Moyu Dev Blog ", // 網(wǎng)站title"description": " Web, Node C/C++ Dev ","mainInfoColor": "", // 首頁信息的文字顏色, 默認(rèn)白色"canvasColor": "", // 首頁飄散的雪花顏色"leftPercentage": 50,// 左側(cè)百分比, 右側(cè)將會自動為100-leftPercentage, <=0 將會在非首頁頁面隱藏left"pageSize": 6, // 每頁文章數(shù)目, <=0 一頁展示所有"summaryNum": 50, // 摘要的文字截斷字?jǐn)?shù)"postTarget": "_blank", // 文章中l(wèi)ink的跳轉(zhuǎn)方式"iconTarget": "_blank", // 左側(cè)icon的link的跳轉(zhuǎn)方式"projectTarget": "_blank", // project中l(wèi)ink的跳轉(zhuǎn)方式"home": { // 首頁中右側(cè)文字內(nèi)容"title": "About Me","contentHtml": "<p>I’m a Javascript enthusiast. I organise Baidu BEFE Meetup and try my best to help out with the team. I’m also a member of the core dev team.</p><p><img class='emoji' src='http://emojipedia-us.s3.amazonaws.com/cache/08/84/088419f4d97c19762c29008c4a89bbf4.png'/></p>"},// projects"projects": [{"title": "Moyu Theme","state": "Doing", //可無"image": "https://raw.githubusercontent.com/TaylanTatli/Ramme/master/assets/img/screenshot-post.png""link": "" // 點擊跳轉(zhuǎn)地址},{"title": "Moyu Theme","state": "Doing","image": "https://raw.githubusercontent.com/TaylanTatli/Ramme/master/assets/img/screenshot-post.png"},{"title": "Moyu Theme","state": "Doing","image": "https://raw.githubusercontent.com/TaylanTatli/Ramme/master/assets/img/screenshot-post.png"},{"title": "Moyu Theme","state": "Doing","image": "https://raw.githubusercontent.com/TaylanTatli/Ramme/master/assets/img/screenshot-post.png"},{"title": "Moyu Theme","state": "Doing","image": "https://raw.githubusercontent.com/TaylanTatli/Ramme/master/assets/img/screenshot-post.png"}],"icon": [ // 左側(cè)icons key命名參看font-awesome.css{"github": "https://github.com/moyuyc"}],"coverImage": { // 左側(cè)封面"images": {"tags": "http://taylantatli.me/Halve/images/unsplash-gallery-image-3.jpg","home": "http://taylantatli.me/Halve/images/unsplash-image-10.jpg","article": "http://taylantatli.me/Halve/images/unsplash-gallery-image-3.jpg","serach": "http://taylantatli.me/Halve/images/unsplash-image-10.jpg","notExist": "http://taylantatli.me/Halve/images/unsplash-gallery-image-3.jpg","posts": [ // posts可為數(shù)組(對于每一頁), 可為字符串"http://taylantatli.me/Halve/images/unsplash-image-10.jpg","http://taylantatli.me/Halve/images/home.png","http://taylantatli.me/Halve/images/unsplash-gallery-image-3.jpg"]},"articleCover": true // 是否開啟文章封面, 在文章頭部配置 `cover: ...` 效果請看默認(rèn)文章`Linux C學(xué)習(xí)一周`} }

    Document

    Moka, 認(rèn)為前端UI與數(shù)據(jù)應(yīng)該完全分離開來, 而不是像hexo那樣傳統(tǒng)的blog。
    這樣做的好處不言而喻, 可能第一次加載數(shù)據(jù)較多, 但是后續(xù)操作更加暢快, 網(wǎng)站體驗更加優(yōu)化了。

    既然如此, 那么Moka產(chǎn)生的數(shù)據(jù)是什么樣子的呢?

    數(shù)據(jù)格式

    Moka 采用主流的json字符串

    $ moka generate 后產(chǎn)生的json如下

    {"main": {"filename": {"content": "...","head": {"date": "","title": "","tags": [tagnames...] or "tagname"}}},"index": {"sorted": [filenames...],"tagMap": {"tagname": [filenames...]}} }

    說明

    • "content"可以通過配置控制, 返回markdown或者h(yuǎn)tml(請看下文配置returnRaw說明)

    • "head"表示在文章中頭部---...---中解析出來的數(shù)據(jù), tags 可以是Array(多個)或String(單個)

    • "sorted"為按照時間倒序的filenames數(shù)組

    • "tagMap"為所有tag的映射, 即哪些文章包含"tagname"

    配置說明

    主要包含 default config, moka.config.json, theme.config.json, theme.config.js

    • default config 為Moka初始配置, 不推薦修改

      {theme: "moka", // 當(dāng)前主題apiRoot: "moka_api", // moka generate 數(shù)據(jù)和配置 所存放的文件夾skipRegExp: "/[^\.(md|markdown)]$/", // 在 _articles 中渲染忽略的文件名正則表達(dá)式timeFormat: "YYYY/MM/DD HH:mm:ss", // 默認(rèn)產(chǎn)生的時間格式 (參看moment.js)// marked 配置參看(marked.js: https://github.com/chjj/marked)marked: {options: {gfm: true,tables: true,breaks: false,pedantic: false,sanitize: false,smartLists: true,smartypants: false,highlight: function (code) { return require('highlight.js').highlightAuto(code).value;}},setup: function (renderer) {renderer.heading = function (text, level) { var 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 + '>';}}},returnRaw: false, // * 是否返回markdown字符串, 那么需要主題自己轉(zhuǎn)換markdowntitle: 'Blog',favicon: "favicon.ico", // 網(wǎng)站圖標(biāo)injectScript: true, // 是否注入`moka.inject.js`themeBuild: "build" // 將會取 themes/moka/build 中文件放到 static 中, 認(rèn)為build為生產(chǎn)環(huán)境代碼 }
    • moka.config.json 為全局站點配置, 在apiRoot中可以得到

      {"theme": "moka","title": "Moyu Blog","favicon": "favicon.ico","author": "moyu","description": "moyu Blog","siteName": "site",// moka generate 配置"deploy": {"type": "git","url": "https://github.com/moyuyc/moyuyc.github.io.git","branch": "master"} }
    • theme.config.json 為主題配置, 在apiRoot中可以得到, 完全為主題開發(fā)者自定義

      關(guān)于默認(rèn)主題配置說明, 請看[theme readme](THEME_README.md)
    • theme.config.js 為了主題開放者也能夠控制Moka產(chǎn)生數(shù)據(jù), 可以修改該文件, 從而覆蓋默認(rèn)配置

      module.exports = {apiRoot: "moka_api",skipRegExp: "/[^\.(md|markdown)]$/",//http://momentjs.com/timeFormat: 'YYYY-MM-DD HH:mm', // 返回的時間格式marked: {options: {gfm: true,tables: true,breaks: false,pedantic: false,sanitize: false,smartLists: true,smartypants: false},setup: function (renderer) {// 在這里控制renderer規(guī)則, 詳細(xì)請看 marked}},returnRaw: false,themeBuild: "build", }

    閑話

    開發(fā)者可以通過ajax/fetch/...異步獲取 apiRoot配置下的db.json/moka.config.json/theme.config.json

    然后盡情用react/vue/webpack/...開發(fā)自己喜歡的主題吧。

    還有默認(rèn)主題是用react/webpack開發(fā)的,
    但...不幸的是, 本人誤操作把源碼都刪了..., 但萬幸的是...留下了build, 生產(chǎn)環(huán)境的代碼...

    star it!

    總結(jié)

    以上是生活随笔為你收集整理的实现自己的“单页”博客,只需要一个指令 (Moka)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。