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 18:24:26 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
9d870f91cd09974ab83db326df5cd7e39c3d4637
9d870f91
1 parent
4e710839
封装显示markdown的模块
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
38 deletions
init/config.js
lib/view-markdown.js
server.js
init/config.js
View file @
9d870f9
...
...
@@ -12,7 +12,8 @@ const configure = customize({
"md"
:
{
"base_path"
:
"md"
,
"default_md"
:
"readme.md"
,
"theme"
:
"metro-lake"
"theme"
:
"metro-lake"
,
"toc"
:
true
}
});
...
...
lib/view-markdown.js
0 → 100644
View file @
9d870f9
/**
* view-markdown
* Created by lintry on 2018/1/9.
*/
const
path
=
require
(
'path'
);
class
ViewMarkdown
{
constructor
(
options
)
{
this
.
options
=
options
||
{};
if
(
!
this
.
options
.
base_path
)
{
throw
new
Error
(
'no base path for markdown'
)
}
}
render
(
filename
,
theme
)
{
const
options
=
this
.
options
;
//默认md
if
(
!
filename
||
filename
.
match
(
/.+
\/
$/
))
{
filename
=
(
filename
||
''
)
+
options
.
default_md
||
'readme.md'
}
let
file
=
path
.
resolve
(
options
.
base_path
,
filename
);
if
(
!
path
.
parse
(
file
).
ext
)
{
//追加后缀
file
+=
'.md'
}
const
mdview
=
new
(
require
(
'./mdview'
))({
parseTokens
:
true
,
toc
:
options
.
toc
});
let
result
=
mdview
.
renderFile
(
file
);
let
tittle
=
result
.
tokens
&&
result
.
tokens
[
1
]
&&
result
.
tokens
[
1
].
content
||
filename
;
theme
=
theme
||
options
.
theme
;
return
`
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<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
.
html
}
</div>
</body
</html>
`
;
}
}
module
.
exports
=
ViewMarkdown
\ No newline at end of file
server.js
View file @
9d870f9
...
...
@@ -3,54 +3,24 @@ const app = express();
const
config
=
require
(
'./init/config'
);
const
mdview
=
new
(
require
(
'./lib/mdview'
))({
parseTokens
:
true
,
toc
:
true
}),
path
=
require
(
'path'
);
const
path
=
require
(
'path'
);
const
MARKDOWN_FILE_BASE
=
path
.
resolve
(
process
.
cwd
(),
config
.
md
.
base_path
);
app
.
use
(
express
.
static
(
__dirname
+
'/themes'
));
app
.
use
(
express
.
static
(
MARKDOWN_FILE_BASE
));
function
sendTemplate
(
filename
,
theme
)
{
//默认md
if
(
!
filename
||
filename
.
match
(
/.+
\/
$/
))
{
filename
=
(
filename
||
''
)
+
config
.
md
.
default_md
}
let
file
=
path
.
resolve
(
MARKDOWN_FILE_BASE
,
filename
);
if
(
!
path
.
parse
(
file
).
ext
)
{
//追加后缀
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>
${
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
.
html
}
</div>
</body
</html>
`
;
}
app
.
get
(
'/**'
,
function
(
req
,
res
,
next
)
{
function
viewMarkdown
(
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
));
})
const
viewer
=
new
(
require
(
'./lib/view-markdown'
))(
config
.
md
);
res
.
send
(
viewer
.
render
(
urls
[
1
],
query
.
t
));
}
app
.
get
(
'/*'
,
viewMarkdown
);
const
PORT
=
config
.
system
.
bind_port
;
app
.
listen
(
PORT
);
...
...
Please
register
or
login
to post a comment