吉吉于

free

让Express支持Markdown

什么是Markdown,就是一种标记语言,能够清楚快捷的输出所要的文本内容(格式上有增强效果),支持html。Github上的readme就是用markdown来实现。今天学习一下为Node.js上Express添加Markdown功能。Express 并不直接支持markdown语法,需要为项目添加markdown-js模块的依赖。

1.修改pacage.json

原版:

{
    "name": "application-name"
    , "version": "0.0.1"
    , "private": true
    , "dependencies": {
      "express": "2.4.6"
    , "jade": ">= 0.0.1"
  }
}

修改为:

{
    "name": "LazyBlog"
    , "version": "0.0.1"
    , "private": true
    , "dependencies": {
      "express": "2.4.6"
    , "jade": ">= 0.0.1"
    , "markdown-js": ">= 0.0.1"
  }
}

 

然后CMD进入LazyBlog目录,安装依赖:npm install

会在LazyBlog生成node_modules 目录,里边有

 

现在可以写代码了,打开app.js,导入markdown模块

var express = require('express')
  , routes = require('./routes')
  , markdown=require('markdown-js');

var app = module.exports = express.createServer();

然后给Express注册Markdown渲染器

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.register('.md',{
	compile:function(str,options){
		var html=markdown.makeHtml(str);
		return function(locals){
			return html.replace(/\{([^}]+)\}/g,function(_,name){
				return locals[name];
			});
		}
	}
});
// Routes

接着添加对markdown的路由(如果这些看起来对你很头大,那么先学学Express.js吧,上一篇文章有写过。如果不知道路由什么意思?那就先去学Node.js吧…)

app.get('/', routes.index);
//app.get('/md5/:string',routes.md5);
app.get('/markdown',function(req,res){
	res.render('index.md',{layout:false});
});

路由写好了,它指向index.md,用它来处理markdown语法文本,所以我们要去views目录建立一个index.md文件。

打开index.md ,现在可以写一写markdown标记的文本了,这里我把windows下markdownpad的欢迎界面内容搬过来了,你可以自己写点别的,比如

This is a demo page
===================
[Lazynight](http://lazynight.me \"Click\")

MarkdownPad欢迎界面

# Welcome to MarkdownPad #

**MarkdownPad** is a full-featured Markdown editor for Windows. 

## Full control over your documents ##

Want to make something **bold**? Press `Ctrl + B`.

How about *italic*? Press `Ctrl + I`.

> Write a quote with `Ctrl + Q`

No matter what you're working on, you'll have quick access to Markdown syntax with handy keyboard shortcuts and toolbar buttons.

## See your changes instantly with LivePreview ##

Don't guess if your [hyperlink syntax](http://markdownpad.com) is correct; LivePreview will show you exactly what your document looks like every time you press a key.

## Make it your own ##

Fonts, sizes, color schemes, and even the HTML stylesheets are 100% customizable so you can turn MarkdownPad into your ideal editor.

 

 

 

转载请注明:于哲的博客 » 让Express支持Markdown