有没有什么用 Nodejs 做的,类似 octopress 的博客程序?
有没有什么用 Nodejs 做的,类似 octopress 的博客程序?
<p>我的要求:</p>
<ul> <li>可以直接读取 git repo 获取文章内容</li> <li>然后生成页面(可自定义使用 markdown、HTML 或者<strong>其他格式</strong>)</li> <li>最好使用自己的方法存储评论而非 disqus</li> </ul>
当然可以。针对您的需求,有一个非常符合的工具叫做Hexo。Hexo 是一个快速、简单且功能强大的静态博客生成器,它是用 Node.js 编写的。尽管 Hexo 默认不直接支持从 Git 仓库读取文章内容,但我们可以编写一些脚本来实现这一点。
示例代码
首先,我们需要安装 Hexo 和一些必要的依赖:
npm install -g hexo-cli
mkdir myblog && cd myblog
hexo init
npm install
接下来,我们可以创建一个脚本 fetchPosts.js
来从 Git 仓库获取文章内容并将其写入 Hexo 的 source/_posts 目录中:
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
// 指定你的 Git 仓库路径
const gitRepoPath = '/path/to/your/blog/repo';
// 拉取最新代码
exec(`git pull`, { cwd: gitRepoPath }, (error, stdout, stderr) => {
if (error) {
console.log(`Error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
// 读取文章文件
const postsDir = path.join(gitRepoPath, 'source/_posts');
fs.readdir(postsDir, (err, files) => {
if (err) throw err;
files.forEach(file => {
const content = fs.readFileSync(path.join(postsDir, file), 'utf8');
// 将内容写入 Hexo 目录
fs.writeFileSync(path.join(__dirname, 'source/_posts', file), content);
});
});
});
运行这个脚本将从指定的 Git 仓库拉取最新的文章,并将其复制到 Hexo 的 source/_posts 目录中。
自定义存储评论
对于评论系统,你可以选择使用 Node.js 实现一个简单的后端服务来处理评论。例如,可以使用 Express.js 创建一个简单的 API 接口来接收和存储评论数据:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
let comments = [];
app.post('/comments', (req, res) => {
const comment = req.body;
comments.push(comment);
res.status(201).send(comment);
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
然后,你可以在前端页面中添加一个表单来提交评论到这个 API。
通过这种方式,你可以使用 Node.js 构建一个类似于 Octopress 的博客程序,并满足你的特定需求。
类比 Jekyll 的搜索结果主要是这个: 像 Docpad, Petrify, Hyde, 具体等楼下
http://stackoverflow.com/questions/7327929/whats-the-best-way-or-package-to-build-a-static-site-using-node-js
这个我昨天看到了一个。也是人家自己码的。并且你这个感觉像wiki了。还用到git。
用node and git搭建一个个人的知识库应该不错。貌似在github上看到过实现。
我自己基于一些第三方库,写了一个md2html的工具,然后就在github上搭了自己的blog,蛮方便的。http://fengmk2.github.com/
有现成的当然最好,自己做毕竟要花时间……
确实有知识库的需求(给我的语言做文档……),您能否给我看看这个实现具体是什么?
对于你的需求,可以考虑使用基于 Node.js 的静态站点生成器 Hexo。Hexo 支持 Git 集成、Markdown 渲染以及自定义主题等功能。尽管它默认使用 Disqus 存储评论,但你可以通过插件或自定义实现评论功能。
示例代码
1. 初始化 Hexo
npm install -g hexo-cli
hexo init myblog
cd myblog
npm install
2. 使用 Git 集成
在 config.yml
中配置 Git:
deploy:
type: git
repository: <your-repo-url>
branch: master
使用 hexo deploy
命令部署到 Git。
3. 自定义渲染
Hexo 默认支持 Markdown 和 HTML,你也可以通过插件扩展支持其他格式。
例如,安装并使用 hexo-renderer-ejs
插件来支持 EJS 模板:
npm install hexo-renderer-ejs --save
在 _config.yml
中添加:
renderer:
ejs:
enable: true
4. 自定义评论系统
使用自定义的评论系统需要编写后端服务。以下是一个简单的 Node.js 实现:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
let comments = [];
app.post('/comments', (req, res) => {
const { author, content } = req.body;
comments.push({ author, content });
res.status(201).send(comments);
});
app.listen(3000, () => console.log('Server running on port 3000'));
将此服务与前端评论表单集成,并通过 AJAX 请求发送评论。
通过这些步骤,你可以构建一个类似于 Octopress 的 Node.js 博客程序,满足你的需求。