JS 使用 showdown 将 markdown 转 HTML (附 CSS)

4,830 阅读6分钟

MedusaSorcerer的博客


showdown GitHub

使用 showdownmarkdown 转换为 HTML 很实用, 特别是在使用 markdown 语法进行文章编辑的时候。

引入

下载依赖包后, 使用你的路径在 HTML 界面进行引用:

<script src="showdown-1.9.1/dist/showdown.min.js"></script>

使用

var converter = new showdown.Converter();  // 生成转换器对象
var html_info = converter.makeHtml(content); // 转换 content 内容

最终 html_info 就是我们利用转换器生成的 HTML 字符对象了, 你可以直接用 .innerHTML 进行使用了。

CSS

.content 是 markdown 的展示 HTML 所在的父标签 class 属性。

.content {
    margin: 0 auto;
    font-family: "Microsoft YaHei", arial, sans-serif;
    color: #444444;
    line-height: 1;
    padding: 30px;
    border-style: solid;
    border-color: #ffcdcd;
    border-width: 1px;
}

@media screen and (min-width: 768px) {
    .content {
        width: 90%;
        margin: 10px auto;
    }
}

.content h1,
.content h2,
.content h3,
.content h4 {
    color: #111111;
    font-weight: 400;
    margin-top: 1em;
}

.content h1,
.content h2,
.content h3,
.content h4,
.content h5 {
    font-family: Georgia, Palatino, serif;
    font-weight: bold;
}

.content h1,
.content h2,
.content h3,
.content h4,
.content h5,
.content p,
.content dl {
    margin-bottom: 16px;
    padding: 0;
}

.content h1 {
    font-size: 48px;
    line-height: 54px;
}

.content h2 {
    font-size: 36px;
    line-height: 42px;
}

.content h1,
.content h2 {
    border-bottom: 1px solid #EFEAEA;
    padding-bottom: 10px;
}

.content h3 {
    font-size: 24px;
    line-height: 30px;
}

.content h4 {
    font-size: 21px;
    line-height: 26px;
}

.content h5 {
    font-size: 18px;
}

.content a {
    color: #0099ff;
    margin: 0;
    padding: 0;
    vertical-align: baseline;
}

.content a:hover {
    text-decoration: none;
    color: #ff6600;
}

.content a:visited {
    /*color: purple;*/
}

.content ul,
.content ol {
    padding: 0;
    padding-left: 24px;
    margin: 0 0 0 5px;
    list-style: circle !important;
}

.content li {
    line-height: 24px;
    list-style: circle !important;
}

.content p,
.content ul,
.content ol {
    font-size: 16px;
    line-height: 24px;
}

.content ol ol,
.content ul ol {
    list-style-type: lower-roman;
}

.content code {
    font-family: Consolas, Monaco, Andale Mono, monospace;
    line-height: 1.5;
    font-size: 13px;
    border-style: solid;
    border-color: #f88a8a;
    border-width: 1px;
}

.content code,
.content pre {
    border-radius: 3px;
    background-color: #fdf3b8;
    color: red;
}

.content pre {
    margin-top: 10px;
}

.content code {
    font-family: Consolas, Monaco, Andale Mono, monospace;
    margin: 0 2px;
}

.content pre {
    line-height: 1.7em;
    overflow: auto;
    padding: 6px 10px;
    border-left: 5px solid #6CE26C;
}

.content pre > code {
    border: 0;
    display: inline;
    max-width: initial;
    padding: 0;
    margin: 0;
    overflow: initial;
    line-height: inherit;
    font-size: .85em;
    white-space: pre;
    background: 0 0;

}

.content code {
    color: #666555;
}


/** markdown preview plus 对于代码块的处理有些问题, 所以使用统一的颜色 */
/*code .keyword {
  color: #8959a8;
}

code .number {
  color: #f5871f;
}

code .comment {
  color: #998
}*/

.content aside {
    display: block;
    float: right;
    width: 390px;
}

.content blockquote {
    border-left: .5em solid #eee;
    padding: 0 0 0 2em;
    margin-left: 0;
}

.content blockquote cite {
    font-size: 14px;
    line-height: 20px;
    color: #bfbfbf;
}

.content blockquote cite:before {
    content: '\2014 \00A0';
}

.content blockquote p {
    color: #666;
}

.content hr {
    text-align: left;
    color: #999;
    height: 2px;
    padding: 0;
    margin: 16px 0;
    background-color: #e7e7e7;
    border: 0 none;
}

.content dl {
    padding: 0;
}

.content dl dt {
    padding: 10px 0;
    margin-top: 16px;
    font-size: 1em;
    font-style: italic;
    font-weight: bold;
}

.content dl dd {
    padding: 0 16px;
    margin-bottom: 16px;
}

.content dd {
    margin-left: 0;
}

/* Code below this line is copyright Twitter Inc. */

.content button,
.content input,
.content select,
.content textarea {
    font-size: 100%;
    margin: 0;
    vertical-align: baseline;
    *vertical-align: middle;
}

.content button,
.content input {
    line-height: normal;
    *overflow: visible;
}

.content button::-moz-focus-inner,
.content input::-moz-focus-inner {
    border: 0;
    padding: 0;
}

.content button,
.content input[type="button"],
.content input[type="reset"],
.content input[type="submit"] {
    cursor: pointer;
    -webkit-appearance: button;
}

.content input[type=checkbox],
.content input[type=radio] {
    cursor: pointer;
}

/* override default chrome & firefox settings */
.content input:not([type="image"]),
.content textarea {
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}

.content input[type="search"] {
    -webkit-appearance: textfield;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}

.content input[type="search"]::-webkit-search-decoration {
    -webkit-appearance: none;
}

.content label,
.content input,
.content select,
.content textarea {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 13px;
    font-weight: normal;
    line-height: normal;
    margin-bottom: 18px;
}

.content input[type=checkbox],
.content input[type=radio] {
    cursor: pointer;
    margin-bottom: 0;
}

.content input[type=text],
.content input[type=password],
.content textarea,
.content select {
    display: inline-block;
    width: 210px;
    padding: 4px;
    font-size: 13px;
    font-weight: normal;
    line-height: 18px;
    height: 18px;
    color: #808080;
    border: 1px solid #ccc;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}

.content select,
.content input[type=file] {
    height: 27px;
    line-height: 27px;
}

.content textarea {
    height: auto;
}

/* grey out placeholders */
.content :-moz-placeholder {
    color: #bfbfbf;
}

.content ::-webkit-input-placeholder {
    color: #bfbfbf;
}

.content input[type=text],
.content input[type=password],
.content select,
.content textarea {
    -webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
    -moz-transition: border linear 0.2s, box-shadow linear 0.2s;
    transition: border linear 0.2s, box-shadow linear 0.2s;
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
    -moz-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

.content input[type=text]:focus,
.content input[type=password]:focus,
.content textarea:focus {
    outline: none;
    border-color: rgba(82, 168, 236, 0.8);
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
    -moz-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6);
}

/* buttons */
.content button {
    display: inline-block;
    padding: 4px 14px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 13px;
    line-height: 18px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    background-color: #0064cd;
    background-repeat: repeat-x;
    background-image: -khtml-gradient(linear, left top, left bottom, from(#049cdb), to(#0064cd));
    background-image: -moz-linear-gradient(top, #049cdb, #0064cd);
    background-image: -ms-linear-gradient(top, #049cdb, #0064cd);
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #049cdb), color-stop(100%, #0064cd));
    background-image: -webkit-linear-gradient(top, #049cdb, #0064cd);
    background-image: -o-linear-gradient(top, #049cdb, #0064cd);
    background-image: linear-gradient(top, #049cdb, #0064cd);
    color: #fff;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
    border: 1px solid #004b9a;
    border-bottom-color: #003f81;
    -webkit-transition: 0.1s linear all;
    -moz-transition: 0.1s linear all;
    transition: 0.1s linear all;
    border-color: #0064cd #0064cd #003f81;
    border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
}

.content button:hover {
    color: #fff;
    background-position: 0 -15px;
    text-decoration: none;
}

.content button:active {
    -webkit-box-shadow: inset 0 3px 7px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
    -moz-box-shadow: inset 0 3px 7px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
    box-shadow: inset 0 3px 7px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.05);
}

.content button::-moz-focus-inner {
    padding: 0;
    border: 0;
}

.content table {
    *border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;
    width: 100%;
}

.content table {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    /*-webkit-box-shadow: 0 1px 1px #ccc;
    -moz-box-shadow: 0 1px 1px #ccc;
    box-shadow: 0 1px 1px #ccc;   */
}

.content table tr:hover {
    background: #fbf8e9;
    -o-transition: all 0.1s ease-in-out;
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;
}

.content table td,
.content .table th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;
}

.content table th {
    background-color: #dce9f9;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
    background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -moz-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -ms-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: -o-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image: linear-gradient(top, #ebf3fc, #dce9f9);
    /*-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;*/
    border-top: none;
    text-shadow: 0 1px 0 rgba(255, 255, 255, .5);
    padding: 5px;
}

.content table td:first-child,
.content table th:first-child {
    border-left: none;
}

.content table th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}

.content table th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}

.content table th:only-child {
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}

.content table tr:last-child td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}

.content table tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}