Markdown Renderer is a very simple HTTP server written in Go. It renders Markdown documents retrieved from another (specified) HTTP server into HTML.
Markdown Renderer uses package
github.com/shurcooL/github_flavored_markdown
to render Markdown documents.
Package github_flavored_markdown provides a GitHub Flavored Markdown renderer with fenced code block highlighting, clickable heading anchor links.
This is a real use case, and the one that motivated me to write Markdown Renderer.
In our company, we have an SVN server, on which our code and documents
reside. We would like to be able to browse our documents from the Web
browser, in particular, we want those documents in Markdown syntax
being renderred to HTML. However, the SVN server is not smart enough
to render Markdown documents; more than that, it does not even
recognizes file types of documents and returns all documents with
Content-Type: text/plain
anyway.
This inspires me to set up an Nginx server, which proxy_pass
es all
requests to the SVN server, and set the correct Content-Type
by the
file extension name of corresponding document. This can be done using
the more_set_headers
directive provided by Nginx module
HttpHeadersMoreModule
.
Any example Nginx configuration should be like this:
server {
location ~ \.docx$ {
more_set_headers application/msword;
}
location ~ \.xlsx$ {
more_set_headers application/vnd.ms-excel;
}
}
However, this module is not able to render Markdown text into HTML. Indeed, I cannot find an Nginx module that can do this. I tried to write one by my own; however, had I digged into this work could I realise what a pain it is to write an Nginx filter module! This made me to resort to an alternative way, to write a separate HTTP server, instead of an Nginx module. Thus comes Markdown Renderer.
With Markdown Renderer, a new location
line can be added to above
example configuration:
location ~ \.md$ {
proxy_pass http://localhost:8002;
}
where localhost:8002
is supposed to be the Markdown Renderer server
started with proper command line flags set. For example:
./markdown-renderer -addr=:8002 -dataserver="http://svn-server:9006
where svn-server:9006
is just a replaceholder; you should change it
to your SVN or document server.
you can also use it with local files:
./markdown-renderer -addr=:8002 -mode local -root /home/http/public_html/docs
The nginx.conf
attached with this project configures two Nginx
virtual servers: the document-type-recognizer server as described in
above use case, and one that mimics the SVN/document server.
The recognizer server listens on localhost:8001
, the Markdown
Renderer server listens on localhost:8002
, and the fake SVN server
listens on localhost:8003
. They work in a chain:
|browser|----|:8001|----(.md files)----|:8002|----|:8003|
\---(other docs)-------------/
If you want to setup this configuration on your computer and play with it, these are the steps:
Checkout and build Markdown Renderer:
export ~/Projects/markdown-renderer cd ~/Projects go get github.com/ihacklog/markdown-renderer
Download, build and install Nginx.
Make Nginx use the configuration file provided with Markdown Renderer.
cd /usr/local/nginx/conf # suppose that Nginx was installed here. mv nginx.conf nginx.conf.bak # backup the configuration file.
(Optional) Edit nginx.conf
to specify the document root
directory to be where Markdown Renderer source code is.
location / { root /home/hacklog/go/src/github.com/ihacklog/markdown-renderer; }
Start Nginx.
/usr/local/nginx/sbin/nginx
Build and start Markdown Renderer
cd ~/Projects/markdown-renderer/src/github.com/wangkuiyi/markdown-renderer go install ~/Projects/markdown-renderer/bin/markdown-renderer
Direct your Web browser to http://localhost:8001
and you should
see the README.md
file renderred into HTML.
Markdown Renderer requires that the Markdown filename matches the
regular expression ^/([_a-zA-Z0-9]+)\\.md$)
.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。