Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Shenlin
/
markdown-view
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
lintry
2018-01-09 17:30:20 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3e1a8bb375c2001afffd36a3909c396280acd759
3e1a8bb3
1 parent
b0bb3067
调整md的解析输出结构,增加对[TOC]的支持
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
11 deletions
bin/mdview
lib/mdview.js
package.json
server.js
bin/mdview
View file @
3e1a8bb
...
...
@@ -15,4 +15,4 @@ const mdview = new (require('../lib/mdview')),
const
filename
=
path
.
resolve
(
process
.
cwd
(),
target
);
var
result
=
mdview
.
renderFile
(
filename
);
console
.
log
(
result
);
console
.
log
(
result
.
html
);
...
...
lib/mdview.js
View file @
3e1a8bb
...
...
@@ -7,25 +7,38 @@ function MarkdownIt (opts) {
}
opts
=
opts
||
{};
let
plugin
=
require
(
'markdown-it-github-toc'
).
default
;
const
md
=
require
(
'markdown-it'
)(
opts
.
options
);
const
md
=
require
(
'markdown-it'
)(
opts
.
options
)
.
use
(
plugin
,
{
anchorLinkSymbol
:
'¶'
});
this
.
renderFile
=
function
(
filename
)
{
if
(
!
filename
)
{
throw
new
Error
(
'filename can not be null!'
)
}
let
result
;
let
result
=
{},
content
;
try
{
con
st
con
tent
=
fs
.
readFileSync
(
filename
).
toString
();
result
=
md
.
render
(
content
);
content
=
fs
.
readFileSync
(
filename
).
toString
();
result
.
html
=
md
.
render
(
content
);
}
catch
(
e
)
{
result
=
md
.
render
(
`
console
.
error
(
'convert md to html error'
,
e
);
result
.
html
=
md
.
render
(
`
# 404
*文件找不到!*
`
)
}
try
{
if
(
opts
.
parseTokens
)
{
result
.
tokens
=
md
.
parse
(
content
,
{});
}
}
catch
(
e
)
{
console
.
error
(
'parse md error'
,
e
)
}
return
result
;
}
}
...
...
package.json
View file @
3e1a8bb
...
...
@@ -17,6 +17,7 @@
"express"
:
"^4.16.2"
,
"github-markdown-css"
:
"^2.10.0"
,
"kml-customize"
:
"git+ssh://git@gitlab.kmlab.com/comm/customize.git#1.0.0"
,
"markdown-it"
:
"^8.4.0"
"markdown-it"
:
"^8.4.0"
,
"markdown-it-github-toc"
:
"^3.2.4"
}
}
...
...
server.js
View file @
3e1a8bb
...
...
@@ -3,7 +3,7 @@ const app = express();
const
config
=
require
(
'./init/config'
);
const
mdview
=
new
(
require
(
'./lib/mdview'
)),
const
mdview
=
new
(
require
(
'./lib/mdview'
))
({
parseTokens
:
true
})
,
path
=
require
(
'path'
);
const
MARKDOWN_FILE_BASE
=
path
.
resolve
(
process
.
cwd
(),
config
.
md
.
base_path
);
...
...
@@ -22,28 +22,33 @@ function sendTemplate (filename, theme) {
file
+=
'.md'
}
let
result
=
mdview
.
renderFile
(
file
);
let
tittle
=
result
.
tokens
&&
result
.
tokens
[
1
]
&&
result
.
tokens
[
1
].
content
||
filename
;
theme
=
theme
||
config
.
md
.
theme
;
return
`
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>
less theme for standard markdown
</title>
<title>
${
tittle
}
</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/markdown.css">
<link rel="stylesheet" href="/
${
theme
}
/
${
theme
}
.css">
</head>
<body>
<div class='markdown
${
theme
}
'>
${
result
}
${
result
.
html
}
</div>
</body
</html>
`
;
}
app
.
get
(
'/**'
,
function
(
req
,
res
)
{
app
.
get
(
'/**'
,
function
(
req
,
res
,
next
)
{
let
url
=
req
.
url
,
urls
=
url
.
match
(
/^
\/([\/\w]
+
)
/
)
||
[],
query
=
req
.
query
;
let
ext
=
path
.
parse
(
url
).
ext
;
if
(
ext
&&
ext
!==
'md'
)
{
return
next
();
}
res
.
send
(
sendTemplate
(
urls
[
1
],
query
.
t
));
})
...
...
Please
register
or
login
to post a comment